2021-02-06 19:02:12 +01:00
package org.mosad.teapod.ui.activity.main.fragments
2020-10-08 22:20:20 +02:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
2021-06-06 17:54:19 +02:00
import androidx.lifecycle.lifecycleScope
2022-01-06 18:57:49 +01:00
import com.bumptech.glide.Glide
2022-01-03 14:10:41 +01:00
import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
2020-10-15 21:00:31 +02:00
import kotlinx.coroutines.launch
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.FragmentHomeBinding
2022-01-03 14:10:41 +01:00
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
2022-01-06 18:57:49 +01:00
import org.mosad.teapod.parser.crunchyroll.Item
2022-01-06 18:39:23 +01:00
import org.mosad.teapod.parser.crunchyroll.SortBy
2020-10-15 21:00:31 +02:00
import org.mosad.teapod.util.adapter.MediaItemAdapter
import org.mosad.teapod.util.decoration.MediaItemDecoration
2021-01-20 16:10:41 +01:00
import org.mosad.teapod.util.showFragment
2022-01-05 00:28:49 +01:00
import org.mosad.teapod.util.toItemMediaList
2022-01-06 18:57:49 +01:00
import kotlin.random.Random
2020-10-08 22:20:20 +02:00
class HomeFragment : Fragment ( ) {
2020-11-25 22:35:55 +01:00
private lateinit var binding : FragmentHomeBinding
2022-01-05 00:28:49 +01:00
private lateinit var adapterUpNext : MediaItemAdapter
2022-01-03 14:10:41 +01:00
private lateinit var adapterWatchlist : MediaItemAdapter
2020-12-11 10:54:40 +01:00
private lateinit var adapterNewTitles : MediaItemAdapter
2021-01-21 18:22:53 +01:00
private lateinit var adapterTopTen : MediaItemAdapter
2020-10-08 22:20:20 +02:00
2022-01-06 18:57:49 +01:00
private lateinit var highlightMedia : Item
2020-12-06 15:18:15 +01:00
2020-11-25 22:35:55 +01:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = FragmentHomeBinding . inflate ( inflater , container , false )
return binding . root
2020-10-08 22:20:20 +02:00
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2021-06-06 17:54:19 +02:00
lifecycleScope . launch {
2020-12-06 15:18:15 +01:00
context ?. let {
initHighlight ( )
initRecyclerViews ( )
initActions ( )
}
}
}
2020-10-16 11:23:32 +02:00
2020-12-06 15:18:15 +01:00
private fun initHighlight ( ) {
2022-01-06 18:57:49 +01:00
lifecycleScope . launch {
val newTitles = Crunchyroll . browse ( sortBy = SortBy . NEWLY _ADDED , n = 10 )
highlightMedia = newTitles . items [ Random . nextInt ( newTitles . items . size ) ]
// add media item to gui
binding . textHighlightTitle . text = highlightMedia . title
Glide . with ( requireContext ( ) ) . load ( highlightMedia . images . poster _wide [ 0 ] [ 3 ] . source )
. into ( binding . imageHighlight )
// TODO watchlist indicator
2021-12-27 21:14:35 +01:00
// if (StorageController.myList.contains(0)) {
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
// } else {
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
// }
2022-01-06 18:57:49 +01:00
}
2020-10-16 11:23:32 +02:00
}
2022-01-03 14:10:41 +01:00
/ * *
* Suspend , since adapters need to be initialized before we can initialize the actions .
* /
private suspend fun initRecyclerViews ( ) {
binding . recyclerWatchlist . addItemDecoration ( MediaItemDecoration ( 9 ) )
2020-12-06 15:18:15 +01:00
binding . recyclerNewEpisodes . addItemDecoration ( MediaItemDecoration ( 9 ) )
2020-12-11 10:54:40 +01:00
binding . recyclerNewTitles . addItemDecoration ( MediaItemDecoration ( 9 ) )
2021-01-21 18:22:53 +01:00
binding . recyclerTopTen . addItemDecoration ( MediaItemDecoration ( 9 ) )
2020-12-06 15:18:15 +01:00
2022-01-03 14:10:41 +01:00
val asyncJobList = arrayListOf < Job > ( )
2020-12-06 15:18:15 +01:00
2022-01-05 00:28:49 +01:00
// continue watching
val upNextJob = lifecycleScope . launch {
// TODO create EpisodeItemAdapter, which will start the playback of the selected episode immediately
adapterUpNext = MediaItemAdapter ( Crunchyroll . upNextAccount ( ) . toItemMediaList ( ) )
binding . recyclerNewEpisodes . adapter = adapterUpNext
}
asyncJobList . add ( upNextJob )
2022-01-03 14:10:41 +01:00
// watchlist
val watchlistJob = lifecycleScope . launch {
2022-01-05 00:28:49 +01:00
adapterWatchlist = MediaItemAdapter ( Crunchyroll . watchlist ( 50 ) . toItemMediaList ( ) )
2022-01-03 14:10:41 +01:00
binding . recyclerWatchlist . adapter = adapterWatchlist
}
asyncJobList . add ( watchlistJob )
2022-01-06 18:57:49 +01:00
// new simulcasts
2022-01-06 18:39:23 +01:00
val simulcastsJob = lifecycleScope . launch {
2022-01-06 18:57:49 +01:00
// val latestSeasonTag = Crunchyroll.seasonList().items.first().id
// val newSimulcasts = Crunchyroll.browse(seasonTag = latestSeasonTag, n = 50)
2022-01-06 18:39:23 +01:00
val newSimulcasts = Crunchyroll . browse ( sortBy = SortBy . NEWLY _ADDED , n = 50 )
adapterNewTitles = MediaItemAdapter ( newSimulcasts . toItemMediaList ( ) )
binding . recyclerNewTitles . adapter = adapterNewTitles
}
asyncJobList . add ( simulcastsJob )
2021-01-21 18:22:53 +01:00
2022-01-06 18:57:49 +01:00
// newly added / top ten
val newlyAddedJob = lifecycleScope . launch {
adapterTopTen = MediaItemAdapter ( Crunchyroll . browse ( sortBy = SortBy . POPULARITY , n = 10 ) . toItemMediaList ( ) )
binding . recyclerTopTen . adapter = adapterTopTen
}
asyncJobList . add ( newlyAddedJob )
2022-01-03 14:10:41 +01:00
asyncJobList . joinAll ( )
2020-10-19 17:34:41 +02:00
}
private fun initActions ( ) {
2020-12-06 15:18:15 +01:00
binding . buttonPlayHighlight . setOnClickListener {
2022-01-06 18:57:49 +01:00
// TODO implement
2021-06-06 17:54:19 +02:00
lifecycleScope . launch {
2021-12-27 21:14:35 +01:00
//val media = AoDParser.getMediaById(0)
2020-12-06 15:18:15 +01:00
2021-12-27 21:14:35 +01:00
// Log.d(javaClass.name, "Starting Player with mediaId: ${media.aodId}")
//(activity as MainActivity).startPlayer(media.aodId, media.playlist.first().mediaId)
2020-12-06 15:18:15 +01:00
}
}
binding . textHighlightMyList . setOnClickListener {
2022-01-06 18:57:49 +01:00
// TODO implement
2022-01-03 14:10:41 +01:00
// if (StorageController.myList.contains(0)) {
// StorageController.myList.remove(0)
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
// } else {
// StorageController.myList.add(0)
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
// }
// StorageController.saveMyList(requireContext())
2020-12-06 15:18:15 +01:00
}
binding . textHighlightInfo . setOnClickListener {
2022-01-06 18:57:49 +01:00
activity ?. showFragment ( MediaFragment ( highlightMedia . id ) )
2021-01-20 16:10:41 +01:00
}
2022-01-05 00:28:49 +01:00
adapterUpNext . onItemClick = { id , _ ->
activity ?. showFragment ( MediaFragment ( id ) )
}
2022-01-03 14:10:41 +01:00
adapterWatchlist . onItemClick = { id , _ ->
2022-01-05 00:28:49 +01:00
activity ?. showFragment ( MediaFragment ( id ) )
2020-12-06 15:18:15 +01:00
}
2022-01-06 18:39:23 +01:00
adapterNewTitles . onItemClick = { id , _ ->
activity ?. showFragment ( MediaFragment ( id ) )
}
2022-01-06 18:57:49 +01:00
adapterTopTen . onItemClick = { id , _ ->
activity ?. showFragment ( MediaFragment ( id ) ) //(mediaId))
}
2020-10-08 22:20:20 +02:00
}
2020-12-06 15:18:15 +01:00
2022-01-06 18:57:49 +01:00
}