SessionControlAdapter.kt 11.2 KB
Newer Older
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
3
 * 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/. */
4
5
6

package org.mozilla.fenix.home.sessioncontrol

7
import android.content.Context
8
9
import android.view.LayoutInflater
import android.view.ViewGroup
10
import androidx.annotation.LayoutRes
11
import androidx.lifecycle.LifecycleOwner
12
import androidx.recyclerview.widget.DiffUtil
13
import androidx.recyclerview.widget.ListAdapter
14
import androidx.recyclerview.widget.RecyclerView
15
import mozilla.components.feature.tab.collections.TabCollection
16
import mozilla.components.feature.top.sites.TopSite
17
import org.mozilla.fenix.components.Components
18
import org.mozilla.fenix.components.tips.Tip
19
import org.mozilla.fenix.home.OnboardingState
20
21
import org.mozilla.fenix.home.sessioncontrol.viewholders.CollectionHeaderViewHolder
import org.mozilla.fenix.home.sessioncontrol.viewholders.CollectionViewHolder
22
import org.mozilla.fenix.home.sessioncontrol.viewholders.NoCollectionsMessageViewHolder
23
import org.mozilla.fenix.home.sessioncontrol.viewholders.PrivateBrowsingDescriptionViewHolder
24
import org.mozilla.fenix.home.sessioncontrol.viewholders.TorBootstrapPagerViewHolder
25
import org.mozilla.fenix.home.sessioncontrol.viewholders.TabInCollectionViewHolder
26
import org.mozilla.fenix.home.sessioncontrol.viewholders.TopSitePagerViewHolder
27
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingAutomaticSignInViewHolder
28
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingFinishViewHolder
29
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingHeaderViewHolder
30
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingManualSignInViewHolder
31
32
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingPrivacyNoticeViewHolder
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingPrivateBrowsingViewHolder
33
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingSectionHeaderViewHolder
34
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingThemePickerViewHolder
35
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingToolbarPositionPickerViewHolder
36
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingTrackingProtectionViewHolder
37
import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingWhatsNewViewHolder
38
import org.mozilla.fenix.home.tips.ButtonTipViewHolder
39
import mozilla.components.feature.tab.collections.Tab as ComponentTab
40

41
sealed class AdapterItem(@LayoutRes val viewType: Int) {
42
    data class TipItem(val tip: Tip) : AdapterItem(
43
44
45
        ButtonTipViewHolder.LAYOUT_ID
    )

46
    data class TopSitePager(val topSites: List<TopSite>) : AdapterItem(TopSitePagerViewHolder.LAYOUT_ID) {
47
        override fun sameAs(other: AdapterItem): Boolean {
48
            val newTopSites = (other as? TopSitePager) ?: return false
49
            return newTopSites.topSites.size == this.topSites.size
50
51
        }

52
        override fun contentsSameAs(other: AdapterItem): Boolean {
53
            val newTopSites = (other as? TopSitePager) ?: return false
54
55
            if (newTopSites.topSites.size != this.topSites.size) return false
            val newSitesSequence = newTopSites.topSites.asSequence()
56
            val oldTopSites = this.topSites.asSequence()
57
            return newSitesSequence.zip(oldTopSites).all { (new, old) -> new == old }
58
59
60
        }
    }

61
    object PrivateBrowsingDescription : AdapterItem(PrivateBrowsingDescriptionViewHolder.LAYOUT_ID)
62
    object NoCollectionsMessage : AdapterItem(NoCollectionsMessageViewHolder.LAYOUT_ID)
63

64
65
    object TorBootstrap : AdapterItem(TorBootstrapPagerViewHolder.LAYOUT_ID)

66
67
68
    object CollectionHeader : AdapterItem(CollectionHeaderViewHolder.LAYOUT_ID)
    data class CollectionItem(
        val collection: TabCollection,
69
        val expanded: Boolean
70
    ) : AdapterItem(CollectionViewHolder.LAYOUT_ID) {
71
72
73
74
75
76
77
78
        override fun sameAs(other: AdapterItem) =
            other is CollectionItem && collection.id == other.collection.id

        override fun contentsSameAs(other: AdapterItem): Boolean {
            (other as? CollectionItem)?.let {
                return it.expanded == this.expanded && it.collection.title == this.collection.title
            } ?: return false
        }
79
    }
80

81
82
83
84
    data class TabInCollectionItem(
        val collection: TabCollection,
        val tab: ComponentTab,
        val isLastTab: Boolean
85
    ) : AdapterItem(TabInCollectionViewHolder.LAYOUT_ID) {
86
87
        override fun sameAs(other: AdapterItem) =
            other is TabInCollectionItem && tab.id == other.tab.id
88
89
90
91
92
93
    }

