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-12-28 20:32:44 +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-12-20 22:14:58 +01:00
import org.mosad.teapod.parser.crunchyroll.Item
import org.mosad.teapod.parser.crunchyroll.NoneItem
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
* /
2021-12-20 22:14:58 +01:00
class MediaFragment ( private val mediaIdStr : String , mediaCr : Item = NoneItem ) : 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
2021-12-28 20:32:44 +01:00
// TODO is position 0 always episodes? (and 1 always similar titles)
TabLayoutMediator ( binding . tabEpisodesSimilar , binding . pagerEpisodesSimilar ) { tab , position ->
tab . text = when ( position ) {
0 -> getString ( R . string . episodes )
1 -> getString ( R . string . similar _titles )
else -> " "
}
} . attach ( )
2021-02-06 19:02:12 +01:00
2021-06-06 17:54:19 +02:00
lifecycleScope . launch {
2021-12-20 22:14:58 +01:00
model . loadCrunchy ( mediaIdStr )
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-12-28 20:32:44 +01:00
// TODO reimplement
// if (model.media.getEpisodeById(model.nextEpisodeId).title.isNotEmpty()) {
// binding.textTitle.text = model.media.getEpisodeById(model.nextEpisodeId).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
2021-07-17 13:15:14 +02:00
val backdropUrl = tmdbResult ?. backdropPath ?. let { TMDBApiController . imageUrl + it }
2021-12-28 20:32:44 +01:00
?: seriesCrunchy . images . poster _wide [ 0 ] [ 2 ] . source
2021-07-17 13:15:14 +02:00
val posterUrl = tmdbResult ?. posterPath ?. let { TMDBApiController . imageUrl + it }
2021-12-28 20:32:44 +01:00
?: seriesCrunchy . images . poster _tall [ 0 ] [ 2 ] . source
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-12-28 20:32:44 +01:00
binding . textTitle . text = seriesCrunchy . title
//binding.textYear.text = media.year.toString() // TODO get from tmdb
//binding.textAge.text = media.age.toString() // TODO get from tmdb
binding . textOverview . text = seriesCrunchy . description
2021-07-10 23:37:16 +02:00
2021-12-20 22:14:58 +01:00
// TODO set "my list" indicator
2021-12-28 20:32:44 +01:00
// if (StorageController.myList.contains(media.aodId)) {
// Glide.with(requireContext()).load(R.drawable.ic_baseline_check_24).into(binding.imageMyListAction)
// } else {
// Glide.with(requireContext()).load(R.drawable.ic_baseline_add_24).into(binding.imageMyListAction)
// }
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)
2021-09-05 00:04:59 +02:00
val fragmentsSize = if ( fragments . lastIndex < 0 ) 0 else fragments . lastIndex
2021-02-06 19:02:12 +01:00
fragments . clear ( )
2021-09-05 00:04:59 +02:00
pagerAdapter . notifyItemRangeRemoved ( 0 , fragmentsSize )
2021-02-06 19:02:12 +01:00
2021-12-28 20:32:44 +01:00
// add the episodes fragment (as tab)
MediaFragmentEpisodes ( ) . also {
fragments . add ( it )
pagerAdapter . notifyItemInserted ( fragments . indexOf ( it ) )
2020-10-11 23:16:47 +02:00
}
2020-11-25 23:26:46 +01:00
2021-12-28 20:32:44 +01:00
// TODO reimplement via tmdb/metaDB
// specific gui
// if (mediaCrunchy.type == MediaType.TVSHOW.str) {
// // TODO get next episode
//// nextEpisodeId = media.playlist.firstOrNull{ !it.watched }?.mediaId
//// ?: media.playlist.first().mediaId
//
// // TODO title is the next episodes title
//// binding.textTitle.text = media.getEpisodeById(nextEpisodeId).title
//
// // episodes count
// binding.textEpisodesOrRuntime.text = resources.getQuantityString(
// R.plurals.text_episodes_count,
// episodesCrunchy.total,
// episodesCrunchy.total
// )
//
// // episodes
// MediaFragmentEpisodes().also {
// fragments.add(it)
// pagerAdapter.notifyItemInserted(fragments.indexOf(it))
// }
// } else if (media.type == MediaType.MOVIE) {
// val tmdbMovie = (tmdbResult as TMDBMovie?)
//
// if (tmdbMovie?.runtime != null) {
// binding.textEpisodesOrRuntime.text = resources.getQuantityString(
// R.plurals.text_runtime,
// tmdbMovie.runtime,
// tmdbMovie.runtime
// )
// } else {
// binding.textEpisodesOrRuntime.visibility = View.GONE
// }
// }
2021-02-06 19:02:12 +01:00
// if has similar titles
2021-12-28 20:32:44 +01:00
// TODO reimplement
// if (media.similar.isNotEmpty()) {
// MediaFragmentSimilar().also {
// fragments.add(it)
// pagerAdapter.notifyItemInserted(fragments.indexOf(it))
// }
// }
2021-02-06 19:02:12 +01:00
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-12-28 20:32:44 +01:00
// TODO reimplement
// when (media.type) {
// MediaType.MOVIE -> playEpisode(media.playlist.first().mediaId)
// MediaType.TVSHOW -> playEpisode(nextEpisodeId)
// else -> Log.e(javaClass.name, "Wrong Type: ${media.type}")
// }
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-12-28 20:32:44 +01:00
// TODO reimplement
// if (StorageController.myList.contains(media.aodId)) {
// StorageController.myList.remove(media.aodId)
// Glide.with(requireContext()).load(R.drawable.ic_baseline_add_24).into(binding.imageMyListAction)
// } else {
// StorageController.myList.add(media.aodId)
// Glide.with(requireContext()).load(R.drawable.ic_baseline_check_24).into(binding.imageMyListAction)
// }
// StorageController.saveMyList(requireContext())
//
// // notify home fragment on change
// 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-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 " )
2020-11-03 18:21:18 +01:00
2021-12-20 22:14:58 +01: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
}
}