Skip to content

Commit

Permalink
for mozilla-mobile#24177: sync tabs when home is shown
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewTighe authored and mergify[bot] committed Apr 7, 2022
1 parent 38bde17 commit dbfd5ff
Show file tree
Hide file tree
Showing 21 changed files with 859 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.mozilla.fenix.home.Mode
import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesCategory
import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesSelectedCategory
import org.mozilla.fenix.home.recentbookmarks.RecentBookmark
import org.mozilla.fenix.home.recentsyncedtabs.RecentSyncedTabState
import org.mozilla.fenix.home.recenttabs.RecentTab
import org.mozilla.fenix.home.recentvisits.RecentlyVisitedItem
import org.mozilla.fenix.gleanplumb.Message
Expand Down Expand Up @@ -63,6 +64,12 @@ sealed class AppAction : Action {
val categoriesSelected: List<PocketRecommendedStoriesSelectedCategory>
) : AppAction()
object RemoveCollectionsPlaceholder : AppAction()

/**
* Updates the [RecentSyncedTabState] with the given [state].
*/
data class RecentSyncedTabStateChange(val state: RecentSyncedTabState) : AppAction()

/**
* [Action]s related to interactions with the Messaging Framework.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.mozilla.fenix.home.Mode
import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesCategory
import org.mozilla.fenix.home.pocket.PocketRecommendedStoriesSelectedCategory
import org.mozilla.fenix.home.recentbookmarks.RecentBookmark
import org.mozilla.fenix.home.recentsyncedtabs.RecentSyncedTabState
import org.mozilla.fenix.home.recenttabs.RecentTab
import org.mozilla.fenix.home.recentvisits.RecentlyVisitedItem
import org.mozilla.fenix.gleanplumb.MessagingState
Expand All @@ -32,6 +33,7 @@ import org.mozilla.fenix.gleanplumb.MessagingState
* @property topSites The list of [TopSite] in the [HomeFragment].
* @property showCollectionPlaceholder If true, shows a placeholder when there are no collections.
* @property recentTabs The list of recent [RecentTab] in the [HomeFragment].
* @property recentSyncedTabState The [RecentSyncedTabState] in the [HomeFragment].
* @property recentBookmarks The list of recently saved [BookmarkNode]s to show on the [HomeFragment].
* @property recentHistory The list of [RecentlyVisitedItem]s.
* @property pocketStories The list of currently shown [PocketRecommendedStory]s.
Expand All @@ -48,6 +50,7 @@ data class AppState(
val topSites: List<TopSite> = emptyList(),
val showCollectionPlaceholder: Boolean = false,
val recentTabs: List<RecentTab> = emptyList(),
val recentSyncedTabState: RecentSyncedTabState = RecentSyncedTabState.None,
val recentBookmarks: List<RecentBookmark> = emptyList(),
val recentHistory: List<RecentlyVisitedItem> = emptyList(),
val pocketStories: List<PocketRecommendedStory> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ internal object AppStoreReducer {
recentTabs = state.recentTabs.filterOutTab(action.recentTab)
)
}
is AppAction.RecentSyncedTabStateChange -> {
state.copy(
recentSyncedTabState = action.state
)
}
is AppAction.RecentBookmarksChange -> state.copy(recentBookmarks = action.recentBookmarks)
is AppAction.RemoveRecentBookmark -> {
state.copy(recentBookmarks = state.recentBookmarks.filterNot { it.url == action.recentBookmark.url })
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ 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.recentsyncedtabs.RecentSyncedTabFeature
import org.mozilla.fenix.home.recentsyncedtabs.controller.DefaultRecentSyncedTabController
import org.mozilla.fenix.home.recenttabs.RecentTabsListFeature
import org.mozilla.fenix.home.recenttabs.controller.DefaultRecentTabsController
import org.mozilla.fenix.home.recentvisits.RecentVisitsFeature
Expand Down Expand Up @@ -167,6 +169,16 @@ class HomeFragment : Fragment() {
}
}

private val syncedTabFeature by lazy {
RecentSyncedTabFeature(
store = requireComponents.appStore,
context = requireContext(),
storage = requireComponents.backgroundServices.syncedTabsStorage,
accountManager = requireComponents.backgroundServices.accountManager,
lifecycleOwner = viewLifecycleOwner
)
}

private var _sessionControlInteractor: SessionControlInteractor? = null
private val sessionControlInteractor: SessionControlInteractor
get() = _sessionControlInteractor!!
Expand All @@ -178,6 +190,7 @@ class HomeFragment : Fragment() {
private val topSitesFeature = ViewBoundFeatureWrapper<TopSitesFeature>()
private val messagingFeature = ViewBoundFeatureWrapper<MessagingFeature>()
private val recentTabsListFeature = ViewBoundFeatureWrapper<RecentTabsListFeature>()
private val recentSyncedTabFeature = ViewBoundFeatureWrapper<RecentSyncedTabFeature>()
private val recentBookmarksFeature = ViewBoundFeatureWrapper<RecentBookmarksFeature>()
private val historyMetadataFeature = ViewBoundFeatureWrapper<RecentVisitsFeature>()

Expand Down Expand Up @@ -278,6 +291,14 @@ class HomeFragment : Fragment() {
owner = viewLifecycleOwner,
view = binding.root
)

if (FeatureFlags.taskContinuityFeature) {
recentSyncedTabFeature.set(
feature = syncedTabFeature,
owner = viewLifecycleOwner,
view = binding.root
)
}
}

if (requireContext().settings().showRecentBookmarksFeature) {
Expand Down Expand Up @@ -340,6 +361,10 @@ class HomeFragment : Fragment() {
store = components.core.store,
appStore = components.appStore,
),
recentSyncedTabController = DefaultRecentSyncedTabController(
addNewTabUseCase = requireComponents.useCases.tabsUseCases.addTab,
navController = findNavController(),
),
recentBookmarksController = DefaultRecentBookmarksController(
activity = activity,
navController = findNavController(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.home.recentsyncedtabs

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import mozilla.components.browser.storage.sync.SyncedDeviceTabs
import mozilla.components.feature.syncedtabs.SyncedTabsFeature
import mozilla.components.feature.syncedtabs.storage.SyncedTabsStorage
import mozilla.components.feature.syncedtabs.view.SyncedTabsView
import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.support.base.feature.LifecycleAwareFeature
import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.components.appstate.AppAction

/**
* Delegate to handle layout updates and dispatch actions related to the recent synced tab.
*
* @property store Store to dispatch actions to when synced tabs are updated or errors encountered.
* @param accountManager Account manager used to retrieve synced tab state.
* @param context [Context] used for retrieving the sync engine storage state.
* @param storage Storage layer for synced tabs.
* @param lifecycleOwner View lifecycle owner to determine start/stop state for feature.
*/
@Suppress("LongParameterList")
class RecentSyncedTabFeature(
private val store: AppStore,
accountManager: FxaAccountManager,
context: Context,
storage: SyncedTabsStorage,
lifecycleOwner: LifecycleOwner,
) : SyncedTabsView, LifecycleAwareFeature {
private val syncedTabsFeature by lazy {
SyncedTabsFeature(
view = this,
context = context,
storage = storage,
accountManager = accountManager,
lifecycleOwner = lifecycleOwner,
onTabClicked = {}
)
}

override var listener: SyncedTabsView.Listener? = null

override fun startLoading() {
store.dispatch(
AppAction.RecentSyncedTabStateChange(RecentSyncedTabState.Loading)
)
}

override fun displaySyncedTabs(syncedTabs: List<SyncedDeviceTabs>) {
val syncedTab = syncedTabs
.filterNot { it.device.isCurrentDevice || it.tabs.isEmpty() }
.maxByOrNull { it.device.lastAccessTime ?: 0 }
?.let {
val tab = it.tabs.firstOrNull()?.active() ?: return
RecentSyncedTab(
deviceDisplayName = it.device.displayName,
title = tab.title,
url = tab.url,
iconUrl = tab.iconUrl
)
} ?: return
store.dispatch(
AppAction.RecentSyncedTabStateChange(RecentSyncedTabState.Success(syncedTab))
)
}

// UI will either not be displayed if not authenticated (DefaultPresenter.start),
// or the display state will be tied directly to the success and error cases.
override fun stopLoading() = Unit

override fun onError(error: SyncedTabsView.ErrorType) {
store.dispatch(AppAction.RecentSyncedTabStateChange(RecentSyncedTabState.None))
}

override fun start() {
syncedTabsFeature.start()
}

override fun stop() {
syncedTabsFeature.stop()
}
}