    object OnboardingHeader : AdapterItem(OnboardingHeaderViewHolder.LAYOUT_ID)
    data class OnboardingSectionHeader(
        val labelBuilder: (Context) -> String
    ) : AdapterItem(OnboardingSectionHeaderViewHolder.LAYOUT_ID) {
94
95
        override fun sameAs(other: AdapterItem) =
            other is OnboardingSectionHeader && labelBuilder == other.labelBuilder
96
    }
97

98
    object OnboardingManualSignIn : AdapterItem(OnboardingManualSignInViewHolder.LAYOUT_ID)
99
    data class OnboardingAutomaticSignIn(
100
        val state: OnboardingState.SignedOutCanAutoSignIn
101
    ) : AdapterItem(OnboardingAutomaticSignInViewHolder.LAYOUT_ID)
102

103
    object OnboardingThemePicker : AdapterItem(OnboardingThemePickerViewHolder.LAYOUT_ID)
104
105
106
    object OnboardingTrackingProtection :
        AdapterItem(OnboardingTrackingProtectionViewHolder.LAYOUT_ID)

107
108
109
    object OnboardingPrivateBrowsing : AdapterItem(OnboardingPrivateBrowsingViewHolder.LAYOUT_ID)
    object OnboardingPrivacyNotice : AdapterItem(OnboardingPrivacyNoticeViewHolder.LAYOUT_ID)
    object OnboardingFinish : AdapterItem(OnboardingFinishViewHolder.LAYOUT_ID)
110
111
112
    object OnboardingToolbarPositionPicker :
        AdapterItem(OnboardingToolbarPositionPickerViewHolder.LAYOUT_ID)

113
    object OnboardingWhatsNew : AdapterItem(OnboardingWhatsNewViewHolder.LAYOUT_ID)
114
115
116
117
118

    /**
     * True if this item represents the same value as other. Used by [AdapterItemDiffCallback].
     */
    open fun sameAs(other: AdapterItem) = this::class == other::class
Sawyer Blatz's avatar
Sawyer Blatz committed
119
120
121
122
123

    /**
     * Returns a payload if there's been a change, or null if not
     */
    open fun getChangePayload(newItem: AdapterItem): Any? = null
124
125

    open fun contentsSameAs(other: AdapterItem) = this::class == other::class
126
127
128
}

class AdapterItemDiffCallback : DiffUtil.ItemCallback<AdapterItem>() {
129
130
    override fun areItemsTheSame(oldItem: AdapterItem, newItem: AdapterItem) =
        oldItem.sameAs(newItem)
131
132

    @Suppress("DiffUtilEquals")
133
134
    override fun areContentsTheSame(oldItem: AdapterItem, newItem: AdapterItem) =
        oldItem.contentsSameAs(newItem)
Sawyer Blatz's avatar
Sawyer Blatz committed
135
136
137
138

    override fun getChangePayload(oldItem: AdapterItem, newItem: AdapterItem): Any? {
        return oldItem.getChangePayload(newItem) ?: return super.getChangePayload(oldItem, newItem)
    }
139
140
141
}

