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.Episode
import org.mosad.teapod.util.StorageController
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 ->
tab . text = if ( model . media . type == MediaType . TVSHOW && position == 0 ) {
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
if ( model . nextEpisode . title . isNotEmpty ( ) ) {
binding . textTitle . text = model . nextEpisode . title
}
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
2020-10-16 19:56:08 +02:00
val backdropUrl = if ( tmdb . backdropUrl . isNotEmpty ( ) ) tmdb . backdropUrl else media . info . posterUrl
val posterUrl = if ( tmdb . posterUrl . isNotEmpty ( ) ) tmdb . posterUrl else media . info . posterUrl
2020-10-12 20:30:45 +02:00
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
2020-10-13 12:27:13 +02:00
Glide . with ( requireContext ( ) ) . load ( posterUrl )
2021-02-06 19:02:12 +01:00
. into ( binding . imagePoster )
2020-10-12 22:43:42 +02:00
2021-02-06 19:02:12 +01:00
binding . textTitle . text = media . info . title
binding . textYear . text = media . info . year . toString ( )
binding . textAge . text = media . info . age . toString ( )
binding . textOverview . text = media . info . shortDesc
2020-10-16 19:56:08 +02:00
if ( StorageController . myList . contains ( media . id ) ) {
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
2020-10-12 17:52:24 +02:00
if ( media . type == MediaType . TVSHOW ) {
2020-11-03 18:21:18 +01:00
// get next episode
nextEpisode = if ( media . episodes . firstOrNull { ! it . watched } != null ) {
media . episodes . first { ! it . watched }
} else {
media . episodes . first ( )
}
// title is the next episodes title
2021-02-06 19:02:12 +01:00
binding . textTitle . text = nextEpisode . title
// episodes count
binding . textEpisodesOrRuntime . text = resources . getQuantityString (
R . plurals . text _episodes _count ,
media . info . episodesCount ,
media . info . episodesCount
)
// episodes
fragments . add ( MediaFragmentEpisodes ( ) )
pagerAdapter . notifyDataSetChanged ( )
2020-10-12 17:52:24 +02:00
} else if ( media . type == MediaType . MOVIE ) {
2020-10-13 15:56:07 +02:00
if ( tmdb . runtime > 0 ) {
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 ,
tmdb . runtime ,
tmdb . runtime
)
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
if ( media . info . similar . isNotEmpty ( ) ) {
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 {
2020-10-12 17:52:24 +02:00
when ( media . type ) {
2021-02-06 19:02:12 +01:00
MediaType . MOVIE -> playEpisode ( media . episodes . first ( ) )
2020-11-03 18:21:18 +01:00
MediaType . TVSHOW -> playEpisode ( nextEpisode )
2020-10-12 22:43:42 +02:00
else -> Log . e ( javaClass . name , " Wrong Type: $media .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 {
2020-10-16 19:56:08 +02:00
if ( StorageController . myList . contains ( media . id ) ) {
StorageController . myList . remove ( media . id )
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 {
2020-10-16 19:56:08 +02:00
StorageController . myList . add ( media . id )
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
* /
2020-11-03 18:21:18 +01:00
private fun playEpisode ( ep : Episode ) {
2021-02-06 19:02:12 +01:00
( activity as MainActivity ) . startPlayer ( model . media . id , ep . id )
Log . d ( javaClass . name , " Started Player with episodeId: ${ep.id} " )
2020-11-03 18:21:18 +01:00
2021-02-06 19:02:12 +01:00
model . updateNextEpisode ( ep ) // 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
}
}