/**
* The state of the recent synced tab.
*/
sealed class RecentSyncedTabState {
/**
* There is no synced tab, or a user is not authenticated.
*/
object None : RecentSyncedTabState()

/**
* A user is authenticated and the sync is running.
*/
object Loading : RecentSyncedTabState()

/**
* A user is authenticated and the most recent synced tab has been found.
*/
data class Success(val tab: RecentSyncedTab) : RecentSyncedTabState()
}

/**
* A tab that was recently viewed on a synced device.
*
* @param deviceDisplayName The device the tab was viewed on.
* @param title The title of the tab.
* @param url The url of the tab.
* @param iconUrl The url used to retrieve the icon of the tab.
*/
data class RecentSyncedTab(
val deviceDisplayName: String,
val title: String,
val url: String,
val iconUrl: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.home.recentsyncedtabs.controller

import androidx.navigation.NavController
import mozilla.components.feature.tabs.TabsUseCases
import org.mozilla.fenix.R
import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.home.recentsyncedtabs.RecentSyncedTab
import org.mozilla.fenix.home.recentsyncedtabs.interactor.RecentSyncedTabInteractor
import org.mozilla.fenix.tabstray.Page

/**
* An interface that handles the view manipulation of the recent synced tabs in the Home screen.
*/
interface RecentSyncedTabController {
/**
* @see [RecentSyncedTabInteractor.onRecentSyncedTabClicked]
*/
fun handleRecentSyncedTabClick(tab: RecentSyncedTab)

/**
* @see [RecentSyncedTabInteractor.onRecentSyncedTabClicked]
*/
fun handleSyncedTabShowAllClicked()
}

/**
* The default implementation of [RecentSyncedTabController].
*
* @property addNewTabUseCase Use case to open the synced tab when clicked.
* @property navController [NavController] to navigate to synced tabs tray.
*/
class DefaultRecentSyncedTabController(
private val addNewTabUseCase: TabsUseCases.AddNewTabUseCase,
private val navController: NavController,
) : RecentSyncedTabController {
override fun handleRecentSyncedTabClick(tab: RecentSyncedTab) {
addNewTabUseCase.invoke(tab.url)
navController.navigate(R.id.browserFragment)
}

override fun handleSyncedTabShowAllClicked() {
navController.navigate(
HomeFragmentDirections.actionGlobalTabsTrayFragment(page = Page.SyncedTabs)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.home.recentsyncedtabs.interactor

import org.mozilla.fenix.home.recentsyncedtabs.RecentSyncedTab

/**
* Interface for recent synced tab related actions in the Home screen.
*/
interface RecentSyncedTabInteractor {
/**
* Opens the synced tab locally. Called when a user clicks on a recent synced tab.
*
* @param tab The recent synced tab that has been clicked.
*/
fun onRecentSyncedTabClicked(tab: RecentSyncedTab)

/**
* Opens the tabs tray to the synced tab page. Called when a user clicks on the "See all synced
* tabs" button.
*/
fun onSyncedTabShowAllClicked()
}
Loading

0 comments on commit dbfd5ff

Please sign in to comment.