2022-04-10 17:39:30 +02:00
/ * *
* Teapod
*
* Copyright 2020 - 2022 < seil0 @mosad . xyz >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
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
2022-04-10 17:39:30 +02:00
import android.util.Log
2020-10-08 22:20:20 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
2022-04-10 17:39:30 +02:00
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
2021-06-06 17:54:19 +02:00
import androidx.lifecycle.lifecycleScope
2022-04-10 17:39:30 +02:00
import androidx.lifecycle.repeatOnLifecycle
2022-01-06 18:57:49 +01:00
import com.bumptech.glide.Glide
2020-10-15 21:00:31 +02:00
import kotlinx.coroutines.launch
2022-04-10 17:39:30 +02:00
import org.mosad.teapod.R
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.FragmentHomeBinding
2022-04-10 17:39:30 +02:00
import org.mosad.teapod.ui.activity.main.viewmodel.HomeViewModel
import org.mosad.teapod.util.adapter.MediaItemListAdapter
2020-10-15 21:00:31 +02:00
import org.mosad.teapod.util.decoration.MediaItemDecoration
2022-04-10 17:39:30 +02:00
import org.mosad.teapod.util.setDrawableTop
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
2020-10-08 22:20:20 +02:00
class HomeFragment : Fragment ( ) {
2022-04-10 17:39:30 +02:00
private val classTag = javaClass . name
private val model : HomeViewModel by viewModels ( )
2020-11-25 22:35:55 +01:00
private lateinit var binding : FragmentHomeBinding
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 )
2020-12-06 15:18:15 +01:00
binding . recyclerNewEpisodes . addItemDecoration ( MediaItemDecoration ( 9 ) )
2022-04-10 17:39:30 +02:00
binding . recyclerWatchlist . 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-04-10 17:39:30 +02:00
binding . recyclerNewEpisodes . adapter = MediaItemListAdapter (
MediaItemListAdapter . OnClickListener {
activity ?. showFragment ( MediaFragment ( it . id ) )
}
)
2020-12-06 15:18:15 +01:00
2022-04-10 17:39:30 +02:00
binding . recyclerWatchlist . adapter = MediaItemListAdapter (
MediaItemListAdapter . OnClickListener {
activity ?. showFragment ( MediaFragment ( it . id ) )
}
)
2022-01-05 00:28:49 +01:00
2022-04-10 17:39:30 +02:00
binding . recyclerNewTitles . adapter = MediaItemListAdapter (
MediaItemListAdapter . OnClickListener {
activity ?. showFragment ( MediaFragment ( it . id ) )
}
)
2022-01-03 14:10:41 +01:00
2022-04-10 17:39:30 +02:00
binding . recyclerTopTen . adapter = MediaItemListAdapter (
MediaItemListAdapter . OnClickListener {
activity ?. showFragment ( MediaFragment ( it . id ) )
}
)
2022-01-06 18:39:23 +01:00
2022-04-10 17:39:30 +02:00
binding . textHighlightMyList . setOnClickListener {
model . toggleHighlightWatchlist ( )
2021-01-21 18:22:53 +01:00
2022-04-10 17:39:30 +02:00
// disable the watchlist button until the result has been loaded
binding . textHighlightMyList . isClickable = false
// TODO since this might take a few seconds show a loading animation for the watchlist button
2022-01-06 18:57:49 +01:00
}
2020-10-19 17:34:41 +02:00
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
}
}
2022-04-10 17:39:30 +02:00
viewLifecycleOwner . lifecycleScope . launch {
viewLifecycleOwner . repeatOnLifecycle ( Lifecycle . State . STARTED ) {
model . onUiState ( viewLifecycleOwner . lifecycleScope ) { uiState ->
when ( uiState ) {
is HomeViewModel . UiState . Normal -> bindUiStateNormal ( uiState )
is HomeViewModel . UiState . Loading -> bindUiStateLoading ( )
is HomeViewModel . UiState . Error -> bindUiStateError ( uiState )
}
}
}
2020-12-06 15:18:15 +01:00
}
2022-04-10 17:39:30 +02:00
}
2020-12-06 15:18:15 +01:00
2022-04-10 17:39:30 +02:00
private fun bindUiStateNormal ( uiState : HomeViewModel . UiState . Normal ) {
val adapterUpNext = binding . recyclerNewEpisodes . adapter as MediaItemListAdapter
adapterUpNext . submitList ( uiState . upNextItems . filter { ! it . fullyWatched } . toItemMediaList ( ) )
2021-01-20 16:10:41 +01:00
2022-04-10 17:39:30 +02:00
val adapterWatchlist = binding . recyclerWatchlist . adapter as MediaItemListAdapter
adapterWatchlist . submitList ( uiState . watchlistItems . toItemMediaList ( ) )
2022-01-05 00:28:49 +01:00
2022-04-10 17:39:30 +02:00
val adapterNewTitles = binding . recyclerNewTitles . adapter as MediaItemListAdapter
adapterNewTitles . submitList ( uiState . recentlyAddedItems . toItemMediaList ( ) )
val adapterTopTen = binding . recyclerTopTen . adapter as MediaItemListAdapter
adapterTopTen . submitList ( uiState . topTenItems . toItemMediaList ( ) )
2020-12-06 15:18:15 +01:00
2022-04-10 17:39:30 +02:00
// highlight item
binding . textHighlightTitle . text = uiState . highlightItem . title
Glide . with ( requireContext ( ) ) . load ( uiState . highlightItem . images . poster _wide [ 0 ] [ 3 ] . source )
. into ( binding . imageHighlight )
val iconHighlightWatchlist = if ( uiState . highlightIsWatchlist ) {
R . drawable . ic _baseline _check _24
} else {
R . drawable . ic _baseline _add _24
2022-01-06 18:39:23 +01:00
}
2022-04-10 17:39:30 +02:00
binding . textHighlightMyList . setDrawableTop ( iconHighlightWatchlist )
binding . textHighlightMyList . isClickable = true
2022-01-06 18:39:23 +01:00
2022-04-10 17:39:30 +02:00
binding . textHighlightInfo . setOnClickListener {
activity ?. showFragment ( MediaFragment ( uiState . highlightItem . id ) )
2022-01-06 18:57:49 +01:00
}
2020-10-08 22:20:20 +02:00
}
2020-12-06 15:18:15 +01:00
2022-04-10 17:39:30 +02:00
private fun bindUiStateLoading ( ) {
// currently not used
}
private fun bindUiStateError ( uiState : HomeViewModel . UiState . Error ) {
// currently not used
Log . e ( classTag , " A error occurred while loading a UiState: ${uiState.message} " )
}
2022-01-06 18:57:49 +01:00
}