diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 9d46a6f6e..a60ce6f6c 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -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> { + return Observer> { 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, private: Boolean) { homeFragmentStore.dispatch(HomeFragmentAction.TabsChange(emptyList())) diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragmentStore.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragmentStore.kt index 44536919a..93b5408af 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragmentStore.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragmentStore.kt @@ -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.toSessionBundle(sessionManager: SessionManager): List { * 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, val expandedCollections: Set, val mode: Mode, - val tabs: List + val tabs: List, + val topSites: List ) : State sealed class HomeFragmentAction : Action { - data class Change(val tabs: List, val mode: Mode, val collections: List) : + data class Change( + val tabs: List, + val topSites: List, + val mode: Mode, + val collections: List + ) : 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) : HomeFragmentAction() data class ModeChange(val mode: Mode, val tabs: List = emptyList()) : HomeFragmentAction() data class TabsChange(val tabs: List) : HomeFragmentAction() + data class TopSitesChange(val topSites: List) : 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) } } diff --git a/app/src/test/java/org/mozilla/fenix/home/HomeFragmentStoreTest.kt b/app/src/test/java/org/mozilla/fenix/home/HomeFragmentStoreTest.kt index aa7dd48b5..68e706321 100644 --- a/app/src/test/java/org/mozilla/fenix/home/HomeFragmentStoreTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/HomeFragmentStoreTest.kt @@ -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 = 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 = listOf(mockk()) val tabs: List = listOf(mockk(), mockk()) + val topSites: List = 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) } }