summaryrefslogtreecommitdiffstats
path: root/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/home/topsites/TopSitesPagerAdapter.kt
blob: a480f861aff544bd05bcd8929fa9ce2a8dd455ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.home.topsites

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.home.sessioncontrol.AdapterItem.TopSitePagerPayload
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
import org.mozilla.fenix.home.topsites.TopSitePagerViewHolder.Companion.TOP_SITES_PER_PAGE

class TopSitesPagerAdapter(
    private val appStore: AppStore,
    private val viewLifecycleOwner: LifecycleOwner,
    private val interactor: TopSiteInteractor,
) : ListAdapter<List<TopSite>, TopSiteViewHolder>(TopSiteListDiffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopSiteViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(TopSiteViewHolder.LAYOUT_ID, parent, false)
        return TopSiteViewHolder(view, appStore, viewLifecycleOwner, interactor)
    }

    override fun onBindViewHolder(
        holder: TopSiteViewHolder,
        position: Int,
        payloads: MutableList<Any>,
    ) {
        if (payloads.isNullOrEmpty()) {
            onBindViewHolder(holder, position)
        } else {
            if (payloads[0] is TopSitePagerPayload) {
                val adapter = holder.binding.topSitesList.adapter as TopSitesAdapter
                val payload = payloads[0] as TopSitePagerPayload

                update(payload, position, adapter)
            }
        }
    }

    @VisibleForTesting
    internal fun update(
        payload: TopSitePagerPayload,
        position: Int,
        adapter: TopSitesAdapter,
    ) {
        // Only currently selected page items need to be updated
        val currentPageChangedItems = getCurrentPageChanges(payload, position)

        // If no changes have been made to the current page no need to continue
        if (currentPageChangedItems.isEmpty()) return

        // Build the new list from the old one
        val refreshedItems: MutableList<TopSite> = mutableListOf()
        refreshedItems.addAll(adapter.currentList)

        // Update new list with the changed items
        currentPageChangedItems.forEach { item ->
            val index = item.first - (position * TOP_SITES_PER_PAGE)
            if (index in refreshedItems.indices) {
                refreshedItems[index] = item.second
            }
        }

        // Display the updated list without any of the removed items
        adapter.submitList(refreshedItems.filter { it.id != -1L })
    }

    /**
     * @returns the changed only items for the currently specified page in [position]
     */
    @VisibleForTesting
    internal fun getCurrentPageChanges(payload: TopSitePagerPayload, position: Int) =
        payload.changed.filter { changedPair ->
            if (position == 0) {
                changedPair.first < TOP_SITES_PER_PAGE
            } else {
                changedPair.first >= TOP_SITES_PER_PAGE
            }
        }

    override fun onBindViewHolder(holder: TopSiteViewHolder, position: Int) {
        val adapter = holder.binding.topSitesList.adapter as TopSitesAdapter
        adapter.submitList(getItem(position))
    }

    internal object TopSiteListDiffCallback : DiffUtil.ItemCallback<List<TopSite>>() {
        override fun areItemsTheSame(oldItem: List<TopSite>, newItem: List<TopSite>): Boolean {
            return oldItem.size == newItem.size
        }

        override fun areContentsTheSame(oldItem: List<TopSite>, newItem: List<TopSite>): Boolean {
            @Suppress("DiffUtilEquals")
            return newItem.zip(oldItem).all { (new, old) -> new == old }
        }

        override fun getChangePayload(oldItem: List<TopSite>, newItem: List<TopSite>): Any? {
            val changed = mutableSetOf<Pair<Int, TopSite>>()
            for ((index, item) in newItem.withIndex()) {
                if (oldItem.getOrNull(index) != item) {
                    changed.add(Pair(index, item))
                }
            }
            return if (changed.isNotEmpty()) TopSitePagerPayload(changed) else null
        }
    }
}