Commit 030bd386 authored by codrut.topliceanu's avatar codrut.topliceanu Committed by Jeff Boek
Browse files

For #8561 - Scroll to topSites on app cold start

parent cc8f34b6
......@@ -28,6 +28,7 @@ import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.Observer
......@@ -95,6 +96,10 @@ import kotlin.math.min
@ExperimentalCoroutinesApi
@SuppressWarnings("TooManyFunctions", "LargeClass")
class HomeFragment : Fragment() {
private val homeViewModel: HomeScreenViewModel by viewModels {
ViewModelProvider.AndroidViewModelFactory(requireActivity().application)
}
private val snackbarAnchorView: View?
get() {
return if (requireContext().settings().shouldUseBottomToolbar) {
......@@ -206,7 +211,8 @@ class HomeFragment : Fragment() {
setOffset(view)
sessionControlView = SessionControlView(
homeFragmentStore,
view.sessionControlRecyclerView, sessionControlInteractor
view.sessionControlRecyclerView, sessionControlInteractor,
homeViewModel
)
activity.themeManager.applyStatusBarTheme(activity)
......
......@@ -10,4 +10,9 @@ import androidx.lifecycle.ViewModel
class HomeScreenViewModel : ViewModel() {
var layoutManagerState: Parcelable? = null
var motionLayoutProgress = 0F
/**
* Used to remember if we need to scroll to top of the homeFragment's recycleView (top sites) see #8561
* */
var shouldScrollToTopSites: Boolean = true
}
......@@ -18,6 +18,7 @@ import mozilla.components.lib.state.ext.consumeFrom
import org.mozilla.fenix.R
import org.mozilla.fenix.home.HomeFragmentState
import org.mozilla.fenix.home.HomeFragmentStore
import org.mozilla.fenix.home.HomeScreenViewModel
import org.mozilla.fenix.home.Mode
import org.mozilla.fenix.home.OnboardingState
import org.mozilla.fenix.home.Tab
......@@ -163,7 +164,8 @@ private fun collectionTabItems(collection: TabCollection) = collection.tabs.mapI
class SessionControlView(
private val homeFragmentStore: HomeFragmentStore,
override val containerView: View?,
interactor: SessionControlInteractor
interactor: SessionControlInteractor,
private var homeScreenViewModel: HomeScreenViewModel
) : LayoutContainer {
val view: RecyclerView = containerView as RecyclerView
......@@ -193,6 +195,22 @@ class SessionControlView(
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
sessionControlAdapter.submitList(null)
}
sessionControlAdapter.submitList(state.toAdapterList())
val stateAdapterList = state.toAdapterList()
if (homeScreenViewModel.shouldScrollToTopSites) {
sessionControlAdapter.submitList(stateAdapterList) {
val loadedTopSites = stateAdapterList.find { adapterItem ->
adapterItem is AdapterItem.TopSiteList && adapterItem.topSites.isNotEmpty()
}
loadedTopSites?.run {
homeScreenViewModel.shouldScrollToTopSites = false
view.scrollToPosition(stateAdapterList.indexOf(this))
}
}
} else {
sessionControlAdapter.submitList(stateAdapterList)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment