fix crash after clicking on the highlight play button

This commit is contained in:
Jannik 2021-01-01 13:08:25 +01:00
parent 75ecac6144
commit e4ac01605f
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
3 changed files with 25 additions and 19 deletions

View File

@ -125,6 +125,14 @@ object AoDParser {
return media
}
fun markAsWatched(mediaId: Int, episodeId: Int) = GlobalScope.launch {
val episode = getMediaById(mediaId).getEpisodeById(episodeId)
episode.watched = true
sendCallback(episode.watchedCallback)
Log.d(javaClass.name, "Marked episode ${episode.id} as watched")
}
// TODO don't use jsoup here
fun sendCallback(callbackPath: String) = GlobalScope.launch(Dispatchers.IO) {
val headers = mutableMapOf(

View File

@ -106,7 +106,10 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
)
playMedia(mediaSource, replace, seekPosition)
MediaFragment.instance.updateWatchedState(currentEpisode) // watchedCallback for the new episode
// if episodes has not been watched, mark as watched
if (!episode.watched) {
AoDParser.markAsWatched(media.id, episode.id)
}
}
fun playMedia(source: MediaSource, replace: Boolean = false, seekPosition: Long = 0) {

View File

@ -32,14 +32,6 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
private lateinit var tmdb: TMDBResponse
private lateinit var nextEpisode: Episode
companion object {
lateinit var instance: MediaFragment
}
init {
instance = this
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentMediaBinding.inflate(inflater, container, false)
return binding.root
@ -61,6 +53,19 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
}
}
override fun onResume() {
super.onResume()
// only notify adapter, if initialized
if (this::adapterRecEpisodes.isInitialized) {
// TODO find a better solution for this
media.episodes.forEachIndexed() { index, episode ->
adapterRecEpisodes.updateWatchedState(episode.watched, index)
}
adapterRecEpisodes.notifyDataSetChanged()
}
}
/**
* if tmdb data is present, use it, else use the aod data
*/
@ -169,14 +174,4 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
(activity as MainActivity).startPlayer(media.id, ep.id)
}
fun updateWatchedState(ep: Episode) {
AoDParser.sendCallback(ep.watchedCallback)
// only notify adapter, if initialized
if (this::adapterRecEpisodes.isInitialized) {
adapterRecEpisodes.updateWatchedState(true, media.episodes.indexOf(ep))
adapterRecEpisodes.notifyDataSetChanged()
}
}
}