handle new intent in PiP mode

This commit is contained in:
jmir1 2022-04-15 18:11:57 +02:00
parent a8477019e0
commit a34cde7933
2 changed files with 31 additions and 8 deletions

View file

@ -71,10 +71,19 @@ class PlayerActivity :
} }
} }
} }
override fun onNewIntent(intent: Intent?) {
// TODO: When in PiP mode, selecting an episode from list should load new episode override fun onNewIntent(intent: Intent) {
// Currently, below finish simply closes the activity. I don't know how to return a new Intent to update the activity val anime = intent.extras!!.getLong("anime", -1)
finish() val episode = intent.extras!!.getLong("episode", -1)
if (anime == -1L || episode == -1L) {
finish()
return
}
presenter.saveEpisodeProgress(player.timePos, player.duration)
presenter.saveEpisodeHistory()
presenter.anime = null
presenter.init(anime, episode)
super.onNewIntent(intent) super.onNewIntent(intent)
} }

View file

@ -39,11 +39,15 @@ class PlayerPresenter(
private val preferences: PreferencesHelper = Injekt.get(), private val preferences: PreferencesHelper = Injekt.get(),
private val delayedTrackingStore: DelayedTrackingStore = Injekt.get(), private val delayedTrackingStore: DelayedTrackingStore = Injekt.get(),
) : BasePresenter<PlayerActivity>() { ) : BasePresenter<PlayerActivity>() {
/**
* The ID of the anime loaded in the player.
*/
var animeId: Long = -1L
/** /**
* The anime loaded in the player. It can be null when instantiated for a short time. * The anime loaded in the player. It can be null when instantiated for a short time.
*/ */
var anime: Anime? = null var anime: Anime? = null
private set
/** /**
* The episode id of the currently loaded episode. Used to restore from process kill. * The episode id of the currently loaded episode. Used to restore from process kill.
@ -60,7 +64,9 @@ class PlayerPresenter(
* Episode list for the active anime. It's retrieved lazily and should be accessed for the first * Episode list for the active anime. It's retrieved lazily and should be accessed for the first
* time in a background thread to avoid blocking the UI. * time in a background thread to avoid blocking the UI.
*/ */
val episodeList by lazy { lateinit var episodeList: List<Episode>
private fun initEpisodeList(): List<Episode> {
val anime = anime!! val anime = anime!!
val dbEpisodes = db.getEpisodes(anime).executeAsBlocking() val dbEpisodes = db.getEpisodes(anime).executeAsBlocking()
@ -93,7 +99,7 @@ class PlayerPresenter(
else -> dbEpisodes else -> dbEpisodes
} }
episodesForPlayer return episodesForPlayer
.sortedWith(getEpisodeSort(anime, sortDescending = false)) .sortedWith(getEpisodeSort(anime, sortDescending = false))
} }
@ -128,6 +134,10 @@ class PlayerPresenter(
return anime == null return anime == null
} }
private fun needsInit(newAnimeId: Long): Boolean {
return animeId != newAnimeId
}
/** /**
* Initializes this presenter with the given [animeId] and [initialEpisodeId]. This method will * Initializes this presenter with the given [animeId] and [initialEpisodeId]. This method will
* fetch the anime from the database and initialize the initial episode. * fetch the anime from the database and initialize the initial episode.
@ -155,12 +165,16 @@ class PlayerPresenter(
if (!needsInit()) return if (!needsInit()) return
this.anime = anime this.anime = anime
if (episodeId == -1L) episodeId = initialEpisodeId if (initialEpisodeId != -1L) episodeId = initialEpisodeId
checkTrackers(anime) checkTrackers(anime)
source = sourceManager.getOrStub(anime.source) source = sourceManager.getOrStub(anime.source)
if (needsInit(anime.id ?: -1L)) {
this.animeId = anime.id!!
episodeList = initEpisodeList()
}
currentEpisode = episodeList.first { initialEpisodeId == it.id } currentEpisode = episodeList.first { initialEpisodeId == it.id }
launchIO { launchIO {
try { try {