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

Bug 1822634 - Refactor tab counter code from HomeFragment into TabCounterView

This commit is contained in:
Gabriel Luong 2023-03-10 15:51:07 -05:00 committed by mergify[bot]
parent 87725f1ccb
commit d336eff594
3 changed files with 60 additions and 8 deletions

View File

@ -187,7 +187,9 @@ class HomeFragment : Fragment() {
get() = _sessionControlInteractor!!
private var sessionControlView: SessionControlView? = null
private var tabCounterView: TabCounterView? = null
private var appBarLayout: AppBarLayout? = null
private lateinit var currentMode: CurrentMode
private var lastAppliedWallpaperName: String = Wallpaper.defaultName
@ -582,7 +584,7 @@ class HomeFragment : Fragment() {
hideOnboardingIfNeeded = ::hideOnboardingIfNeeded,
).build()
TabCounterView(
tabCounterView = TabCounterView(
context = requireContext(),
browsingModeManager = browsingModeManager,
navController = findNavController(),
@ -613,7 +615,8 @@ class HomeFragment : Fragment() {
}
consumeFrom(requireComponents.core.store) {
updateTabCounter(it)
tabCounterView?.update(it)
showCollectionsPlaceholder(it)
}
homeViewModel.sessionToDelete?.also {
@ -626,7 +629,7 @@ class HomeFragment : Fragment() {
homeViewModel.sessionToDelete = null
updateTabCounter(requireComponents.core.store.state)
tabCounterView?.update(requireComponents.core.store.state)
if (bundleArgs.getBoolean(FOCUS_ON_ADDRESS_BAR)) {
sessionControlInteractor.onNavigateSearch()
@ -791,8 +794,10 @@ class HomeFragment : Fragment() {
_sessionControlInteractor = null
sessionControlView = null
tabCounterView = null
appBarLayout = null
_binding = null
bundleArgs.clear()
lastAppliedWallpaperName = Wallpaper.defaultName
}
@ -1010,16 +1015,13 @@ class HomeFragment : Fragment() {
)
}
// TODO use [FenixTabCounterToolbarButton] instead of [TabCounter]:
// https://github.com/mozilla-mobile/fenix/issues/16792
private fun updateTabCounter(browserState: BrowserState) {
private fun showCollectionsPlaceholder(browserState: BrowserState) {
val tabCount = if (browsingModeManager.mode.isPrivate) {
browserState.privateTabs.size
} else {
browserState.normalTabs.size
}
binding.tabButton.setCountWithAnimation(tabCount)
// The add_tabs_to_collections_button is added at runtime. We need to search for it in the same way.
sessionControlView?.view?.findViewById<MaterialButton>(R.id.add_tabs_to_collections_button)
?.isVisible = tabCount > 0

View File

@ -7,6 +7,9 @@ package org.mozilla.fenix.home
import android.content.Context
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import mozilla.components.browser.state.selector.normalTabs
import mozilla.components.browser.state.selector.privateTabs
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.ui.tabcounter.TabCounter
import mozilla.components.ui.tabcounter.TabCounterMenu
import mozilla.telemetry.glean.private.NoExtras
@ -65,6 +68,22 @@ class TabCounterView(
}
}
/**
* Updates the tab counter count based on the current browser state.
*
* @param browserState [BrowserState] used to get the current tab count for the current
* browsing mode.
*/
fun update(browserState: BrowserState) {
val tabCount = if (browsingModeManager.mode.isPrivate) {
browserState.privateTabs.size
} else {
browserState.normalTabs.size
}
tabCounter.setCountWithAnimation(tabCount)
}
/**
* Callback invoked when a menu item is tapped on.
*/

View File

@ -6,7 +6,12 @@ package org.mozilla.fenix.home
import androidx.navigation.NavController
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import mozilla.components.browser.state.selector.normalTabs
import mozilla.components.browser.state.selector.privateTabs
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.createTab
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.ui.tabcounter.TabCounter
import mozilla.components.ui.tabcounter.TabCounterMenu
@ -46,7 +51,7 @@ class TabCounterViewTest {
settings = mockk(relaxed = true)
modeDidChange = mockk(relaxed = true)
tabCounter = TabCounter(testContext)
tabCounter = spyk(TabCounter(testContext))
browsingModeManager = DefaultBrowsingModeManager(
_mode = BrowsingMode.Normal,
@ -91,4 +96,30 @@ class TabCounterViewTest {
assertEquals(BrowsingMode.Private, browsingModeManager.mode)
}
@Test
fun `WHEN tab counter is updated THEN set the tab counter to the correct number of tabs`() {
val browserState = BrowserState(
tabs = listOf(
createTab(url = "https://www.mozilla.org", id = "mozilla"),
createTab(url = "https://www.firefox.com", id = "firefox"),
createTab(url = "https://getpocket.com", private = true, id = "getpocket"),
),
selectedTabId = "mozilla",
)
tabCounterView.update(browserState)
verify {
tabCounter.setCountWithAnimation(browserState.normalTabs.size)
}
browsingModeManager.mode = BrowsingMode.Private
tabCounterView.update(browserState)
verify {
tabCounter.setCountWithAnimation(browserState.privateTabs.size)
}
}
}