From 269dc65be3b74b53b3363cfc6e60b24d2f5fe8ba Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Sun, 6 Mar 2022 20:48:24 -0500 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/24114 - Refactor initial HomeFragmentState into AppState --- .../mozilla/fenix/components/Components.kt | 41 ++++++++++- .../org/mozilla/fenix/home/HomeFragment.kt | 71 ------------------- 2 files changed, 39 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/Components.kt b/app/src/main/java/org/mozilla/fenix/components/Components.kt index 2810de2930..f5dc3cd5f8 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Components.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Components.kt @@ -27,8 +27,16 @@ import org.mozilla.fenix.R import org.mozilla.fenix.autofill.AutofillConfirmActivity import org.mozilla.fenix.autofill.AutofillSearchActivity import org.mozilla.fenix.autofill.AutofillUnlockActivity +import org.mozilla.fenix.components.appstate.AppState +import org.mozilla.fenix.datastore.pocketStoriesSelectedCategoriesDataStore +import org.mozilla.fenix.ext.asRecentTabs import org.mozilla.fenix.ext.components +import org.mozilla.fenix.ext.filterState import org.mozilla.fenix.ext.settings +import org.mozilla.fenix.ext.sort +import org.mozilla.fenix.home.PocketUpdatesMiddleware +import org.mozilla.fenix.home.blocklist.BlocklistHandler +import org.mozilla.fenix.home.blocklist.BlocklistMiddleware import org.mozilla.fenix.perf.AppStartReasonProvider import org.mozilla.fenix.perf.StartupActivityLog import org.mozilla.fenix.perf.StartupStateProvider @@ -36,8 +44,8 @@ import org.mozilla.fenix.perf.StrictModeManager import org.mozilla.fenix.perf.lazyMonitored import org.mozilla.fenix.utils.ClipboardHandler import org.mozilla.fenix.utils.Settings -import org.mozilla.fenix.wallpapers.WallpaperFileManager import org.mozilla.fenix.wallpapers.WallpaperDownloader +import org.mozilla.fenix.wallpapers.WallpaperFileManager import org.mozilla.fenix.wallpapers.WallpaperManager import org.mozilla.fenix.wifi.WifiConnectionMonitor import java.util.concurrent.TimeUnit @@ -188,7 +196,36 @@ class Components(private val context: Context) { val appStartReasonProvider by lazyMonitored { AppStartReasonProvider() } val startupActivityLog by lazyMonitored { StartupActivityLog() } val startupStateProvider by lazyMonitored { StartupStateProvider(startupActivityLog, appStartReasonProvider) } - val appStore by lazyMonitored { AppStore() } + val appStore by lazyMonitored { + val blocklistHandler = BlocklistHandler(settings) + + AppStore( + initialState = AppState( + collections = core.tabCollectionStorage.cachedTabCollections, + expandedCollections = emptySet(), + topSites = core.topSitesStorage.cachedTopSites.sort(), + recentBookmarks = emptyList(), + showCollectionPlaceholder = settings.showCollectionsPlaceholderOnHome, + showSetAsDefaultBrowserCard = settings.shouldShowSetAsDefaultBrowserCard(), + // Provide an initial state for recent tabs to prevent re-rendering on the home screen. + // This will otherwise cause a visual jump as the section gets rendered from no state + // to some state. + recentTabs = if (settings.showRecentTabsFeature) { + core.store.state.asRecentTabs() + } else { + emptyList() + }, + recentHistory = emptyList() + ).run { filterState(blocklistHandler) }, + middlewares = listOf( + BlocklistMiddleware(blocklistHandler), + PocketUpdatesMiddleware( + core.pocketStoriesService, + context.pocketStoriesSelectedCategoriesDataStore + ) + ) + ) + } } /** 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 3fa8f0bef5..2d016e92a7 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -64,7 +64,6 @@ import mozilla.components.concept.sync.AccountObserver import mozilla.components.concept.sync.AuthType import mozilla.components.concept.sync.OAuthAccount import mozilla.components.feature.tab.collections.TabCollection -import mozilla.components.feature.top.sites.TopSite import mozilla.components.feature.top.sites.TopSitesConfig import mozilla.components.feature.top.sites.TopSitesFeature import mozilla.components.feature.top.sites.TopSitesProviderConfig @@ -82,10 +81,8 @@ import org.mozilla.fenix.R import org.mozilla.fenix.browser.BrowserAnimator.Companion.getToolbarNavOptions import org.mozilla.fenix.browser.BrowserFragmentDirections import org.mozilla.fenix.browser.browsingmode.BrowsingMode -import org.mozilla.fenix.components.Components import org.mozilla.fenix.components.FenixSnackbar import org.mozilla.fenix.components.PrivateShortcutCreateManager -import org.mozilla.fenix.components.StoreProvider import org.mozilla.fenix.components.TabCollectionStorage import org.mozilla.fenix.components.accounts.AccountState import org.mozilla.fenix.components.appstate.AppAction @@ -93,25 +90,18 @@ import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.toolbar.FenixTabCounterMenu import org.mozilla.fenix.components.toolbar.ToolbarPosition import org.mozilla.fenix.databinding.FragmentHomeBinding -import org.mozilla.fenix.datastore.pocketStoriesSelectedCategoriesDataStore -import org.mozilla.fenix.ext.asRecentTabs import org.mozilla.fenix.ext.components -import org.mozilla.fenix.ext.filterState import org.mozilla.fenix.ext.hideToolbar import org.mozilla.fenix.ext.metrics import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.runIfFragmentIsAttached import org.mozilla.fenix.ext.settings -import org.mozilla.fenix.ext.sort -import org.mozilla.fenix.home.blocklist.BlocklistHandler -import org.mozilla.fenix.home.blocklist.BlocklistMiddleware import org.mozilla.fenix.home.mozonline.showPrivacyPopWindow import org.mozilla.fenix.home.pocket.DefaultPocketStoriesController import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesCategory import org.mozilla.fenix.home.recentbookmarks.RecentBookmarksFeature import org.mozilla.fenix.home.recentbookmarks.controller.DefaultRecentBookmarksController -import org.mozilla.fenix.home.recenttabs.RecentTab import org.mozilla.fenix.home.recenttabs.RecentTabsListFeature import org.mozilla.fenix.home.recenttabs.controller.DefaultRecentTabsController import org.mozilla.fenix.home.recentvisits.RecentVisitsFeature @@ -173,7 +163,6 @@ class HomeFragment : Fragment() { } } - private lateinit var homeFragmentStore: HomeFragmentStore private var _sessionControlInteractor: SessionControlInteractor? = null private val sessionControlInteractor: SessionControlInteractor get() = _sessionControlInteractor!! @@ -236,34 +225,6 @@ class HomeFragment : Fragment() { ::dispatchModeChanges ) - val blocklistHandler = BlocklistHandler(components.settings) - homeFragmentStore = StoreProvider.get(this) { - HomeFragmentStore( - initialState = HomeFragmentState( - collections = components.core.tabCollectionStorage.cachedTabCollections, - expandedCollections = emptySet(), - mode = currentMode.getCurrentMode(), - topSites = getTopSites(components), - recentBookmarks = emptyList(), - showCollectionPlaceholder = components.settings.showCollectionsPlaceholderOnHome, - showSetAsDefaultBrowserCard = components.settings.shouldShowSetAsDefaultBrowserCard(), - // Provide an initial state for recent tabs to prevent re-rendering on the home screen. - // This will otherwise cause a visual jump as the section gets rendered from no state - // to some state. - recentTabs = getRecentTabs(components), - recentHistory = emptyList() - ).run { filterState(blocklistHandler) }, - middlewares = listOf( - BlocklistMiddleware(blocklistHandler), - PocketUpdatesMiddleware( - lifecycleScope, - requireComponents.core.pocketStoriesService, - requireContext().pocketStoriesSelectedCategoriesDataStore - ) - ) - ) - } - lifecycleScope.launch(IO) { if (requireContext().settings().showPocketRecommendationsFeature) { val categories = components.core.pocketStoriesService.getStories() @@ -684,22 +645,6 @@ class HomeFragment : Fragment() { subscribeToTabCollections() val context = requireContext() - val components = context.components - - homeFragmentStore.dispatch( - HomeFragmentAction.Change( - collections = components.core.tabCollectionStorage.cachedTabCollections, - mode = currentMode.getCurrentMode(), - topSites = getTopSites(components), - showCollectionPlaceholder = components.settings.showCollectionsPlaceholderOnHome, - // Provide an initial state for recent tabs to prevent re-rendering on the home screen. - // This will otherwise cause a visual jump as the section gets rendered from no state - // to some state. - recentTabs = getRecentTabs(components), - recentBookmarks = emptyList(), - recentHistory = emptyList() - ) - ) requireComponents.backgroundServices.accountManagerAvailableQueue.runIfReadyOrQueue { // By the time this code runs, we may not be attached to a context or have a view lifecycle owner. @@ -1194,22 +1139,6 @@ class HomeFragment : Fragment() { ?.isVisible = tabCount > 0 } - private fun getTopSites(components: Components): List { - return if (components.settings.showTopSitesFeature) { - components.core.topSitesStorage.cachedTopSites.sort() - } else { - emptyList() - } - } - - private fun getRecentTabs(components: Components): List { - return if (components.settings.showRecentTabsFeature) { - components.core.store.state.asRecentTabs() - } else { - emptyList() - } - } - private fun displayWallpaperIfEnabled() { if (shouldEnableWallpaper()) { val wallpaperManger = requireComponents.wallpaperManager