@@ -6,46 +6,49 @@ import android.view.View
66import android.view.ViewGroup
77import androidx.annotation.LayoutRes
88import androidx.core.view.isInvisible
9- import androidx.core.view.isVisible
109import androidx.recyclerview.widget.DiffUtil
10+ import androidx.recyclerview.widget.LinearLayoutManager
1111import androidx.recyclerview.widget.ListAdapter
1212import androidx.recyclerview.widget.RecyclerView
1313import coil.api.load
1414import com.hoc.pagination_mvi.R
1515import com.hoc.pagination_mvi.asObservable
16- import com.hoc.pagination_mvi.ui.main.MainContract.*
16+ import com.hoc.pagination_mvi.ui.main.MainContract.Item
17+ import com.hoc.pagination_mvi.ui.main.MainContract.PlaceholderState
1718import com.jakewharton.rxbinding3.view.clicks
1819import com.jakewharton.rxbinding3.view.detaches
1920import io.reactivex.disposables.CompositeDisposable
2021import io.reactivex.rxkotlin.addTo
2122import io.reactivex.rxkotlin.subscribeBy
2223import io.reactivex.subjects.PublishSubject
24+ import kotlinx.android.synthetic.main.recycler_item_horizontal_list.view.*
2325import kotlinx.android.synthetic.main.recycler_item_photo.view.*
2426import kotlinx.android.synthetic.main.recycler_item_placeholder.view.*
2527
26-
27- class MainAdapter (private val compositeDisposable : CompositeDisposable ) :
28- ListAdapter <Item , MainAdapter .VH >(object : DiffUtil .ItemCallback <Item >() {
29- override fun areItemsTheSame (oldItem : Item , newItem : Item ): Boolean {
30- return when {
31- oldItem is Item .Placeholder && newItem is Item .Placeholder -> true
32- oldItem is Item .HorizontalList && newItem is Item .HorizontalList -> true
33- oldItem is Item .Photo && newItem is Item .Photo -> oldItem.photo.id == newItem.photo.id
34- else -> oldItem == newItem
35- }
28+ private object DiffUtilItemCallback : DiffUtil.ItemCallback<Item>() {
29+ override fun areItemsTheSame (oldItem : Item , newItem : Item ): Boolean {
30+ return when {
31+ oldItem is Item .Placeholder && newItem is Item .Placeholder -> true
32+ oldItem is Item .HorizontalList && newItem is Item .HorizontalList -> true
33+ oldItem is Item .Photo && newItem is Item .Photo -> oldItem.photo.id == newItem.photo.id
34+ else -> oldItem == newItem
3635 }
36+ }
3737
38- override fun areContentsTheSame (oldItem : Item , newItem : Item ) = oldItem == newItem
38+ override fun areContentsTheSame (oldItem : Item , newItem : Item ) = oldItem == newItem
3939
40- override fun getChangePayload (oldItem : Item , newItem : Item ): Any? {
41- return when {
42- oldItem is Item .Placeholder && newItem is Item .Placeholder -> newItem.state
43- oldItem is Item .HorizontalList && newItem is Item .HorizontalList -> newItem
44- oldItem is Item .Photo && newItem is Item .Photo -> newItem.photo
45- else -> null
46- }
40+ override fun getChangePayload (oldItem : Item , newItem : Item ): Any? {
41+ return when {
42+ oldItem is Item .Placeholder && newItem is Item .Placeholder -> newItem.state
43+ oldItem is Item .HorizontalList && newItem is Item .HorizontalList -> newItem
44+ oldItem is Item .Photo && newItem is Item .Photo -> newItem.photo
45+ else -> null
4746 }
48- }) {
47+ }
48+ }
49+
50+ class MainAdapter (private val compositeDisposable : CompositeDisposable ) :
51+ ListAdapter <Item , MainAdapter .VH >(DiffUtilItemCallback ) {
4952
5053 private val retryS = PublishSubject .create<Unit >()
5154 val retryObservable get() = retryS.asObservable()
@@ -63,17 +66,14 @@ class MainAdapter(private val compositeDisposable: CompositeDisposable) :
6366 override fun onBindViewHolder (holder : VH , position : Int ) = holder.bind(getItem(position))
6467
6568 override fun onBindViewHolder (holder : VH , position : Int , payloads : MutableList <Any >) {
66- val payload = payloads.firstOrNull() ? : return holder.bind(getItem(position))
67- Log .d(" ###" , " [PAYLOAD] $payload " )
68-
69- if (payload is PlaceholderState && holder is PlaceHolderVH ) {
70- return holder.update(payload)
71- }
72- if (payload is Item .HorizontalList && holder is HorizontalListVH ) {
73- return holder.update(payload)
74- }
75- if (payload is Item .Photo && holder is PhotoVH ) {
76- return holder.update(payload)
69+ if (payloads.isEmpty()) return holder.bind(getItem(position))
70+ payloads.forEach { payload ->
71+ Log .d(" ###" , " [PAYLOAD] $payload " )
72+ when {
73+ payload is PlaceholderState && holder is PlaceHolderVH -> holder.update(payload)
74+ payload is Item .HorizontalList && holder is HorizontalListVH -> holder.update(payload)
75+ payload is Item .Photo && holder is PhotoVH -> holder.update(payload)
76+ }
7777 }
7878 }
7979
@@ -152,13 +152,33 @@ class MainAdapter(private val compositeDisposable: CompositeDisposable) :
152152 }
153153
154154 private class HorizontalListVH (itemView : View ) : VH(itemView) {
155+ private val recycler = itemView.recycler_horizontal!!
156+ private val progressBar = itemView.progress_bar_horizontal!!
157+ private val textError = itemView.text_error_horizontal!!
158+ private val buttonRetry = itemView.button_retry_horizontal!!
159+ private val adapter = HorizontalAdapter ()
160+
161+ init {
162+ recycler.run {
163+ setHasFixedSize(true )
164+ adapter = this @HorizontalListVH.adapter
165+ layoutManager = LinearLayoutManager (context, RecyclerView .HORIZONTAL , false )
166+ }
167+ }
168+
155169 override fun bind (item : Item ) {
156170 if (item !is Item .HorizontalList ) return
157171 update(item)
158172 }
159173
160174 fun update (item : Item .HorizontalList ) {
161- // TODO
175+ progressBar.isInvisible = ! item.isLoading
176+
177+ textError.isInvisible = item.error == null
178+ buttonRetry.isInvisible = item.error == null
179+ textError.text = item.error?.message
180+
181+ adapter.submitList(item.items)
162182 }
163183 }
164184}
0 commit comments