Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #6758 - Part 5: Add top sites to the HomeFragmentStore
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielluong committed Jan 21, 2020
1 parent 0989af3 commit dae8ebd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
17 changes: 15 additions & 2 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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
Expand Down Expand Up @@ -173,7 +174,8 @@ class HomeFragment : Fragment() {
collections = requireComponents.core.tabCollectionStorage.cachedTabCollections,
expandedCollections = emptySet(),
mode = currentMode.getCurrentMode(),
tabs = emptyList()
tabs = emptyList(),
topSites = requireComponents.core.topSiteStorage.cachedTopSites
)
)
}
Expand Down Expand Up @@ -320,14 +322,16 @@ class HomeFragment : Fragment() {
override fun onStart() {
super.onStart()
subscribeToTabCollections()
subscribeToTopSites()

val context = requireContext()
val components = context.components

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)
Expand Down Expand Up @@ -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()))

Expand Down
22 changes: 18 additions & 4 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragmentStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand All @@ -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)
}
}
24 changes: 20 additions & 4 deletions app/src/test/java/org/mozilla/fenix/home/HomeFragmentStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -55,7 +55,8 @@ class HomeFragmentStoreTest {
collections = emptyList(),
expandedCollections = emptySet(),
mode = currentMode.getCurrentMode(),
tabs = emptyList()
tabs = emptyList(),
topSites = emptyList()
)

homeFragmentStore = HomeFragmentStore(homeFragmentState)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit dae8ebd

Please sign in to comment.