2021-02-06 19:02:12 +01:00
package org.mosad.teapod.ui.activity.main.fragments
2021-12-29 19:36:33 +01:00
import android.annotation.SuppressLint
2021-02-06 19:02:12 +01:00
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2021-12-29 19:36:33 +01:00
import androidx.appcompat.widget.PopupMenu
2021-02-06 19:02:12 +01:00
import androidx.fragment.app.Fragment
2022-04-03 16:55:54 +02:00
import androidx.fragment.app.viewModels
2021-12-29 19:36:33 +01:00
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
2022-03-19 13:10:36 +01:00
import org.mosad.teapod.R
2021-12-29 19:36:33 +01:00
import org.mosad.teapod.databinding.FragmentMediaEpisodesBinding
2021-02-06 19:02:12 +01:00
import org.mosad.teapod.ui.activity.main.MainActivity
import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
import org.mosad.teapod.util.adapter.EpisodeItemAdapter
class MediaFragmentEpisodes : Fragment ( ) {
private lateinit var binding : FragmentMediaEpisodesBinding
private lateinit var adapterRecEpisodes : EpisodeItemAdapter
2022-04-03 16:55:54 +02:00
private val model : MediaFragmentViewModel by viewModels ( { requireParentFragment ( ) } )
2021-02-06 19:02:12 +01:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = FragmentMediaEpisodesBinding . inflate ( inflater , container , false )
return binding . root
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2022-01-05 01:28:39 +01:00
adapterRecEpisodes = EpisodeItemAdapter (
model . currentEpisodesCrunchy ,
model . tmdbTVSeason . episodes ,
2022-04-10 21:24:09 +02:00
model . currentPlayheads ,
EpisodeItemAdapter . OnClickListener { episode ->
playEpisode ( episode . seasonId , episode . id )
} ,
EpisodeItemAdapter . ViewType . MEDIA _FRAGMENT
2022-01-05 01:28:39 +01:00
)
2021-02-06 19:02:12 +01:00
binding . recyclerEpisodes . adapter = adapterRecEpisodes
2021-12-31 16:03:15 +01:00
// don't show season selection if only one season is present
if ( model . seasonsCrunchy . total < 2 ) {
binding . buttonSeasonSelection . visibility = View . GONE
} else {
2022-03-19 13:10:36 +01:00
binding . buttonSeasonSelection . text = getString (
R . string . season _number _title ,
model . currentSeasonCrunchy . seasonNumber ,
model . currentSeasonCrunchy . title
)
2021-12-31 16:03:15 +01:00
binding . buttonSeasonSelection . setOnClickListener { v ->
showSeasonSelection ( v )
}
2021-02-06 19:02:12 +01:00
}
}
2022-01-29 20:32:45 +01:00
@SuppressLint ( " NotifyDataSetChanged " )
2022-01-09 18:41:23 +01:00
fun updateWatchedState ( ) {
2022-01-29 20:32:45 +01:00
// model.currentPlayheads is a val mutable map -> notify dataset changed
2022-04-03 17:33:29 +02:00
if ( this :: adapterRecEpisodes . isInitialized ) {
adapterRecEpisodes . notifyDataSetChanged ( )
}
2022-01-09 18:41:23 +01:00
}
2021-12-29 19:36:33 +01:00
private fun showSeasonSelection ( v : View ) {
// TODO replace with Exposed dropdown menu: https://material.io/components/menus/android#exposed-dropdown-menus
val popup = PopupMenu ( requireContext ( ) , v )
model . seasonsCrunchy . items . forEach { season ->
2022-03-19 13:10:36 +01:00
popup . menu . add ( getString (
R . string . season _number _title ,
season . seasonNumber ,
season . title
)
) . also {
2021-12-29 19:36:33 +01:00
it . setOnMenuItemClickListener {
onSeasonSelected ( season . id )
false
}
}
}
popup . show ( )
}
/ * *
* Call model to load a new season .
* Once loaded update buttonSeasonSelection text and adapterRecEpisodes .
*
* Suppress waring since invalid .
* /
@SuppressLint ( " NotifyDataSetChanged " )
private fun onSeasonSelected ( seasonId : String ) {
// load the new season
lifecycleScope . launch {
model . setCurrentSeason ( seasonId )
2022-03-19 13:10:36 +01:00
binding . buttonSeasonSelection . text = getString (
R . string . season _number _title ,
model . currentSeasonCrunchy . seasonNumber ,
model . currentSeasonCrunchy . title
)
2021-12-29 19:36:33 +01:00
adapterRecEpisodes . notifyDataSetChanged ( )
}
}
2021-12-20 22:14:58 +01:00
private fun playEpisode ( seasonId : String , episodeId : String ) {
( activity as MainActivity ) . startPlayer ( seasonId , episodeId )
2021-08-31 19:47:18 +02:00
Log . d ( javaClass . name , " Started Player with episodeId: $episodeId " )
2021-02-06 19:02:12 +01:00
2021-12-20 22:14:58 +01:00
//model.updateNextEpisode(episodeId) // set the correct next episode
2021-02-06 19:02:12 +01:00
}
}