From dec2ddef27a17f1d3979d3ca01a9785c7d1c2bec Mon Sep 17 00:00:00 2001 From: David Walsh Date: Fri, 22 May 2020 21:41:49 -0500 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/10864 - Display snack bar to undo closing tabs --- .../fenix/browser/BaseBrowserFragment.kt | 29 +++++++++++++++++++ .../org/mozilla/fenix/home/HomeFragment.kt | 27 +++++++++++++++++ .../fenix/tabtray/TabTrayDialogFragment.kt | 5 ++++ .../org/mozilla/fenix/tabtray/TabTrayView.kt | 5 +++- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt index bad6f7cd46..59c76fac83 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -36,6 +36,7 @@ import mozilla.components.browser.state.action.ContentAction import mozilla.components.browser.state.state.content.DownloadState import mozilla.components.browser.state.store.BrowserStore import mozilla.components.concept.engine.prompt.ShareData +import mozilla.components.concept.tabstray.Tab import mozilla.components.feature.accounts.FxaCapability import mozilla.components.feature.accounts.FxaWebChannelFeature import mozilla.components.feature.app.links.AppLinksFeature @@ -96,6 +97,7 @@ import org.mozilla.fenix.ext.settings import org.mozilla.fenix.home.SharedViewModel import org.mozilla.fenix.tabtray.TabTrayDialogFragment import org.mozilla.fenix.theme.ThemeManager +import org.mozilla.fenix.utils.allowUndo import org.mozilla.fenix.wifi.SitePermissionsWifiIntegration import java.lang.ref.WeakReference @@ -225,6 +227,33 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session tabTrayDialog.dismiss() findNavController().navigate(BrowserFragmentDirections.actionGlobalHome()) } + + override fun onTabClosed(tab: Tab) { + val snapshot = sessionManager + .findSessionById(tab.id)?.let { + sessionManager.createSessionSnapshot(it) + } ?: return + + val state = snapshot.engineSession?.saveState() + val isSelected = tab.id == requireComponents.core.store.state.selectedTabId ?: false + + val snackbarMessage = if (snapshot.session.private) { + getString(R.string.snackbar_private_tab_closed) + } else { + getString(R.string.snackbar_tab_closed) + } + + viewLifecycleOwner.lifecycleScope.allowUndo( + requireView(), + snackbarMessage, + getString(R.string.snackbar_deleted_undo), + { + sessionManager.add(snapshot.session, isSelected, engineSessionState = state) + }, + operation = { }//, + //anchorView = view?.tab_tray_controls + ) + } } } ) 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 24bc20d17b..591f023ab7 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -371,6 +371,33 @@ class HomeFragment : Fragment() { (activity as HomeActivity).browsingModeManager.mode = BrowsingMode.fromBoolean(private) tabTrayDialog.dismiss() } + + override fun onTabClosed(tab: mozilla.components.concept.tabstray.Tab) { + val snapshot = sessionManager + .findSessionById(tab.id)?.let { + sessionManager.createSessionSnapshot(it) + } ?: return + + val state = snapshot.engineSession?.saveState() + val isSelected = tab.id == requireComponents.core.store.state.selectedTabId ?: false + + val snackbarMessage = if (snapshot.session.private) { + getString(R.string.snackbar_private_tab_closed) + } else { + getString(R.string.snackbar_tab_closed) + } + + viewLifecycleOwner.lifecycleScope.allowUndo( + requireView(), + snackbarMessage, + getString(R.string.snackbar_deleted_undo), + { + sessionManager.add(snapshot.session, isSelected, engineSessionState = state) + }, + operation = { }//, + //anchorView = view?.tab_tray_controls + ) + } } } diff --git a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt index d4989d4320..5fcdaf1421 100644 --- a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt @@ -21,6 +21,7 @@ class TabTrayDialogFragment : AppCompatDialogFragment(), TabTrayInteractor { interface Interactor { fun onTabSelected(tab: Tab) fun onNewTabTapped(private: Boolean) + fun onTabClosed(tab: Tab) } private lateinit var tabTrayView: TabTrayView @@ -62,6 +63,10 @@ class TabTrayDialogFragment : AppCompatDialogFragment(), TabTrayInteractor { } } + override fun onTabClosed(tab: Tab) { + interactor?.onTabClosed(tab) + } + override fun onTabSelected(tab: Tab) { interactor?.onTabSelected(tab) } diff --git a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt index dc0e5684e7..eef5fcfe44 100644 --- a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt +++ b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt @@ -22,6 +22,7 @@ import org.mozilla.fenix.R import org.mozilla.fenix.ext.components interface TabTrayInteractor { + fun onTabClosed(tab: Tab) fun onTabSelected(tab: Tab) fun onNewTabTapped(private: Boolean) fun onTabTrayDismissed() @@ -110,7 +111,9 @@ class TabTrayView( tabsFeature.filterTabs(filter) } - override fun onTabClosed(tab: Tab) { /*noop*/ } + override fun onTabClosed(tab: Tab) { + interactor.onTabClosed(tab) + } override fun onTabReselected(tab: TabLayout.Tab?) { /*noop*/ } override fun onTabUnselected(tab: TabLayout.Tab?) { /*noop*/ }