2021-01-16 00:16:47 +01:00
package org.mosad.teapod.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
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
import jp.wasabeef.glide.transformations.BlurTransformation
2020-11-26 17:07:33 +01:00
import kotlinx.coroutines.*
2020-10-11 10:02:00 +02:00
import org.mosad.teapod.R
2021-01-16 00:16:47 +01:00
import org.mosad.teapod.activity.main.MainActivity
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.FragmentMediaBinding
2020-10-13 23:47:48 +02:00
import org.mosad.teapod.parser.AoDParser
2020-11-25 23:26:46 +01:00
import org.mosad.teapod.util.*
2020-10-11 23:16:47 +02:00
import org.mosad.teapod.util.DataTypes.MediaType
2020-10-16 18:24:34 +02:00
import org.mosad.teapod.util.adapter.EpisodeItemAdapter
2020-10-11 10:02:00 +02:00
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
2020-10-14 20:22:20 +02:00
private lateinit var adapterRecEpisodes : EpisodeItemAdapter
2020-11-25 23:26:46 +01:00
private lateinit var media : Media
private lateinit var tmdb : TMDBResponse
2020-11-03 18:21:18 +01:00
private lateinit var nextEpisode : Episode
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
2020-11-27 11:06:16 +01:00
GlobalScope . launch ( Dispatchers . Main ) {
2020-11-25 23:26:46 +01:00
// load the streams for the selected media
media = AoDParser . getMediaById ( mediaId )
tmdb = TMDBApiController ( ) . search ( media . info . title , media . type )
2020-10-11 10:02:00 +02:00
2020-11-27 11:06:16 +01:00
if ( this @MediaFragment . isAdded ) {
2020-11-25 23:26:46 +01:00
updateGUI ( )
initActions ( )
}
}
2020-10-12 22:43:42 +02:00
}
2021-01-01 13:08:25 +01:00
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 ( )
}
}
2020-10-12 22:43:42 +02:00
/ * *
* if tmdb data is present , use it , else use the aod data
* /
2020-11-25 23:26:46 +01:00
private fun updateGUI ( ) = with ( binding ) {
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 ) ) )
2020-11-25 23:26:46 +01:00
. into ( imageBackdrop )
2020-10-12 22:43:42 +02:00
2020-10-13 12:27:13 +02:00
Glide . with ( requireContext ( ) ) . load ( posterUrl )
2020-11-25 23:26:46 +01:00
. into ( imagePoster )
2020-10-12 22:43:42 +02:00
2020-11-25 23:26:46 +01:00
textTitle . text = media . info . title
textYear . text = media . info . year . toString ( )
textAge . text = media . info . age . toString ( )
textOverview . text = media . info . shortDesc
2020-10-16 19:56:08 +02:00
if ( StorageController . myList . contains ( media . id ) ) {
2020-11-25 23:26:46 +01:00
Glide . with ( requireContext ( ) ) . load ( R . drawable . ic _baseline _check _24 ) . into ( imageMyListAction )
2020-10-16 18:24:34 +02:00
} else {
2020-11-25 23:26:46 +01:00
Glide . with ( requireContext ( ) ) . load ( R . drawable . ic _baseline _add _24 ) . into ( imageMyListAction )
2020-10-16 18:24:34 +02:00
}
2020-10-11 23:16:47 +02:00
// specific gui
2020-10-12 17:52:24 +02:00
if ( media . type == MediaType . TVSHOW ) {
2020-10-16 18:24:34 +02:00
adapterRecEpisodes = EpisodeItemAdapter ( media . episodes )
2020-11-25 23:26:46 +01:00
recyclerEpisodes . adapter = adapterRecEpisodes
2020-10-11 23:16:47 +02:00
2020-11-25 22:35:55 +01:00
binding . textEpisodesOrRuntime . text = getString ( R . string . text _episodes _count , media . info . episodesCount )
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
2020-11-25 23:26:46 +01:00
textTitle . text = nextEpisode . title
2020-10-12 17:52:24 +02:00
} else if ( media . type == MediaType . MOVIE ) {
2020-11-25 23:26:46 +01:00
recyclerEpisodes . visibility = View . GONE
2020-10-13 15:56:07 +02:00
if ( tmdb . runtime > 0 ) {
2020-11-25 23:26:46 +01:00
textEpisodesOrRuntime . text = getString ( R . string . text _runtime , tmdb . runtime )
2020-10-13 15:56:07 +02:00
} else {
2020-11-25 23:26:46 +01:00
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
frameLoading . visibility = View . GONE // hide loading indicator
2020-10-11 10:02:00 +02:00
}
private fun initActions ( ) {
2020-11-25 22:35:55 +01:00
binding . buttonPlay . setOnClickListener {
2020-10-12 17:52:24 +02:00
when ( media . type ) {
2020-10-23 11:28:47 +02:00
MediaType . MOVIE -> playStream ( 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-12 09:54:32 +02:00
// set onItemClick only in adapter is initialized
if ( this :: adapterRecEpisodes . isInitialized ) {
2020-10-16 10:05:11 +02:00
adapterRecEpisodes . onImageClick = { _ , position ->
2020-11-03 18:21:18 +01:00
playEpisode ( media . episodes [ position ] )
2020-10-12 09:54:32 +02:00
}
2020-10-11 23:16:47 +02:00
}
2020-10-11 10:02:00 +02:00
}
2020-11-03 18:21:18 +01:00
private fun playEpisode ( ep : Episode ) {
playStream ( ep )
// update nextEpisode
nextEpisode = if ( media . episodes . firstOrNull { ! it . watched } != null ) {
media . episodes . first { ! it . watched }
} else {
media . episodes . first ( )
}
2020-11-25 22:35:55 +01:00
binding . textTitle . text = nextEpisode . title
2020-11-03 18:21:18 +01:00
}
2020-10-23 11:28:47 +02:00
private fun playStream ( ep : Episode ) {
2020-11-13 11:23:09 +01:00
Log . d ( javaClass . name , " Starting Player with mediaId: ${media.id} " )
( activity as MainActivity ) . startPlayer ( media . id , ep . id )
2020-10-11 10:02:00 +02:00
}
}