For #19131 - Fixes top sites refresh and interactability

upstream-sync
codrut.topliceanu 3 years ago committed by mergify[bot]
parent 4a0cd8a25a
commit a7f3d34538

@ -53,9 +53,9 @@ class TopSitePagerViewHolder(
} }
fun update(payload: AdapterItem.TopSitePagerPayload) { fun update(payload: AdapterItem.TopSitePagerPayload) {
for (item in payload.changed) { // Due to offscreenPageLimit = 1 we need to update both pages manually here
topSitesPagerAdapter.notifyItemChanged(currentPage, payload) topSitesPagerAdapter.notifyItemChanged(0, payload)
} topSitesPagerAdapter.notifyItemChanged(1, payload)
} }
fun bind(topSites: List<TopSite>) { fun bind(topSites: List<TopSite>) {

@ -11,7 +11,6 @@ import android.view.View
import android.widget.PopupWindow import android.widget.PopupWindow
import androidx.appcompat.content.res.AppCompatResources.getDrawable import androidx.appcompat.content.res.AppCompatResources.getDrawable
import kotlinx.android.synthetic.main.top_site_item.* import kotlinx.android.synthetic.main.top_site_item.*
import kotlinx.android.synthetic.main.top_site_item.view.*
import mozilla.components.browser.menu.BrowserMenuBuilder import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.TopSite
@ -93,11 +92,6 @@ class TopSiteItemViewHolder(
this.topSite = topSite this.topSite = topSite
} }
fun bind(topSitePayload: TopSitesAdapter.TopSitePayload) {
itemView.top_site_title.text = topSitePayload.newTitle
topSite = topSite.copy(title = topSitePayload.newTitle)
}
private fun onTouchEvent( private fun onTouchEvent(
v: View, v: View,
event: MotionEvent, event: MotionEvent,

@ -8,7 +8,6 @@ import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.ListAdapter
import kotlinx.android.synthetic.main.top_site_item.view.*
import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
import org.mozilla.fenix.perf.StartupTimeline import org.mozilla.fenix.perf.StartupTimeline
@ -39,17 +38,10 @@ class TopSitesAdapter(
is TopSite -> { is TopSite -> {
holder.bind((payloads[0] as TopSite)) holder.bind((payloads[0] as TopSite))
} }
is TopSitePayload -> {
holder.bind(payloads[0] as TopSitePayload)
}
} }
} }
} }
data class TopSitePayload(
val newTitle: String?
)
internal object TopSitesDiffCallback : DiffUtil.ItemCallback<TopSite>() { internal object TopSitesDiffCallback : DiffUtil.ItemCallback<TopSite>() {
override fun areItemsTheSame(oldItem: TopSite, newItem: TopSite) = oldItem.id == newItem.id override fun areItemsTheSame(oldItem: TopSite, newItem: TopSite) = oldItem.id == newItem.id
@ -58,7 +50,7 @@ class TopSitesAdapter(
override fun getChangePayload(oldItem: TopSite, newItem: TopSite): Any? { override fun getChangePayload(oldItem: TopSite, newItem: TopSite): Any? {
return if (oldItem.id == newItem.id && oldItem.url == newItem.url && oldItem.title != newItem.title) { return if (oldItem.id == newItem.id && oldItem.url == newItem.url && oldItem.title != newItem.title) {
TopSitePayload(newItem.title) newItem
} else null } else null
} }
} }

@ -10,7 +10,7 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.ListAdapter
import kotlinx.android.synthetic.main.component_top_sites.view.* import kotlinx.android.synthetic.main.component_top_sites.view.*
import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.home.sessioncontrol.AdapterItem import org.mozilla.fenix.home.sessioncontrol.AdapterItem.TopSitePagerPayload
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
import org.mozilla.fenix.home.sessioncontrol.viewholders.TopSitePagerViewHolder.Companion.TOP_SITES_PER_PAGE import org.mozilla.fenix.home.sessioncontrol.viewholders.TopSitePagerViewHolder.Companion.TOP_SITES_PER_PAGE
import org.mozilla.fenix.home.sessioncontrol.viewholders.TopSiteViewHolder import org.mozilla.fenix.home.sessioncontrol.viewholders.TopSiteViewHolder
@ -33,15 +33,26 @@ class TopSitesPagerAdapter(
if (payloads.isNullOrEmpty()) { if (payloads.isNullOrEmpty()) {
onBindViewHolder(holder, position) onBindViewHolder(holder, position)
} else { } else {
if (payloads[0] is AdapterItem.TopSitePagerPayload) { if (payloads[0] is TopSitePagerPayload) {
val adapter = holder.itemView.top_sites_list.adapter as TopSitesAdapter val adapter = holder.itemView.top_sites_list.adapter as TopSitesAdapter
val payload = payloads[0] as AdapterItem.TopSitePagerPayload val payload = payloads[0] as TopSitePagerPayload
for (item in payload.changed) {
adapter.notifyItemChanged( update(payload, position, adapter)
item.first % TOP_SITES_PER_PAGE, }
TopSitesAdapter.TopSitePayload(item.second.title) }
) }
}
private fun update(
payload: TopSitePagerPayload,
position: Int,
adapter: TopSitesAdapter
) {
// Only currently selected page items need to be updated.
for (item in payload.changed) {
if (item.first < TOP_SITES_PER_PAGE && position == 0) {
adapter.notifyItemChanged(item.first, item.second)
} else if (item.first >= TOP_SITES_PER_PAGE && position == 1) {
adapter.notifyItemChanged(item.first - TOP_SITES_PER_PAGE, item.second)
} }
} }
} }
@ -67,7 +78,7 @@ class TopSitesPagerAdapter(
changed.add(Pair(index, item)) changed.add(Pair(index, item))
} }
} }
return if (changed.isNotEmpty()) AdapterItem.TopSitePagerPayload(changed) else null return if (changed.isNotEmpty()) TopSitePagerPayload(changed) else null
} }
} }
} }

@ -29,7 +29,7 @@ class TopSitesAdapterTest {
assertEquals( assertEquals(
TopSitesAdapter.TopSitesDiffCallback.getChangePayload(topSite, topSite2), TopSitesAdapter.TopSitesDiffCallback.getChangePayload(topSite, topSite2),
TopSitesAdapter.TopSitePayload("Title2") topSite.copy(title = "Title2")
) )
val topSite3 = TopSite( val topSite3 = TopSite(

Loading…
Cancel
Save