Skip to content

Commit

Permalink
fix episode sorting for real
Browse files Browse the repository at this point in the history
  • Loading branch information
jmir1 committed Jun 14, 2021
1 parent 636588f commit 24876cd
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,7 @@ class NotificationReceiver : BroadcastReceiver() {
* @param episode episode that needs to be opened
*/
internal fun openEpisodePendingActivity(context: Context, anime: Anime, episode: Episode): PendingIntent {
val episodeList = ArrayList<Episode>()
val newIntent = WatcherActivity.newIntent(context, anime, episode, episodeList)
val newIntent = WatcherActivity.newIntent(context, anime, episode)
return PendingIntent.getActivity(context, AnimeController.REQUEST_INTERNAL, newIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.min

class AnimeController :
Expand Down Expand Up @@ -905,15 +904,7 @@ class AnimeController :
fun openEpisode(episode: Episode, playerChangeRequested: Boolean = false) {
val activity = activity ?: return
launchIO {
val episodeList = ArrayList<Episode>()
val list = presenter.filteredAndSortedEpisodes
val idx = list.indexOf(EpisodeItem(episode, anime!!))
val upper = if (list.lastIndex < idx + 100) list.lastIndex else idx + 100
val lower = if (0 > idx - 100) 0 else idx - 100
for (episodeItem in list.slice(lower..upper)) {
episodeList.add(episodeItem.episode)
}
val intent = WatcherActivity.newIntent(activity, presenter.anime, episode, episodeList)
val intent = WatcherActivity.newIntent(activity, presenter.anime, episode)
val useInternal = if (preferences.alwaysUseExternalPlayer()) playerChangeRequested else !playerChangeRequested
if (useInternal) {
startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,9 @@ class AnimePresenter(
*/
fun getNextUnseenEpisode(): EpisodeItem? {
return if (sortDescending()) {
return filteredAndSortedEpisodes.find { !it.seen }
return filteredAndSortedEpisodes.findLast { !it.seen }
} else {
filteredAndSortedEpisodes.findLast { !it.seen }
filteredAndSortedEpisodes.find { !it.seen }
}
}

Expand All @@ -518,9 +518,9 @@ class AnimePresenter(
.filter { !it.seen && it.status == AnimeDownload.State.NOT_DOWNLOADED }
.distinctBy { it.name }
return if (sortDescending()) {
episodes
} else {
episodes.reversed()
} else {
episodes
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import eu.kanade.tachiyomi.data.backup.BackupRestoreService
import eu.kanade.tachiyomi.data.database.AnimeDatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Anime
import eu.kanade.tachiyomi.data.database.models.AnimeHistory
import eu.kanade.tachiyomi.data.database.models.Episode
import eu.kanade.tachiyomi.databinding.AnimeHistoryControllerBinding
import eu.kanade.tachiyomi.ui.anime.AnimeController
import eu.kanade.tachiyomi.ui.base.controller.DialogController
Expand All @@ -31,7 +30,6 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.appcompat.queryTextChanges
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy

/**
Expand Down Expand Up @@ -152,25 +150,24 @@ class AnimeHistoryController :

override fun onResumeClick(position: Int) {
val activity = activity ?: return
val (anime, chapter, _) = (adapter?.getItem(position) as? AnimeHistoryItem)?.mch ?: return
val (anime, chapter, _) = (adapter?.getItem(position) as? AnimeHistoryItem)?.aeh ?: return

val nextEpisode = presenter.getNextEpisode(chapter, anime)
if (nextEpisode != null) {
val episodeList = ArrayList<Episode>()
val newIntent = WatcherActivity.newIntent(activity, anime, nextEpisode, episodeList)
val newIntent = WatcherActivity.newIntent(activity, anime, nextEpisode)
startActivity(newIntent)
} else {
activity.toast(R.string.no_next_chapter)
activity.toast(R.string.no_next_episode)
}
}

override fun onRemoveClick(position: Int) {
val (anime, _, animehistory) = (adapter?.getItem(position) as? AnimeHistoryItem)?.mch ?: return
RemoveAnimeHistoryDialog(this, anime, animehistory).showDialog(router)
val (anime, _, animeHistory) = (adapter?.getItem(position) as? AnimeHistoryItem)?.aeh ?: return
RemoveAnimeHistoryDialog(this, anime, animeHistory).showDialog(router)
}

override fun onItemClick(position: Int) {
val anime = (adapter?.getItem(position) as? AnimeHistoryItem)?.mch?.anime ?: return
val anime = (adapter?.getItem(position) as? AnimeHistoryItem)?.aeh?.anime ?: return
router.pushController(AnimeController(anime).withFadeTransaction())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.AnimeEpisodeHistory
import eu.kanade.tachiyomi.ui.recent.DateSectionItem

class AnimeHistoryItem(val mch: AnimeEpisodeHistory, header: DateSectionItem) :
class AnimeHistoryItem(val aeh: AnimeEpisodeHistory, header: DateSectionItem) :
AbstractSectionableItem<AnimeHistoryHolder, DateSectionItem>(header) {

override fun getLayoutRes(): Int {
Expand All @@ -26,17 +26,17 @@ class AnimeHistoryItem(val mch: AnimeEpisodeHistory, header: DateSectionItem) :
position: Int,
payloads: List<Any?>?
) {
holder.bind(mch)
holder.bind(aeh)
}

override fun equals(other: Any?): Boolean {
if (other is AnimeHistoryItem) {
return mch.anime.id == other.mch.anime.id
return aeh.anime.id == other.aeh.anime.id
}
return false
}

override fun hashCode(): Int {
return mch.anime.id!!.hashCode()
return aeh.anime.id!!.hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ class AnimeHistoryPresenter : BasePresenter<AnimeHistoryController>() {
/**
* Retrieves the next chapter of the given one.
*
* @param chapter the chapter of the animehistory object.
* @param episode the chapter of the animehistory object.
* @param anime the anime of the chapter.
*/
fun getNextEpisode(chapter: Episode, anime: Anime): Episode? {
if (!chapter.seen) {
return chapter
fun getNextEpisode(episode: Episode, anime: Anime): Episode? {
if (!episode.seen) {
return episode
}

val sortFunction: (Episode, Episode) -> Int = when (anime.sorting) {
Expand All @@ -129,13 +129,13 @@ class AnimeHistoryPresenter : BasePresenter<AnimeHistoryController>() {
}

val chapters = db.getEpisodes(anime).executeAsBlocking()
.sortedWith { c1, c2 -> sortFunction(c2, c1) }
.sortedWith { c1, c2 -> sortFunction(c1, c2) }

val currEpisodeIndex = chapters.indexOfFirst { chapter.id == it.id }
val currEpisodeIndex = chapters.indexOfFirst { episode.id == it.id }
return when (anime.sorting) {
Anime.EPISODE_SORTING_SOURCE -> chapters.getOrNull(currEpisodeIndex + 1)
Anime.EPISODE_SORTING_NUMBER -> {
val chapterNumber = chapter.episode_number
val chapterNumber = episode.episode_number

((currEpisodeIndex + 1) until chapters.size)
.map { chapters[it] }
Expand All @@ -146,7 +146,7 @@ class AnimeHistoryPresenter : BasePresenter<AnimeHistoryController>() {
}
Anime.EPISODE_SORTING_UPLOAD_DATE -> {
chapters.drop(currEpisodeIndex + 1)
.firstOrNull { it.date_upload >= chapter.date_upload }
.firstOrNull { it.date_upload >= episode.date_upload }
}
else -> throw NotImplementedError("Unknown sorting method")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import eu.davidea.flexibleadapter.SelectableAdapter
import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.animelib.AnimelibUpdateService
import eu.kanade.tachiyomi.data.database.models.Episode
import eu.kanade.tachiyomi.data.download.AnimeDownloadService
import eu.kanade.tachiyomi.data.download.model.AnimeDownload
import eu.kanade.tachiyomi.data.notification.Notifications
Expand Down Expand Up @@ -207,8 +206,7 @@ class AnimeUpdatesController :
*/
private fun openEpisode(item: AnimeUpdatesItem) {
val activity = activity ?: return
val episodeList = ArrayList<Episode>()
val intent = WatcherActivity.newIntent(activity, item.anime, item.episode, episodeList)
val intent = WatcherActivity.newIntent(activity, item.anime, item.episode)
startActivity(intent)
}

Expand Down
81 changes: 55 additions & 26 deletions app/src/main/java/eu/kanade/tachiyomi/ui/watcher/WatcherActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class WatcherActivity : AppCompatActivity() {
private lateinit var uri: String
private lateinit var links: List<Link>
private var currentQuality = 0
private lateinit var episodeList: ArrayList<Episode>

private var duration: Long = 0
private var currentWindow = 0
Expand Down Expand Up @@ -129,7 +128,6 @@ class WatcherActivity : AppCompatActivity() {
source = Injekt.get<AnimeSourceManager>().getOrStub(anime.source)
userAgentString = WebSettings.getDefaultUserAgent(this)
Timber.w(userAgentString)
episodeList = intent.getSerializableExtra("episodeList") as ArrayList<Episode>
links = EpisodeLoader.getLinks(episode, anime, source)
if (links.lastIndex > 0) settingsBtn.visibility = View.VISIBLE
uri = links.first().url
Expand Down Expand Up @@ -183,29 +181,17 @@ class WatcherActivity : AppCompatActivity() {
captcha()
}
skipBtn.setOnClickListener { exoPlayer.seekTo(exoPlayer.currentPosition + 85000) }
setBtnListeners()
nextBtn.setOnClickListener {
nextEpisode()
}
prevBtn.setOnClickListener {
previousEpisode()
}
youTubeDoubleTap.player(exoPlayer)
playerView.player = exoPlayer
duration = exoPlayer.duration
}

private fun setBtnListeners() {
if (episodeList.indexOf(episode) != episodeList.lastIndex && episodeList.isNotEmpty()) {
nextBtn.setOnClickListener {
nextEpisode()
}
} else {
nextBtn.setOnClickListener(null)
}
if (episodeList.indexOf(episode) != 0 && episodeList.isNotEmpty()) {
prevBtn.setOnClickListener {
previousEpisode()
}
} else {
prevBtn.setOnClickListener(null)
}
}

override fun onBackPressed() {
releasePlayer()
super.onBackPressed()
Expand All @@ -224,9 +210,10 @@ class WatcherActivity : AppCompatActivity() {
private fun nextEpisode() {
saveEpisodeHistory(EpisodeItem(episode, anime))
setEpisodeProgress(episode, anime, exoPlayer.currentPosition, exoPlayer.duration)
episode = episodeList[episodeList.indexOf(episode) + 1]
val oldEpisode = episode
episode = getNextEpisode(episode, anime)
if (oldEpisode == episode) return
title.text = anime.title + " - " + episode.name
setBtnListeners()
links = EpisodeLoader.getLinks(episode, anime, source)
settingsBtn.visibility = if (links.lastIndex > 0) View.VISIBLE else View.GONE
uri = links.first().url
Expand All @@ -241,9 +228,10 @@ class WatcherActivity : AppCompatActivity() {
private fun previousEpisode() {
saveEpisodeHistory(EpisodeItem(episode, anime))
setEpisodeProgress(episode, anime, exoPlayer.currentPosition, exoPlayer.duration)
episode = episodeList[episodeList.indexOf(episode) - 1]
val oldEpisode = episode
episode = getPreviousEpisode(episode, anime)
if (oldEpisode == episode) return
title.text = anime.title + " - " + episode.name
setBtnListeners()
links = EpisodeLoader.getLinks(episode, anime, source)
settingsBtn.visibility = if (links.lastIndex > 0) View.VISIBLE else View.GONE
uri = links.first().url
Expand Down Expand Up @@ -475,13 +463,54 @@ class WatcherActivity : AppCompatActivity() {
}
}

private fun getNextEpisode(episode: Episode, anime: Anime): Episode {
val sortFunction: (Episode, Episode) -> Int = when (anime.sorting) {
Anime.EPISODE_SORTING_SOURCE -> { c1, c2 -> c2.source_order.compareTo(c1.source_order) }
Anime.EPISODE_SORTING_NUMBER -> { c1, c2 -> c1.episode_number.compareTo(c2.episode_number) }
Anime.EPISODE_SORTING_UPLOAD_DATE -> { c1, c2 -> c1.date_upload.compareTo(c2.date_upload) }
else -> throw NotImplementedError("Unknown sorting method")
}

val episodes = db.getEpisodes(anime).executeAsBlocking()
.sortedWith { e1, e2 -> sortFunction(e1, e2) }

val currEpisodeIndex = episodes.indexOfFirst { episode.id == it.id }
val episodeNumber = episode.episode_number
return ((currEpisodeIndex + 1) until episodes.size)
.map { episodes[it] }
.firstOrNull {
it.episode_number > episodeNumber &&
it.episode_number <= episodeNumber + 1
} ?: episode
}

private fun getPreviousEpisode(episode: Episode, anime: Anime): Episode {
val sortFunction: (Episode, Episode) -> Int = when (anime.sorting) {
Anime.EPISODE_SORTING_SOURCE -> { c1, c2 -> c2.source_order.compareTo(c1.source_order) }
Anime.EPISODE_SORTING_NUMBER -> { c1, c2 -> c1.episode_number.compareTo(c2.episode_number) }
Anime.EPISODE_SORTING_UPLOAD_DATE -> { c1, c2 -> c1.date_upload.compareTo(c2.date_upload) }
else -> throw NotImplementedError("Unknown sorting method")
}

val episodes = db.getEpisodes(anime).executeAsBlocking()
.sortedWith { e1, e2 -> sortFunction(e2, e1) }

val currEpisodeIndex = episodes.indexOfFirst { episode.id == it.id }
val episodeNumber = episode.episode_number
return ((currEpisodeIndex + 1) until episodes.size)
.map { episodes[it] }
.firstOrNull {
it.episode_number < episodeNumber &&
it.episode_number >= episodeNumber - 1
} ?: episode
}

companion object {
fun newIntent(context: Context, anime: Anime, episode: Episode, episodeList: ArrayList<Episode>): Intent {
fun newIntent(context: Context, anime: Anime, episode: Episode): Intent {
return Intent(context, WatcherActivity::class.java).apply {
putExtra("anime", anime)
putExtra("episode", episode)
putExtra("second", episode.last_second_seen)
putExtra("episodeList", episodeList)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
}
Expand Down

0 comments on commit 24876cd

Please sign in to comment.