For #6758 - Part 5: Add top sites to the HomeFragmentStore
parent
e1863dd3c2
commit
f68f89f2bf
|
@ -62,6 +62,7 @@ import mozilla.components.feature.media.state.MediaState
|
|||
import mozilla.components.feature.media.state.MediaStateMachine
|
||||
import mozilla.components.feature.tab.collections.TabCollection
|
||||
import mozilla.components.support.ktx.android.util.dpToPx
|
||||
import mozilla.components.feature.top.sites.TopSite
|
||||
import org.mozilla.fenix.BrowserDirection
|
||||
import org.mozilla.fenix.HomeActivity
|
||||
import org.mozilla.fenix.R
|
||||
|
@ -174,7 +175,8 @@ class HomeFragment : Fragment() {
|
|||
collections = requireComponents.core.tabCollectionStorage.cachedTabCollections,
|
||||
expandedCollections = emptySet(),
|
||||
mode = currentMode.getCurrentMode(),
|
||||
tabs = emptyList()
|
||||
tabs = emptyList(),
|
||||
topSites = requireComponents.core.topSiteStorage.cachedTopSites
|
||||
)
|
||||
)
|
||||
}
|
||||
|
@ -311,6 +313,7 @@ class HomeFragment : Fragment() {
|
|||
override fun onStart() {
|
||||
super.onStart()
|
||||
subscribeToTabCollections()
|
||||
subscribeToTopSites()
|
||||
|
||||
val context = requireContext()
|
||||
val components = context.components
|
||||
|
@ -318,7 +321,8 @@ class HomeFragment : Fragment() {
|
|||
homeFragmentStore.dispatch(HomeFragmentAction.Change(
|
||||
collections = components.core.tabCollectionStorage.cachedTabCollections,
|
||||
mode = currentMode.getCurrentMode(),
|
||||
tabs = getListOfSessions().toTabs()
|
||||
tabs = getListOfSessions().toTabs(),
|
||||
topSites = components.core.topSiteStorage.cachedTopSites
|
||||
))
|
||||
|
||||
requireComponents.backgroundServices.accountManager.register(currentMode, owner = this)
|
||||
|
@ -573,6 +577,15 @@ class HomeFragment : Fragment() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun subscribeToTopSites(): Observer<List<TopSite>> {
|
||||
return Observer<List<TopSite>> { topSites ->
|
||||
requireComponents.core.topSiteStorage.cachedTopSites = topSites
|
||||
homeFragmentStore.dispatch(HomeFragmentAction.TopSitesChange(topSites))
|
||||
}.also { observer ->
|
||||
requireComponents.core.topSiteStorage.getTopSites().observe(this, observer)
|
||||
}
|
||||
}
|
||||
|
||||
private fun removeAllTabsWithUndo(listOfSessionsToDelete: Sequence<Session>, private: Boolean) {
|
||||
homeFragmentStore.dispatch(HomeFragmentAction.TabsChange(emptyList()))
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import mozilla.components.browser.session.Session
|
|||
import mozilla.components.browser.session.SessionManager
|
||||
import mozilla.components.feature.media.state.MediaState
|
||||
import mozilla.components.feature.tab.collections.TabCollection
|
||||
import mozilla.components.feature.top.sites.TopSite
|
||||
import mozilla.components.lib.state.Action
|
||||
import mozilla.components.lib.state.State
|
||||
import mozilla.components.lib.state.Store
|
||||
|
@ -44,21 +45,32 @@ fun List<Tab>.toSessionBundle(sessionManager: SessionManager): List<Session> {
|
|||
* in the [HomeFragment].
|
||||
* @property mode The state of the [HomeFragment] UI.
|
||||
* @property tabs The list of opened [Tab] in the [HomeFragment].
|
||||
* @property topSites The list of [TopSite] in the [HomeFragment].
|
||||
*/
|
||||
data class HomeFragmentState(
|
||||
val collections: List<TabCollection>,
|
||||
val expandedCollections: Set<Long>,
|
||||
val mode: Mode,
|
||||
val tabs: List<Tab>
|
||||
val tabs: List<Tab>,
|
||||
val topSites: List<TopSite>
|
||||
) : State
|
||||
|
||||
sealed class HomeFragmentAction : Action {
|
||||
data class Change(val tabs: List<Tab>, val mode: Mode, val collections: List<TabCollection>) :
|
||||
data class Change(
|
||||
val tabs: List<Tab>,
|
||||
val topSites: List<TopSite>,
|
||||
val mode: Mode,
|
||||
val collections: List<TabCollection>
|
||||
) :
|
||||
HomeFragmentAction()
|
||||
data class CollectionExpanded(val collection: TabCollection, val expand: Boolean) : HomeFragmentAction()
|
||||
|
||||
data class CollectionExpanded(val collection: TabCollection, val expand: Boolean) :
|
||||
HomeFragmentAction()
|
||||
|
||||
data class CollectionsChange(val collections: List<TabCollection>) : HomeFragmentAction()
|
||||
data class ModeChange(val mode: Mode, val tabs: List<Tab> = emptyList()) : HomeFragmentAction()
|
||||
data class TabsChange(val tabs: List<Tab>) : HomeFragmentAction()
|
||||
data class TopSitesChange(val topSites: List<TopSite>) : HomeFragmentAction()
|
||||
}
|
||||
|
||||
private fun homeFragmentStateReducer(
|
||||
|
@ -69,7 +81,8 @@ private fun homeFragmentStateReducer(
|
|||
is HomeFragmentAction.Change -> state.copy(
|
||||
collections = action.collections,
|
||||
mode = action.mode,
|
||||
tabs = action.tabs
|
||||
tabs = action.tabs,
|
||||
topSites = action.topSites
|
||||
)
|
||||
is HomeFragmentAction.CollectionExpanded -> {
|
||||
val newExpandedCollection = state.expandedCollections.toMutableSet()
|
||||
|
@ -85,5 +98,6 @@ private fun homeFragmentStateReducer(
|
|||
is HomeFragmentAction.CollectionsChange -> state.copy(collections = action.collections)
|
||||
is HomeFragmentAction.ModeChange -> state.copy(mode = action.mode, tabs = action.tabs)
|
||||
is HomeFragmentAction.TabsChange -> state.copy(tabs = action.tabs)
|
||||
is HomeFragmentAction.TopSitesChange -> state.copy(topSites = action.topSites)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import io.mockk.every
|
|||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mozilla.components.feature.tab.collections.TabCollection
|
||||
import mozilla.components.feature.top.sites.TopSite
|
||||
import mozilla.components.service.fxa.manager.FxaAccountManager
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
|
@ -22,7 +23,6 @@ import org.mozilla.fenix.ext.components
|
|||
import org.mozilla.fenix.onboarding.FenixOnboarding
|
||||
|
||||
class HomeFragmentStoreTest {
|
||||
|
||||
private lateinit var context: Context
|
||||
private lateinit var accountManager: FxaAccountManager
|
||||
private lateinit var onboarding: FenixOnboarding
|
||||
|
@ -55,7 +55,8 @@ class HomeFragmentStoreTest {
|
|||
collections = emptyList(),
|
||||
expandedCollections = emptySet(),
|
||||
mode = currentMode.getCurrentMode(),
|
||||
tabs = emptyList()
|
||||
tabs = emptyList(),
|
||||
topSites = emptyList()
|
||||
)
|
||||
|
||||
homeFragmentStore = HomeFragmentStore(homeFragmentState)
|
||||
|
@ -110,6 +111,17 @@ class HomeFragmentStoreTest {
|
|||
assertThat(homeFragmentStore.state.collections).isEqualTo(tabCollections)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Test changing the top sites in HomeFragmentStore`() = runBlocking {
|
||||
assertEquals(0, homeFragmentStore.state.topSites.size)
|
||||
|
||||
// Add 2 TopSites to the HomeFragmentStore.
|
||||
val topSites: List<TopSite> = listOf(mockk(), mockk())
|
||||
homeFragmentStore.dispatch(HomeFragmentAction.TopSitesChange(topSites)).join()
|
||||
|
||||
assertThat(homeFragmentStore.state.topSites).isEqualTo(topSites)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Test changing the tab in HomeFragmentStore`() = runBlocking {
|
||||
assertEquals(0, homeFragmentStore.state.tabs.size)
|
||||
|
@ -137,25 +149,29 @@ class HomeFragmentStoreTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `Test changing the collections, mode and tabs in the HomeFragmentStore`() = runBlocking {
|
||||
fun `Test changing the collections, mode, tabs and top sites in the HomeFragmentStore`() = runBlocking {
|
||||
// Verify that the default state of the HomeFragment is correct.
|
||||
assertEquals(0, homeFragmentStore.state.collections.size)
|
||||
assertEquals(0, homeFragmentStore.state.tabs.size)
|
||||
assertEquals(0, homeFragmentStore.state.topSites.size)
|
||||
assertThat(homeFragmentStore.state.mode).isEqualTo(Mode.Normal)
|
||||
|
||||
val collections: List<TabCollection> = listOf(mockk())
|
||||
val tabs: List<Tab> = listOf(mockk(), mockk())
|
||||
val topSites: List<TopSite> = listOf(mockk(), mockk())
|
||||
|
||||
homeFragmentStore.dispatch(
|
||||
HomeFragmentAction.Change(
|
||||
collections = collections,
|
||||
mode = Mode.Private,
|
||||
tabs = tabs
|
||||
tabs = tabs,
|
||||
topSites = topSites
|
||||
)
|
||||
).join()
|
||||
|
||||
assertEquals(1, homeFragmentStore.state.collections.size)
|
||||
assertThat(homeFragmentStore.state.mode).isEqualTo(Mode.Private)
|
||||
assertEquals(2, homeFragmentStore.state.tabs.size)
|
||||
assertEquals(2, homeFragmentStore.state.topSites.size)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue