2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-17 15:26:23 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/23456 ⁃ Use lifecycleScope in TopSiteItemViewHolder

This commit is contained in:
Gabriel Luong 2022-02-15 17:19:34 -05:00 committed by mergify[bot]
parent bbb1009866
commit 4c40cd09be
10 changed files with 34 additions and 16 deletions

View File

@ -259,7 +259,7 @@ class SessionControlAdapter(
return when (viewType) {
TopPlaceholderViewHolder.LAYOUT_ID -> TopPlaceholderViewHolder(view)
ButtonTipViewHolder.LAYOUT_ID -> ButtonTipViewHolder(view, interactor)
TopSitePagerViewHolder.LAYOUT_ID -> TopSitePagerViewHolder(view, interactor)
TopSitePagerViewHolder.LAYOUT_ID -> TopSitePagerViewHolder(view, viewLifecycleOwner, interactor)
PrivateBrowsingDescriptionViewHolder.LAYOUT_ID -> PrivateBrowsingDescriptionViewHolder(
view,
interactor

View File

@ -10,7 +10,8 @@ import android.view.View
import android.widget.PopupWindow
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.core.view.isVisible
import kotlinx.coroutines.CoroutineScope
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch
@ -28,6 +29,7 @@ import org.mozilla.fenix.utils.view.ViewHolder
class TopSiteItemViewHolder(
view: View,
private val viewLifecycleOwner: LifecycleOwner,
private val interactor: TopSiteInteractor
) : ViewHolder(view) {
private lateinit var topSite: TopSite
@ -83,7 +85,7 @@ class TopSiteItemViewHolder(
if (topSite is TopSite.Provided) {
binding.topSiteSubtitle.isVisible = true
CoroutineScope(IO).launch {
viewLifecycleOwner.lifecycleScope.launch(IO) {
itemView.context.components.core.client.bitmapForUrl(topSite.imageUrl)?.let { bitmap ->
withContext(Main) {
binding.faviconImage.setImageBitmap(bitmap)

View File

@ -6,6 +6,7 @@ package org.mozilla.fenix.home.topsites
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import mozilla.components.feature.top.sites.TopSite
@ -18,11 +19,12 @@ import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
class TopSitePagerViewHolder(
view: View,
viewLifecycleOwner: LifecycleOwner,
interactor: TopSiteInteractor
) : RecyclerView.ViewHolder(view) {
private val binding = ComponentTopSitesPagerBinding.bind(view)
private val topSitesPagerAdapter = TopSitesPagerAdapter(interactor)
private val topSitesPagerAdapter = TopSitesPagerAdapter(viewLifecycleOwner, interactor)
private val pageIndicator = binding.pageIndicator
private var currentPage = 0

View File

@ -5,6 +5,7 @@
package org.mozilla.fenix.home.topsites
import android.view.View
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.R
@ -14,10 +15,11 @@ import org.mozilla.fenix.utils.AccessibilityGridLayoutManager
class TopSiteViewHolder(
view: View,
viewLifecycleOwner: LifecycleOwner,
interactor: TopSiteInteractor
) : RecyclerView.ViewHolder(view) {
private val topSitesAdapter = TopSitesAdapter(interactor)
private val topSitesAdapter = TopSitesAdapter(viewLifecycleOwner, interactor)
val binding = ComponentTopSitesBinding.bind(view)
init {

View File

@ -6,6 +6,7 @@ package org.mozilla.fenix.home.topsites
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import mozilla.components.feature.top.sites.TopSite
@ -13,12 +14,13 @@ import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
import org.mozilla.fenix.perf.StartupTimeline
class TopSitesAdapter(
private val viewLifecycleOwner: LifecycleOwner,
private val interactor: TopSiteInteractor
) : ListAdapter<TopSite, TopSiteItemViewHolder>(TopSitesDiffCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopSiteItemViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(TopSiteItemViewHolder.LAYOUT_ID, parent, false)
return TopSiteItemViewHolder(view, interactor)
return TopSiteItemViewHolder(view, viewLifecycleOwner, interactor)
}
override fun onBindViewHolder(holder: TopSiteItemViewHolder, position: Int) {

View File

@ -7,6 +7,7 @@ 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
@ -15,13 +16,14 @@ import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
import org.mozilla.fenix.home.topsites.TopSitePagerViewHolder.Companion.TOP_SITES_PER_PAGE
class TopSitesPagerAdapter(
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, interactor)
return TopSiteViewHolder(view, viewLifecycleOwner, interactor)
}
override fun onBindViewHolder(

View File

@ -5,6 +5,7 @@
package org.mozilla.fenix.home.topsites
import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@ -26,6 +27,8 @@ class TopSiteItemViewHolderTest {
private lateinit var binding: TopSiteItemBinding
private lateinit var interactor: TopSiteInteractor
private lateinit var lifecycleOwner: LifecycleOwner
private val pocket = TopSite.Default(
id = 1L,
title = "Pocket",
@ -37,12 +40,14 @@ class TopSiteItemViewHolderTest {
fun setup() {
binding = TopSiteItemBinding.inflate(LayoutInflater.from(testContext))
interactor = mockk(relaxed = true)
lifecycleOwner = mockk(relaxed = true)
every { testContext.components.core.icons } returns BrowserIcons(testContext, mockk(relaxed = true))
}
@Test
fun `calls interactor on click`() {
TopSiteItemViewHolder(binding.root, interactor).bind(pocket)
TopSiteItemViewHolder(binding.root, lifecycleOwner, interactor).bind(pocket)
binding.topSiteItem.performClick()
verify { interactor.onSelectTopSite(pocket) }
@ -51,7 +56,7 @@ class TopSiteItemViewHolderTest {
@Test
fun `calls interactor on long click`() {
every { testContext.components.analytics } returns mockk(relaxed = true)
TopSiteItemViewHolder(binding.root, interactor).bind(pocket)
TopSiteItemViewHolder(binding.root, lifecycleOwner, interactor).bind(pocket)
binding.topSiteItem.performLongClick()
verify { interactor.onTopSiteMenuOpened() }
@ -66,7 +71,7 @@ class TopSiteItemViewHolderTest {
createdAt = 0
)
TopSiteItemViewHolder(binding.root, interactor).bind(defaultTopSite)
TopSiteItemViewHolder(binding.root, lifecycleOwner, interactor).bind(defaultTopSite)
val pinIndicator = binding.topSiteTitle.compoundDrawables[0]
assertNotNull(pinIndicator)
@ -81,7 +86,7 @@ class TopSiteItemViewHolderTest {
createdAt = 0
)
TopSiteItemViewHolder(binding.root, interactor).bind(pinnedTopSite)
TopSiteItemViewHolder(binding.root, lifecycleOwner, interactor).bind(pinnedTopSite)
val pinIndicator = binding.topSiteTitle.compoundDrawables[0]
assertNotNull(pinIndicator)
@ -96,7 +101,7 @@ class TopSiteItemViewHolderTest {
createdAt = 0
)
TopSiteItemViewHolder(binding.root, interactor).bind(frecentTopSite)
TopSiteItemViewHolder(binding.root, lifecycleOwner, interactor).bind(frecentTopSite)
val pinIndicator = binding.topSiteTitle.compoundDrawables[0]
assertNull(pinIndicator)

View File

@ -5,6 +5,7 @@
package org.mozilla.fenix.home.topsites
import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import io.mockk.mockk
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.support.test.robolectric.testContext
@ -20,17 +21,19 @@ import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
class TopSiteViewHolderTest {
private lateinit var binding: ComponentTopSitesBinding
private lateinit var lifecycleOwner: LifecycleOwner
private lateinit var interactor: TopSiteInteractor
@Before
fun setup() {
binding = ComponentTopSitesBinding.inflate(LayoutInflater.from(testContext))
interactor = mockk()
interactor = mockk(relaxed = true)
lifecycleOwner = mockk(relaxed = true)
}
@Test
fun `binds list of top sites`() {
TopSiteViewHolder(binding.root, interactor).bind(
TopSiteViewHolder(binding.root, lifecycleOwner, interactor).bind(
listOf(
TopSite.Default(
id = 1L,

View File

@ -49,7 +49,7 @@ class TopSitesPagerAdapterTest {
@Before
fun setup() {
topSitesPagerAdapter = spyk(TopSitesPagerAdapter(mockk()))
topSitesPagerAdapter = spyk(TopSitesPagerAdapter(mockk(), mockk()))
}
@Test

View File

@ -47,7 +47,7 @@ class StartupReportFullyDrawnTest {
holderItemView = spyk(binding.root)
every { activity.findViewById<LinearLayout>(R.id.rootContainer) } returns rootContainer
every { holderItemView.context } returns activity
holder = TopSiteItemViewHolder(holderItemView, mockk())
holder = TopSiteItemViewHolder(holderItemView, mockk(), mockk())
every { rootContainer.viewTreeObserver } returns viewTreeObserver
every { holderItemView.viewTreeObserver } returns viewTreeObserver