@@ -3,55 +3,83 @@ package com.intive.tmdbandroid.home.ui.adapters
33import android.content.res.Resources
44import android.view.LayoutInflater
55import android.view.ViewGroup
6- import androidx.paging.PagingDataAdapter
7- import androidx.recyclerview.widget.DiffUtil
8- import androidx.recyclerview.widget.LinearLayoutManager
9- import androidx.recyclerview.widget.RecyclerView
6+ import androidx.paging.AsyncPagingDataDiffer
7+ import androidx.paging.PagingData
8+ import androidx.recyclerview.widget.*
109import com.intive.tmdbandroid.R
10+ import com.intive.tmdbandroid.common.TVShowAsyncPagingDataDiffCallback
1111import com.intive.tmdbandroid.databinding.ItemHorizontalListBinding
1212import com.intive.tmdbandroid.databinding.ItemScreeningBinding
1313import com.intive.tmdbandroid.databinding.ItemTitleBinding
1414import com.intive.tmdbandroid.model.TVShow
1515
16- class TVShowPageAdapter (private val clickListener : ((TVShow ) -> Unit )) : PagingDataAdapter<TVShow, RecyclerView.ViewHolder>(COMPARATOR ) {
16+ class TVShowPageAdapter (private val clickListener : ((TVShow ) -> Unit )) : RecyclerView.Adapter< RecyclerView.ViewHolder>() {
1717 companion object {
18- private val COMPARATOR = object : DiffUtil .ItemCallback <TVShow >() {
19- override fun areItemsTheSame (oldItem : TVShow , newItem : TVShow ): Boolean = (oldItem == newItem)
20- override fun areContentsTheSame (oldItem : TVShow , newItem : TVShow ): Boolean = (oldItem == newItem)
21- }
22-
2318 private const val HEADER = 0
2419 private const val WATCHLIST = 1
2520 private const val POPULAR = 2
2621 }
2722
23+ var widthSize: Int = 0
24+ val adapterCallback = AdapterListUpdateCallback (this )
25+
26+ private val differ = AsyncPagingDataDiffer (
27+ TVShowAsyncPagingDataDiffCallback (),
28+ object : ListUpdateCallback {
29+ override fun onInserted (position : Int , count : Int ) {
30+ adapterCallback.onInserted(position + 1 , count)
31+ }
32+
33+ override fun onRemoved (position : Int , count : Int ) {
34+ adapterCallback.onRemoved(position + 1 , count)
35+ }
36+
37+ override fun onMoved (fromPosition : Int , toPosition : Int ) {
38+ adapterCallback.onMoved(fromPosition + 1 , toPosition + 1 )
39+ }
40+
41+ override fun onChanged (position : Int , count : Int , payload : Any? ) {
42+ adapterCallback.onChanged(position + 1 , count, payload)
43+ }
44+ }
45+ )
46+
47+ suspend fun submitData (tvShowPagingData : PagingData <TVShow >) {
48+ differ.submitData(tvShowPagingData)
49+ }
50+
2851 private val watchlistAdapter = WatchlistAdapter (clickListener)
2952
53+ override fun getItemCount (): Int {
54+ return differ.itemCount + 3
55+ }
56+
3057 fun refreshWatchlistAdapter (list : List <TVShow >) {
3158 watchlistAdapter.submitList(list)
3259 }
3360
3461 override fun getItemViewType (position : Int ): Int {
3562 return when (position) {
36- 0 ,2 -> HEADER
63+ 0 , 2 -> HEADER
3764 1 -> WATCHLIST
3865 else -> POPULAR
3966 }
4067 }
4168
4269 override fun onBindViewHolder (holder : RecyclerView .ViewHolder , position : Int ) {
70+
4371 when (holder) {
4472 is HeaderHolder -> holder.bind(position)
4573 is WatchlistHolder -> holder.bind()
46- is TVShowHolder -> getItem(position - 3 )?.let { holder.bind(it) }
74+ is TVShowHolder -> differ. getItem(position - 3 )?.let { holder.bind(it) }
4775 }
4876 }
4977
5078 override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): RecyclerView .ViewHolder {
5179 return when (viewType) {
5280 HEADER -> HeaderHolder (ItemTitleBinding .inflate(LayoutInflater .from(parent.context), parent, false ))
5381 WATCHLIST -> WatchlistHolder (ItemHorizontalListBinding .inflate(LayoutInflater .from(parent.context), parent, false ))
54- POPULAR -> TVShowHolder (ItemScreeningBinding .inflate(LayoutInflater .from(parent.context), parent, false ), clickListener )
82+ POPULAR -> TVShowHolder (ItemScreeningBinding .inflate(LayoutInflater .from(parent.context), parent, false ), clickListener)
5583 else -> throw Exception (" Illegal ViewType" )
5684 }
5785 }
@@ -78,6 +106,7 @@ class TVShowPageAdapter(private val clickListener: ((TVShow) -> Unit)) : PagingD
78106 fun bind () {
79107 rvWatchlist.apply {
80108 layoutManager = LinearLayoutManager (itemView.context, LinearLayoutManager .HORIZONTAL , false )
109+ watchlistAdapter.widthSize = widthSize
81110 adapter = watchlistAdapter
82111 }
83112 }
0 commit comments