forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for mozilla-mobile#24177: sync tabs when home is shown
- Loading branch information
1 parent
38bde17
commit dbfd5ff
Showing
21 changed files
with
859 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
app/src/main/java/org/mozilla/fenix/home/recentsyncedtabs/RecentSyncedTabFeature.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?, | ||
) |
50 changes: 50 additions & 0 deletions
50
...main/java/org/mozilla/fenix/home/recentsyncedtabs/controller/RecentSyncedTabController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...main/java/org/mozilla/fenix/home/recentsyncedtabs/interactor/RecentSyncedTabInteractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.