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
2021-12-27 21:14:35 +01:00
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
2020-11-15 13:39:33 +01:00
import kotlinx.coroutines.launch
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.FragmentLibraryBinding
2021-12-05 00:42:56 +01:00
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.util.ItemMedia
2022-12-03 00:05:57 +01:00
import org.mosad.teapod.util.adapter.MediaItemListAdapter
2020-11-15 13:39:33 +01:00
import org.mosad.teapod.util.decoration.MediaItemDecoration
2021-01-20 16:10:41 +01:00
import org.mosad.teapod.util.showFragment
2022-12-03 00:05:57 +01:00
import org.mosad.teapod.util.toItemMediaList
2020-10-08 22:20:20 +02:00
class LibraryFragment : Fragment ( ) {
2020-11-25 22:35:55 +01:00
private lateinit var binding : FragmentLibraryBinding
2022-12-03 00:05:57 +01:00
private lateinit var adapter : MediaItemListAdapter
2020-10-08 22:20:20 +02:00
2021-12-27 21:14:35 +01:00
private val itemList = arrayListOf < ItemMedia > ( )
private val pageSize = 30
private var nextItemIndex = 0
2020-11-25 22:35:55 +01:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = FragmentLibraryBinding . 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 )
2022-12-03 00:05:57 +01:00
binding . recyclerMediaLibrary . addItemDecoration ( MediaItemDecoration ( 9 ) )
// TODO replace with pagination3
// https://medium.com/swlh/paging3-recyclerview-pagination-made-easy-333c7dfa8797
binding . recyclerMediaLibrary . addOnScrollListener ( PaginationScrollListener ( ) )
2020-10-15 13:00:44 +02:00
// init async
2021-06-06 17:54:19 +02:00
lifecycleScope . launch {
2020-10-09 13:25:12 +02:00
// create and set the adapter, needs context
2021-06-06 17:54:19 +02:00
context ?. let {
2022-12-03 00:05:57 +01:00
itemList . addAll ( Crunchyroll . browse ( n = pageSize ) . toItemMediaList ( ) )
2021-12-27 21:14:35 +01:00
nextItemIndex += pageSize
2021-12-05 00:42:56 +01:00
2022-12-03 00:05:57 +01:00
adapter = MediaItemListAdapter ( MediaItemListAdapter . OnClickListener {
activity ?. showFragment ( MediaFragment ( it . id ) )
} )
2021-06-06 17:54:19 +02:00
binding . recyclerMediaLibrary . adapter = adapter
2022-12-03 00:05:57 +01:00
adapter . submitList ( itemList )
2020-10-09 13:02:58 +02:00
}
2020-10-08 22:20:20 +02:00
}
}
2021-12-27 21:14:35 +01:00
inner class PaginationScrollListener : RecyclerView . OnScrollListener ( ) {
private var isLoading = false
override fun onScrolled ( recyclerView : RecyclerView , dx : Int , dy : Int ) {
super . onScrolled ( recyclerView , dx , dy )
val layoutManager = recyclerView . layoutManager as GridLayoutManager ?
if ( !is Loading ) layoutManager ?. let {
// itemList.size - 5 to start loading a bit earlier than the actual end
if ( layoutManager . findLastCompletelyVisibleItemPosition ( ) >= ( itemList . size - 5 ) ) {
// load new browse results async
isLoading = true
lifecycleScope . launch {
val firstNewItemIndex = itemList . lastIndex + 1
2022-12-03 00:05:57 +01:00
itemList . addAll ( Crunchyroll . browse ( start = nextItemIndex , n = pageSize ) . toItemMediaList ( ) )
2021-12-27 21:14:35 +01:00
nextItemIndex += pageSize
adapter . notifyItemRangeInserted ( firstNewItemIndex , pageSize )
isLoading = false
}
}
}
}
}
2020-10-08 22:20:20 +02:00
}