1
0
Fork 0

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

master
mcarare 2020-03-30 13:43:01 +03:00 committed by Emily Kager
parent ab78951146
commit f52fa44207
5 changed files with 41 additions and 18 deletions

View File

@ -15,6 +15,7 @@ import androidx.annotation.CallSuper
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
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.sessionsOfType
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.SharedViewModel
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.theme.ThemeManager
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
private var sessionRemoved = false
private val sharedViewModel: SharedViewModel by activityViewModels()
@CallSuper
override fun onCreateView(
inflater: LayoutInflater,
@ -206,7 +210,8 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session
bookmarkTapped = { lifecycleScope.launch { bookmarkTapped(it) } },
scope = lifecycleScope,
tabCollectionStorage = requireComponents.core.tabCollectionStorage,
topSiteStorage = requireComponents.core.topSiteStorage
topSiteStorage = requireComponents.core.topSiteStorage,
sharedViewModel = sharedViewModel
)
_browserInteractor = BrowserInteractor(

View File

@ -37,6 +37,7 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getRootView
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.SharedViewModel
import org.mozilla.fenix.lib.Do
import org.mozilla.fenix.settings.deletebrowsingdata.deleteAndQuit
@ -70,7 +71,8 @@ class DefaultBrowserToolbarController(
private val bookmarkTapped: (Session) -> Unit,
private val scope: CoroutineScope,
private val tabCollectionStorage: TabCollectionStorage,
private val topSiteStorage: TopSiteStorage
private val topSiteStorage: TopSiteStorage,
private val sharedViewModel: SharedViewModel
) : BrowserToolbarController {
private val currentSession
@ -119,6 +121,7 @@ class DefaultBrowserToolbarController(
}
override fun handleTabCounterClick() {
sharedViewModel.shouldScrollToSelectedTab = true
animateTabAndNavigateHome()
}
@ -128,7 +131,8 @@ class DefaultBrowserToolbarController(
ToolbarMenu.Item.AddToHomeScreen -> activity.settings().installPwaOpened = true
is ToolbarMenu.Item.ReaderMode -> activity.settings().readerModeOpened = 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
// before we transition the fragment. This makes the animation feel smoother
delay(ANIMATION_DELAY)
val directions = BrowserFragmentDirections.actionBrowserFragmentToHomeFragmentPopUp(
shouldScrollToSelectedTab = true
)
navController.navigate(directions)
if (!navController.popBackStack(R.id.homeFragment, false)) {
val directions = BrowserFragmentDirections.actionBrowserFragmentToHomeFragment()
navController.nav(
R.id.browserFragment,
directions,
null
)
}
}
}

View File

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

View File

@ -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
}

View File

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