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

[fenix] No issue - Make TabLayoutMediator lifecycle aware (https://github.com/mozilla-mobile/fenix/pull/18779)

This commit is contained in:
Jonathan Almeida 2021-04-05 23:10:29 +04:00 committed by GitHub
parent 5b26effc9a
commit 5beadfdb15
3 changed files with 36 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import androidx.annotation.VisibleForTesting
import com.google.android.material.tabs.TabLayout
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.support.base.feature.LifecycleAwareFeature
import org.mozilla.fenix.tabstray.TrayPagerAdapter.Companion.POSITION_NORMAL_TABS
import org.mozilla.fenix.tabstray.TrayPagerAdapter.Companion.POSITION_PRIVATE_TABS
@ -19,17 +20,23 @@ class TabLayoutMediator(
private val tabLayout: TabLayout,
private val interactor: TabsTrayInteractor,
private val store: BrowserStore
) {
) : LifecycleAwareFeature {
private val observer = TabLayoutObserver(interactor)
/**
* Start observing the [TabLayout] and select the current tab for initial state.
*/
fun attach() {
tabLayout.addOnTabSelectedListener(TabLayoutObserver(interactor))
override fun start() {
tabLayout.addOnTabSelectedListener(observer)
selectActivePage()
}
override fun stop() {
tabLayout.removeOnTabSelectedListener(observer)
}
@VisibleForTesting
internal fun selectActivePage() {
val selectedTab = store.state.selectedTab ?: return

View File

@ -19,6 +19,7 @@ import org.mozilla.fenix.HomeActivity
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.components.browser.state.selector.normalTabs
import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import mozilla.components.ui.tabcounter.TabCounter
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.requireComponents
@ -32,6 +33,8 @@ class TabsTrayFragment : AppCompatDialogFragment(), TabsTrayInteractor {
lateinit var behavior: BottomSheetBehavior<ConstraintLayout>
private val tabLayoutMediator = ViewBoundFeatureWrapper<TabLayoutMediator>()
private val selectTabUseCase by lazy {
SelectTabUseCaseWrapper(
requireComponents.analytics.metrics,
@ -85,11 +88,14 @@ class TabsTrayFragment : AppCompatDialogFragment(), TabsTrayInteractor {
setupPager(view.context, this, browserTrayInteractor, syncedTabsTrayInteractor)
TabLayoutMediator(
tabLayout = tab_layout,
interactor = this,
store = requireComponents.core.store
).attach()
tabLayoutMediator.set(
feature = TabLayoutMediator(
tabLayout = tab_layout,
interactor = this,
store = requireComponents.core.store
), owner = this,
view = view
)
consumeFrom(requireComponents.core.store) {
view.findViewById<TabCounter>(R.id.tab_counter)?.apply {

View File

@ -49,6 +49,21 @@ class TabLayoutMediatorTest {
verify { tab.select() }
}
@Test
fun `lifecycle methods adds and removes observer`() {
val store = createState("456")
val tabLayout: TabLayout = mockk(relaxed = true)
val mediator = TabLayoutMediator(tabLayout, mockk(relaxed = true), store)
mediator.start()
verify { tabLayout.addOnTabSelectedListener(any()) }
mediator.stop()
verify { tabLayout.removeOnTabSelectedListener(any()) }
}
private fun createState(selectedId: String) = BrowserStore(
initialState = BrowserState(
tabs = listOf(