class SessionControlAdapter(
142
    private val interactor: SessionControlInteractor,
143
144
    private val viewLifecycleOwner: LifecycleOwner,
    private val components: Components
145
) : ListAdapter<AdapterItem, RecyclerView.ViewHolder>(AdapterItemDiffCallback()) {
146
147
148
149
150
151

    // This method triggers the ComplexMethod lint error when in fact it's quite simple.
    @SuppressWarnings("ComplexMethod")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
        return when (viewType) {
152
            ButtonTipViewHolder.LAYOUT_ID -> ButtonTipViewHolder(view, interactor)
153
            TopSitePagerViewHolder.LAYOUT_ID -> TopSitePagerViewHolder(view, interactor)
154
155
156
157
            PrivateBrowsingDescriptionViewHolder.LAYOUT_ID -> PrivateBrowsingDescriptionViewHolder(
                view,
                interactor
            )
158
159
160
161
162
            TorBootstrapPagerViewHolder.LAYOUT_ID -> TorBootstrapPagerViewHolder(
                view,
                components,
                interactor
            )
163
            NoCollectionsMessageViewHolder.LAYOUT_ID ->
164
165
166
167
168
169
                NoCollectionsMessageViewHolder(
                    view,
                    viewLifecycleOwner,
                    components.core.store,
                    interactor
                )
170
            CollectionHeaderViewHolder.LAYOUT_ID -> CollectionHeaderViewHolder(view)
171
            CollectionViewHolder.LAYOUT_ID -> CollectionViewHolder(view, interactor)
172
173
174
175
176
            TabInCollectionViewHolder.LAYOUT_ID -> TabInCollectionViewHolder(
                view,
                interactor,
                differentLastItem = true
            )
177
            OnboardingHeaderViewHolder.LAYOUT_ID -> OnboardingHeaderViewHolder(view)
178
            OnboardingSectionHeaderViewHolder.LAYOUT_ID -> OnboardingSectionHeaderViewHolder(view)
179
180
181
            OnboardingAutomaticSignInViewHolder.LAYOUT_ID -> OnboardingAutomaticSignInViewHolder(
                view
            )
182
            OnboardingManualSignInViewHolder.LAYOUT_ID -> OnboardingManualSignInViewHolder(view)
183
            OnboardingThemePickerViewHolder.LAYOUT_ID -> OnboardingThemePickerViewHolder(view)
184
185
186
187
188
189
190
191
192
193
194
            OnboardingTrackingProtectionViewHolder.LAYOUT_ID -> OnboardingTrackingProtectionViewHolder(
                view
            )
            OnboardingPrivateBrowsingViewHolder.LAYOUT_ID -> OnboardingPrivateBrowsingViewHolder(
                view,
                interactor
            )
            OnboardingPrivacyNoticeViewHolder.LAYOUT_ID -> OnboardingPrivacyNoticeViewHolder(
                view,
                interactor
            )
195
            OnboardingFinishViewHolder.LAYOUT_ID -> OnboardingFinishViewHolder(view, interactor)
196
            OnboardingWhatsNewViewHolder.LAYOUT_ID -> OnboardingWhatsNewViewHolder(view, interactor)
197
198
199
            OnboardingToolbarPositionPickerViewHolder.LAYOUT_ID -> OnboardingToolbarPositionPickerViewHolder(
                view
            )
200
201
202
203
            else -> throw IllegalStateException()
        }
    }

204
    override fun getItemViewType(position: Int) = getItem(position).viewType
205

206
    @SuppressWarnings("ComplexMethod")
207
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
208
        val item = getItem(position)
209
        when (holder) {
210
211
212
213
            is ButtonTipViewHolder -> {
                val tipItem = item as AdapterItem.TipItem
                holder.bind(tipItem.tip)
            }
214
215
            is TopSitePagerViewHolder -> {
                holder.bind((item as AdapterItem.TopSitePager).topSites)
216
            }
217
            is CollectionViewHolder -> {
218
219
                val (collection, expanded) = item as AdapterItem.CollectionItem
                holder.bindSession(collection, expanded)
220
            }
221
            is TabInCollectionViewHolder -> {
222
223
                val (collection, tab, isLastTab) = item as AdapterItem.TabInCollectionItem
                holder.bindSession(collection, tab, isLastTab)
224
            }
225
            is OnboardingSectionHeaderViewHolder -> holder.bind(
226
                (item as AdapterItem.OnboardingSectionHeader).labelBuilder
227
            )
228
            is OnboardingManualSignInViewHolder -> holder.bind()
229
230
            is OnboardingAutomaticSignInViewHolder -> holder.bind(
                (item as AdapterItem.OnboardingAutomaticSignIn).state.withAccount
231
            )
232
233
234
        }
    }
}