For #9498: Replace navargs with viewModel to remember if scroll to tab

fennec/production
mcarare 4 years ago committed by Emily Kager
parent ab78951146
commit f52fa44207

@ -15,6 +15,7 @@ import androidx.annotation.CallSuper
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
@ -84,6 +85,7 @@ import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.sessionsOfType import org.mozilla.fenix.ext.sessionsOfType
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.SharedViewModel
import org.mozilla.fenix.settings.SupportUtils import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.theme.ThemeManager import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.wifi.SitePermissionsWifiIntegration import org.mozilla.fenix.wifi.SitePermissionsWifiIntegration
@ -130,6 +132,8 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session
// We need this so we don't accidentally remove all external sessions on back press // We need this so we don't accidentally remove all external sessions on back press
private var sessionRemoved = false private var sessionRemoved = false
private val sharedViewModel: SharedViewModel by activityViewModels()
@CallSuper @CallSuper
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -206,7 +210,8 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session
bookmarkTapped = { lifecycleScope.launch { bookmarkTapped(it) } }, bookmarkTapped = { lifecycleScope.launch { bookmarkTapped(it) } },
scope = lifecycleScope, scope = lifecycleScope,
tabCollectionStorage = requireComponents.core.tabCollectionStorage, tabCollectionStorage = requireComponents.core.tabCollectionStorage,
topSiteStorage = requireComponents.core.topSiteStorage topSiteStorage = requireComponents.core.topSiteStorage,
sharedViewModel = sharedViewModel
) )
_browserInteractor = BrowserInteractor( _browserInteractor = BrowserInteractor(

@ -37,6 +37,7 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getRootView import org.mozilla.fenix.ext.getRootView
import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.SharedViewModel
import org.mozilla.fenix.lib.Do import org.mozilla.fenix.lib.Do
import org.mozilla.fenix.settings.deletebrowsingdata.deleteAndQuit import org.mozilla.fenix.settings.deletebrowsingdata.deleteAndQuit
@ -70,7 +71,8 @@ class DefaultBrowserToolbarController(
private val bookmarkTapped: (Session) -> Unit, private val bookmarkTapped: (Session) -> Unit,
private val scope: CoroutineScope, private val scope: CoroutineScope,
private val tabCollectionStorage: TabCollectionStorage, private val tabCollectionStorage: TabCollectionStorage,
private val topSiteStorage: TopSiteStorage private val topSiteStorage: TopSiteStorage,
private val sharedViewModel: SharedViewModel
) : BrowserToolbarController { ) : BrowserToolbarController {
private val currentSession private val currentSession
@ -119,6 +121,7 @@ class DefaultBrowserToolbarController(
} }
override fun handleTabCounterClick() { override fun handleTabCounterClick() {
sharedViewModel.shouldScrollToSelectedTab = true
animateTabAndNavigateHome() animateTabAndNavigateHome()
} }
@ -128,7 +131,8 @@ class DefaultBrowserToolbarController(
ToolbarMenu.Item.AddToHomeScreen -> activity.settings().installPwaOpened = true ToolbarMenu.Item.AddToHomeScreen -> activity.settings().installPwaOpened = true
is ToolbarMenu.Item.ReaderMode -> activity.settings().readerModeOpened = true is ToolbarMenu.Item.ReaderMode -> activity.settings().readerModeOpened = true
ToolbarMenu.Item.OpenInApp -> activity.settings().openInAppOpened = true ToolbarMenu.Item.OpenInApp -> activity.settings().openInAppOpened = true
else -> {} else -> {
}
} }
} }
} }
@ -302,10 +306,14 @@ class DefaultBrowserToolbarController(
// Delay for a short amount of time so the browser has time to start animating out // Delay for a short amount of time so the browser has time to start animating out
// before we transition the fragment. This makes the animation feel smoother // before we transition the fragment. This makes the animation feel smoother
delay(ANIMATION_DELAY) delay(ANIMATION_DELAY)
val directions = BrowserFragmentDirections.actionBrowserFragmentToHomeFragmentPopUp( if (!navController.popBackStack(R.id.homeFragment, false)) {
shouldScrollToSelectedTab = true val directions = BrowserFragmentDirections.actionBrowserFragmentToHomeFragment()
) navController.nav(
navController.navigate(directions) R.id.browserFragment,
directions,
null
)
}
} }
} }

@ -37,7 +37,6 @@ import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
@ -116,6 +115,8 @@ class HomeFragment : Fragment() {
ViewModelProvider.AndroidViewModelFactory(requireActivity().application) ViewModelProvider.AndroidViewModelFactory(requireActivity().application)
} }
private val sharedViewModel: SharedViewModel by activityViewModels()
private val snackbarAnchorView: View? private val snackbarAnchorView: View?
get() { get() {
return if (requireContext().settings().shouldUseBottomToolbar) { return if (requireContext().settings().shouldUseBottomToolbar) {
@ -510,10 +511,9 @@ class HomeFragment : Fragment() {
activity?.window?.setBackgroundDrawableResource(R.drawable.private_home_background_gradient) activity?.window?.setBackgroundDrawableResource(R.drawable.private_home_background_gradient)
} }
hideToolbar() hideToolbar()
val safeArguments = arguments?.let { navArgs<HomeFragmentArgs>().value } if (sharedViewModel.shouldScrollToSelectedTab) {
val shouldScrollToSelectedTab = safeArguments?.shouldScrollToSelectedTab ?: false
if (shouldScrollToSelectedTab) {
scrollToSelectedTab() scrollToSelectedTab()
sharedViewModel.shouldScrollToSelectedTab = false
} }
} }

@ -0,0 +1,15 @@
/* 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
import androidx.lifecycle.ViewModel
class SharedViewModel : ViewModel() {
/**
* Used to remember if we need to scroll to the selected tab in the homeFragment's recycleView see #7356
* */
var shouldScrollToSelectedTab: Boolean = false
}

@ -63,10 +63,6 @@
android:id="@+id/homeFragment" android:id="@+id/homeFragment"
android:name="org.mozilla.fenix.home.HomeFragment" android:name="org.mozilla.fenix.home.HomeFragment"
tools:layout="@layout/fragment_home"> tools:layout="@layout/fragment_home">
<argument
android:name="shouldScrollToSelectedTab"
app:argType="boolean"
android:defaultValue="false" />
<action <action
android:id="@+id/action_homeFragment_to_turnOnSyncFragment" android:id="@+id/action_homeFragment_to_turnOnSyncFragment"
app:destination="@+id/turnOnSyncFragment" /> app:destination="@+id/turnOnSyncFragment" />
@ -185,9 +181,8 @@
app:exitAnim="@anim/fade_out" app:exitAnim="@anim/fade_out"
tools:layout="@layout/fragment_browser"> tools:layout="@layout/fragment_browser">
<action <action
android:id="@+id/action_browserFragment_to_homeFragment_popUp" android:id="@+id/action_browserFragment_to_homeFragment"
app:destination="@id/homeFragment" app:destination="@id/homeFragment" />
app:popUpTo="@id/homeFragment" />
<action <action
android:id="@+id/action_browserFragment_to_searchFragment" android:id="@+id/action_browserFragment_to_searchFragment"
app:enterAnim="@anim/fade_in_up" app:enterAnim="@anim/fade_in_up"

Loading…
Cancel
Save