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#19916 - Add last viewed tab to home screen
Co-authored-by: Jonathan Almeida <jalmeida@mozilla.com>
- Loading branch information
1 parent
0c1e712
commit e686281
Showing
29 changed files
with
837 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* 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.ext | ||
|
||
import mozilla.components.browser.state.selector.selectedTab | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.state.TabSessionState | ||
|
||
/** | ||
* Returns the currently selected tab if there's one as a list. | ||
* | ||
* @return A list of the currently selected tab or an empty list. | ||
*/ | ||
fun BrowserState.asRecentTabs(): List<TabSessionState> { | ||
val tab = selectedTab | ||
|
||
return if (tab != null && !tab.content.private) { | ||
listOfNotNull(tab) | ||
} else { | ||
emptyList() | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
app/src/main/java/org/mozilla/fenix/home/recenttabs/controller/RecentTabController.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.recenttabs.controller | ||
|
||
import androidx.navigation.NavController | ||
import mozilla.components.feature.tabs.TabsUseCases | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.ext.nav | ||
import org.mozilla.fenix.ext.navigateBlockingForAsyncNavGraph | ||
import org.mozilla.fenix.home.HomeFragmentDirections | ||
import org.mozilla.fenix.home.recenttabs.interactor.RecentTabInteractor | ||
|
||
/** | ||
* An interface that handles the view manipulation of the recent tabs in the Home screen. | ||
*/ | ||
interface RecentTabController { | ||
|
||
/** | ||
* @see [RecentTabInteractor.onRecentTabClicked] | ||
*/ | ||
fun handleRecentTabClicked(tabId: String) | ||
|
||
/** | ||
* @see [RecentTabInteractor.onRecentTabShowAllClicked] | ||
*/ | ||
fun handleRecentTabShowAllClicked() | ||
} | ||
|
||
/** | ||
* The default implementation of [RecentTabController]. | ||
*/ | ||
class DefaultRecentTabsController( | ||
private val selectTabUseCase: TabsUseCases.SelectTabUseCase, | ||
private val navController: NavController | ||
) : RecentTabController { | ||
|
||
override fun handleRecentTabClicked(tabId: String) { | ||
selectTabUseCase.invoke(tabId) | ||
navController.navigateBlockingForAsyncNavGraph(R.id.browserFragment) | ||
} | ||
|
||
override fun handleRecentTabShowAllClicked() { | ||
navController.nav( | ||
R.id.homeFragment, | ||
HomeFragmentDirections.actionGlobalTabsTrayFragment() | ||
) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/org/mozilla/fenix/home/recenttabs/interactor/RecentTabInteractor.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,23 @@ | ||
/* 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.recenttabs.interactor | ||
|
||
/** | ||
* Interface for recent tab related actions in the Home screen. | ||
*/ | ||
interface RecentTabInteractor { | ||
/** | ||
* Opens the given tab. Called when a user clicks on a recent tab. | ||
* | ||
* @param tabId The ID of the tab to open. | ||
*/ | ||
fun onRecentTabClicked(tabId: String) | ||
|
||
/** | ||
* Show the tabs tray. Called when a user clicks on the "Show all" button besides the recent | ||
* tabs. | ||
*/ | ||
fun onRecentTabShowAllClicked() | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/org/mozilla/fenix/home/recenttabs/view/RecentTabViewHolder.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,34 @@ | ||
/* 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.recenttabs.view | ||
|
||
import android.view.View | ||
import kotlinx.android.synthetic.main.recent_tabs_list_row.* | ||
import mozilla.components.browser.state.state.TabSessionState | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.home.sessioncontrol.SessionControlInteractor | ||
import org.mozilla.fenix.utils.view.ViewHolder | ||
|
||
/** | ||
* View holder for a recent tab item. | ||
*/ | ||
class RecentTabViewHolder( | ||
view: View, | ||
private val interactor: SessionControlInteractor | ||
) : ViewHolder(view) { | ||
|
||
fun bindTab(tab: TabSessionState) { | ||
recent_tab_title.text = tab.content.title | ||
recent_tab_icon.setImageBitmap(tab.content.icon) | ||
|
||
itemView.setOnClickListener { | ||
interactor.onRecentTabClicked(tab.id) | ||
} | ||
} | ||
|
||
companion object { | ||
const val LAYOUT_ID = R.layout.recent_tabs_list_row | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/org/mozilla/fenix/home/recenttabs/view/RecentTabsHeaderViewHolder.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,30 @@ | ||
/* 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.recenttabs.view | ||
|
||
import android.view.View | ||
import kotlinx.android.synthetic.main.recent_tabs_header.* | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.home.sessioncontrol.SessionControlInteractor | ||
import org.mozilla.fenix.utils.view.ViewHolder | ||
|
||
/** | ||
* View holder for the recent tabs header and "Show all" button. | ||
*/ | ||
class RecentTabsHeaderViewHolder( | ||
view: View, | ||
private val interactor: SessionControlInteractor | ||
) : ViewHolder(view) { | ||
|
||
init { | ||
show_all_button.setOnClickListener { | ||
interactor.onRecentTabShowAllClicked() | ||
} | ||
} | ||
|
||
companion object { | ||
const val LAYOUT_ID = R.layout.recent_tabs_header | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/org/mozilla/fenix/home/sessioncontrol/RecentTabsListFeature.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,47 @@ | ||
/* 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.sessioncontrol | ||
|
||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.map | ||
import mozilla.components.browser.state.selector.normalTabs | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import mozilla.components.lib.state.helpers.AbstractBinding | ||
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged | ||
import org.mozilla.fenix.home.HomeFragmentAction | ||
import org.mozilla.fenix.home.HomeFragmentStore | ||
|
||
/** | ||
* View-bound feature that dispatches recent tab changes to the [HomeFragmentStore] when the | ||
* [BrowserStore] is updated. | ||
*/ | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class RecentTabsListFeature( | ||
private val browserStore: BrowserStore, | ||
private val homeStore: HomeFragmentStore | ||
) : AbstractBinding<BrowserState>(browserStore) { | ||
|
||
override suspend fun onState(flow: Flow<BrowserState>) { | ||
flow.map { it.selectedTabId } | ||
.ifChanged() | ||
.collect { selectedTabId -> | ||
// Attempt to get the selected normal tab since here may not be a selected tab or | ||
// the selected tab may be a private tab. | ||
val selectedTab = browserStore.state.normalTabs.firstOrNull { | ||
it.id == selectedTabId | ||
} | ||
val recentTabsList = if (selectedTab != null) { | ||
listOf(selectedTab) | ||
} else { | ||
emptyList() | ||
} | ||
|
||
homeStore.dispatch(HomeFragmentAction.RecentTabsChange(recentTabsList)) | ||
} | ||
} | ||
} |
Oops, something went wrong.