2021-02-06 19:02:12 +01:00
package org.mosad.teapod.ui.activity.main.fragments
2020-10-11 10:02:00 +02:00
2020-10-12 22:43:42 +02:00
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
2020-10-11 10:02:00 +02:00
import android.os.Bundle
2020-10-11 23:16:47 +02:00
import android.util.Log
2020-10-11 10:02:00 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2020-10-16 18:24:34 +02:00
import androidx.fragment.app.Fragment
2021-02-06 19:02:12 +01:00
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.activityViewModels
2021-06-06 17:54:19 +02:00
import androidx.lifecycle.lifecycleScope
2021-02-06 19:02:12 +01:00
import androidx.viewpager2.adapter.FragmentStateAdapter
2020-10-11 10:02:00 +02:00
import com.bumptech.glide.Glide
2020-10-12 22:43:42 +02:00
import com.bumptech.glide.request.RequestOptions
2021-02-20 22:57:00 +01:00
import com.google.android.material.appbar.AppBarLayout
2021-02-06 19:02:12 +01:00
import com.google.android.material.tabs.TabLayoutMediator
2020-10-12 22:43:42 +02:00
import jp.wasabeef.glide.transformations.BlurTransformation
2021-06-06 17:54:19 +02:00
import kotlinx.coroutines.launch
2020-10-11 10:02:00 +02:00
import org.mosad.teapod.R
2021-02-08 00:22:50 +01:00
import org.mosad.teapod.databinding.FragmentMediaBinding
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
2020-10-11 23:16:47 +02:00
import org.mosad.teapod.util.DataTypes.MediaType
2021-06-06 17:54:19 +02:00
import org.mosad.teapod.util.StorageController
2021-07-25 19:15:31 +02:00
import org.mosad.teapod.util.tmdb.TMDBMovie
2021-07-10 23:37:16 +02:00
import org.mosad.teapod.util.tmdb.TMDBApiController
2021-02-20 22:57:00 +01:00
2021-02-06 19:02:12 +01:00
/ * *
* The media detail fragment .
* Note : the fragment is created only once , when selecting a similar title etc .
* therefore fragments may be not empty and model may be the old one
* /
2020-11-25 23:26:46 +01:00
class MediaFragment ( private val mediaId : Int ) : Fragment ( ) {
2020-10-11 23:16:47 +02:00
2020-11-25 22:35:55 +01:00
private lateinit var binding : FragmentMediaBinding
2021-02-06 19:02:12 +01:00
private lateinit var pagerAdapter : FragmentStateAdapter
2020-11-25 23:26:46 +01:00
2021-02-06 19:02:12 +01:00
private val fragments = arrayListOf < Fragment > ( )
private val model : MediaFragmentViewModel by activityViewModels ( )
2020-10-11 10:02:00 +02:00
2020-11-25 22:35:55 +01:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = FragmentMediaBinding . inflate ( inflater , container , false )
return binding . root
2020-10-11 10:02:00 +02:00
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2020-11-25 23:26:46 +01:00
binding . frameLoading . visibility = View . VISIBLE
2021-03-11 19:11:12 +01:00
// tab layout and pager
2021-02-06 19:02:12 +01:00
pagerAdapter = ScreenSlidePagerAdapter ( requireActivity ( ) )
2021-02-08 00:22:50 +01:00
// fix material components issue #1878, if more tabs are added increase
binding . pagerEpisodesSimilar . offscreenPageLimit = 2
2021-02-06 19:02:12 +01:00
binding . pagerEpisodesSimilar . adapter = pagerAdapter
TabLayoutMediator ( binding . tabEpisodesSimilar , binding . pagerEpisodesSimilar ) { tab , position ->
2021-08-31 19:47:18 +02:00
tab . text = if ( model . media2 . type == MediaType . TVSHOW && position == 0 ) {
2021-02-06 19:02:12 +01:00
getString ( R . string . episodes )
} else {
getString ( R . string . similar _titles )
}
} . attach ( )
2021-06-06 17:54:19 +02:00
lifecycleScope . launch {
2021-02-06 19:02:12 +01:00
model . load ( mediaId ) // load the streams and tmdb for the selected media
2020-10-11 10:02:00 +02:00
2021-06-06 17:54:19 +02:00
updateGUI ( )
initActions ( )
2020-11-25 23:26:46 +01:00
}
2020-10-12 22:43:42 +02:00
}
2021-01-01 13:08:25 +01:00
override fun onResume ( ) {
super . onResume ( )
2021-02-27 21:12:42 +01:00
// update the next ep text if there is one, since it may have changed
2021-08-31 19:47:18 +02:00
println ( model . nextEpisodeId )
if ( model . media2 . getEpisodeById ( model . nextEpisodeId ) . title . isNotEmpty ( ) ) {
binding . textTitle . text = model . media2 . getEpisodeById ( model . nextEpisodeId ) . title
2021-02-27 21:12:42 +01:00
}
2021-01-01 13:08:25 +01:00
}
2020-10-12 22:43:42 +02:00
/ * *
* if tmdb data is present , use it , else use the aod data
* /
2021-02-06 19:02:12 +01:00
private fun updateGUI ( ) = with ( model ) {
2020-10-11 23:16:47 +02:00
// generic gui
2021-07-17 13:15:14 +02:00
val backdropUrl = tmdbResult ?. backdropPath ?. let { TMDBApiController . imageUrl + it }
2021-08-31 19:47:18 +02:00
?: media2 . posterURL
2021-07-17 13:15:14 +02:00
val posterUrl = tmdbResult ?. posterPath ?. let { TMDBApiController . imageUrl + it }
2021-08-31 19:47:18 +02:00
?: media2 . posterURL
2020-10-12 20:30:45 +02:00
2021-07-10 23:37:16 +02:00
// load poster and backdrop
Glide . with ( requireContext ( ) ) . load ( posterUrl )
. into ( binding . imagePoster )
2020-10-13 12:27:13 +02:00
Glide . with ( requireContext ( ) ) . load ( backdropUrl )
. apply ( RequestOptions . placeholderOf ( ColorDrawable ( Color . DKGRAY ) ) )
2020-10-16 10:05:11 +02:00
. apply ( RequestOptions . bitmapTransform ( BlurTransformation ( 20 , 3 ) ) )
2021-02-06 19:02:12 +01:00
. into ( binding . imageBackdrop )
2020-10-12 22:43:42 +02:00
2021-08-31 19:47:18 +02:00
binding . textTitle . text = media2 . title
binding . textYear . text = media2 . year . toString ( )
binding . textAge . text = media2 . age . toString ( )
binding . textOverview . text = media2 . shortText
2021-07-10 23:37:16 +02:00
// set "my list" indicator
2021-08-31 19:47:18 +02:00
if ( StorageController . myList . contains ( media2 . aodId ) ) {
2021-02-06 19:02:12 +01:00
Glide . with ( requireContext ( ) ) . load ( R . drawable . ic _baseline _check _24 ) . into ( binding . imageMyListAction )
2020-10-16 18:24:34 +02:00
} else {
2021-02-06 19:02:12 +01:00
Glide . with ( requireContext ( ) ) . load ( R . drawable . ic _baseline _add _24 ) . into ( binding . imageMyListAction )
2020-10-16 18:24:34 +02:00
}
2020-10-11 23:16:47 +02:00
2021-02-06 19:02:12 +01:00
// clear fragments, since it lives in onCreate scope (don't do this in onPause/onStop -> FragmentManager transaction)
fragments . clear ( )
pagerAdapter . notifyDataSetChanged ( )
2020-10-11 23:16:47 +02:00
// specific gui
2021-08-31 19:47:18 +02:00
if ( media2 . type == MediaType . TVSHOW ) {
2020-11-03 18:21:18 +01:00
// get next episode
2021-08-31 19:47:18 +02:00
nextEpisodeId = media2 . playlist . firstOrNull { ! it . watched } ?. mediaId
?: media2 . playlist . first ( ) . mediaId
2020-11-03 18:21:18 +01:00
// title is the next episodes title
2021-08-31 19:47:18 +02:00
binding . textTitle . text = media2 . getEpisodeById ( nextEpisodeId ) . title
2021-02-06 19:02:12 +01:00
// episodes count
binding . textEpisodesOrRuntime . text = resources . getQuantityString (
R . plurals . text _episodes _count ,
2021-08-31 19:47:18 +02:00
media2 . playlist . size ,
media2 . playlist . size
2021-02-06 19:02:12 +01:00
)
// episodes
fragments . add ( MediaFragmentEpisodes ( ) )
pagerAdapter . notifyDataSetChanged ( )
2021-08-31 19:47:18 +02:00
} else if ( media2 . type == MediaType . MOVIE ) {
2021-07-25 19:15:31 +02:00
val tmdbMovie = ( tmdbResult as TMDBMovie ? )
2020-10-13 15:56:07 +02:00
2021-07-17 13:15:14 +02:00
if ( tmdbMovie ?. runtime != null ) {
2021-02-06 19:02:12 +01:00
binding . textEpisodesOrRuntime . text = resources . getQuantityString (
2021-01-21 18:22:53 +01:00
R . plurals . text _runtime ,
2021-07-10 23:37:16 +02:00
tmdbMovie . runtime ,
tmdbMovie . runtime
2021-01-21 18:22:53 +01:00
)
2020-10-13 15:56:07 +02:00
} else {
2021-02-06 19:02:12 +01:00
binding . textEpisodesOrRuntime . visibility = View . GONE
2020-10-13 15:56:07 +02:00
}
2020-10-11 23:16:47 +02:00
}
2020-11-25 23:26:46 +01:00
2021-02-06 19:02:12 +01:00
// if has similar titles
2021-08-31 19:47:18 +02:00
if ( media2 . similar . isNotEmpty ( ) ) {
2021-02-06 19:02:12 +01:00
fragments . add ( MediaFragmentSimilar ( ) )
pagerAdapter . notifyDataSetChanged ( )
}
2021-02-20 22:57:00 +01:00
// disable scrolling on appbar, if no tabs where added
if ( fragments . isEmpty ( ) ) {
val params = binding . linearMedia . layoutParams as AppBarLayout . LayoutParams
params . scrollFlags = 0 // clear all scroll flags
}
2021-02-06 19:02:12 +01:00
binding . frameLoading . visibility = View . GONE // hide loading indicator
2020-10-11 10:02:00 +02:00
}
2021-02-06 19:02:12 +01:00
private fun initActions ( ) = with ( model ) {
2020-11-25 22:35:55 +01:00
binding . buttonPlay . setOnClickListener {
2021-08-31 19:47:18 +02:00
when ( media2 . type ) {
MediaType . MOVIE -> playEpisode ( media2 . playlist . first ( ) . mediaId )
MediaType . TVSHOW -> playEpisode ( nextEpisodeId )
else -> Log . e ( javaClass . name , " Wrong Type: ${media2.type} " )
2020-10-12 17:52:24 +02:00
}
2020-10-11 23:16:47 +02:00
}
2020-10-15 21:00:31 +02:00
// add or remove media from myList
2020-11-25 22:35:55 +01:00
binding . linearMyListAction . setOnClickListener {
2021-08-31 19:47:18 +02:00
if ( StorageController . myList . contains ( media2 . aodId ) ) {
StorageController . myList . remove ( media2 . aodId )
2020-11-25 22:35:55 +01:00
Glide . with ( requireContext ( ) ) . load ( R . drawable . ic _baseline _add _24 ) . into ( binding . imageMyListAction )
2020-10-16 18:24:34 +02:00
} else {
2021-08-31 19:47:18 +02:00
StorageController . myList . add ( media2 . aodId )
2020-11-25 22:35:55 +01:00
Glide . with ( requireContext ( ) ) . load ( R . drawable . ic _baseline _check _24 ) . into ( binding . imageMyListAction )
2020-10-15 21:00:31 +02:00
}
2020-10-16 19:56:08 +02:00
StorageController . saveMyList ( requireContext ( ) )
2020-10-16 11:23:32 +02:00
2020-10-16 18:24:34 +02:00
// notify home fragment on change
2020-10-16 11:23:32 +02:00
parentFragmentManager . findFragmentByTag ( " HomeFragment " ) ?. let {
( it as HomeFragment ) . updateMyListMedia ( )
}
2020-10-15 21:00:31 +02:00
}
2020-10-11 10:02:00 +02:00
}
2021-02-06 19:02:12 +01:00
/ * *
* play the current episode
* TODO this is also used in MediaFragmentEpisode , we should only have on implementation
* /
2021-08-31 19:47:18 +02:00
private fun playEpisode ( episodeId : Int ) {
( activity as MainActivity ) . startPlayer ( model . media2 . aodId , episodeId )
Log . d ( javaClass . name , " Started Player with episodeId: $episodeId " )
2020-11-03 18:21:18 +01:00
2021-08-31 19:47:18 +02:00
model . updateNextEpisode ( episodeId ) // set the correct next episode
2020-11-03 18:21:18 +01:00
}
2021-02-06 19:02:12 +01:00
/ * *
* A simple pager adapter
* /
private inner class ScreenSlidePagerAdapter ( fa : FragmentActivity ) : FragmentStateAdapter ( fa ) {
override fun getItemCount ( ) : Int = fragments . size
override fun createFragment ( position : Int ) : Fragment = fragments [ position ]
2020-10-11 10:02:00 +02:00
}
}