fix updateNextEpisode()

the function could crash, if no matching episode was found, if that's the case now, it returns the first episode
This commit is contained in:
Jannik 2021-02-02 23:50:43 +01:00
parent e650dc3d13
commit 25294c2d87
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
4 changed files with 8 additions and 10 deletions

View File

@ -183,7 +183,7 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
(activity as MainActivity).startPlayer(model.media.id, ep.id)
Log.d(javaClass.name, "Started Player with episodeId: ${ep.id}")
model.updateNextEpisode() // set the correct next episode
model.updateNextEpisode(ep) // set the correct next episode
}
/**

View File

@ -55,7 +55,7 @@ class MediaFragmentEpisodes : Fragment() {
(activity as MainActivity).startPlayer(model.media.id, ep.id)
Log.d(javaClass.name, "Started Player with episodeId: ${ep.id}")
model.updateNextEpisode() // set the correct next episode
model.updateNextEpisode(ep) // set the correct next episode
}
}

View File

@ -35,18 +35,14 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
}
/**
* based on watched state and current episode number set the next episode
* get the next episode based on episode number (the true next episode)
* if no matching is found, use first episode
*/
fun updateNextEpisode() {
fun updateNextEpisode(currentEp: Episode) {
if (media.type == MediaType.MOVIE) return // return if movie
val currentEpNumber = nextEpisode.number
nextEpisode = if (media.episodes.firstOrNull{ !it.watched } != null) {
media.episodes.first{ !it.watched && it.number > currentEpNumber }
} else {
media.episodes.first()
}
nextEpisode = media.episodes.firstOrNull{ it.number > currentEp.number }
?: media.episodes.first()
}
}

View File

@ -55,6 +55,7 @@ data class Media(
fun getEpisodeById(id: Int) = episodes.first { it.id == id }
}
// TODO all val?
data class Info(
var title: String = "",
var posterUrl: String = "",
@ -97,6 +98,7 @@ data class Stream(
/**
* this class is used for tmdb responses
* TODO why is runtime var?
*/
data class TMDBResponse(
val id: Int = 0,