2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-05 21:20:45 +00:00

For #1826 - Create ViewModel for Restoring Home Scroll Position

This commit is contained in:
Emily Kager 2019-05-21 10:05:25 -07:00 committed by Emily Kager
parent 4b186eb86d
commit 0c022f6646
2 changed files with 36 additions and 30 deletions

View File

@ -7,7 +7,6 @@ package org.mozilla.fenix.home
import android.content.res.Resources
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -82,8 +81,6 @@ class HomeFragment : Fragment(), CoroutineScope {
private var homeMenu: HomeMenu? = null
var deleteSessionJob: (suspend () -> Unit)? = null
private var layoutManagerState: Parcelable? = null
private var motionLayoutProgress = 0F
private val onboarding by lazy { FenixOnboarding(requireContext()) }
private lateinit var sessionControlComponent: SessionControlComponent
@ -193,22 +190,6 @@ class HomeFragment : Fragment(), CoroutineScope {
homeDividerShadow.bringToFront()
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
savedInstanceState?.apply {
layoutManagerState = getParcelable(KEY_LAYOUT_MANAGER_STATE)
motionLayoutProgress = getFloat(KEY_MOTION_LAYOUT_PROGRESS)
homeLayout.progress = if (motionLayoutProgress > MOTION_LAYOUT_PROGRESS_ROUND_POINT) 1.0f else 0f
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable(KEY_LAYOUT_MANAGER_STATE, layoutManagerState)
outState.putFloat(KEY_MOTION_LAYOUT_PROGRESS, motionLayoutProgress)
}
override fun onDestroyView() {
homeMenu = null
job.cancel()
@ -217,10 +198,14 @@ class HomeFragment : Fragment(), CoroutineScope {
override fun onResume() {
super.onResume()
layoutManagerState?.also { parcelable ->
val homeViewModel = activity?.run {
ViewModelProviders.of(this).get(HomeScreenViewModel::class.java)
}
homeViewModel?.layoutManagerState?.also { parcelable ->
sessionControlComponent.view.layoutManager?.onRestoreInstanceState(parcelable)
}
homeLayout?.progress = if (motionLayoutProgress > MOTION_LAYOUT_PROGRESS_ROUND_POINT) 1.0f else 0f
homeLayout?.progress =
if (homeViewModel?.motionLayoutProgress ?: 0F > MOTION_LAYOUT_PROGRESS_ROUND_POINT) 1.0f else 0f
(activity as AppCompatActivity).supportActionBar?.hide()
}
@ -235,11 +220,13 @@ class HomeFragment : Fragment(), CoroutineScope {
is SessionControlAction.Collection -> handleCollectionAction(it.action)
is SessionControlAction.Onboarding -> handleOnboardingAction(it.action)
is SessionControlAction.ReloadData -> {
layoutManagerState?.also { parcelable ->
val homeViewModel = activity?.run {
ViewModelProviders.of(this).get(HomeScreenViewModel::class.java)
}
homeViewModel?.layoutManagerState?.also { parcelable ->
sessionControlComponent.view.layoutManager?.onRestoreInstanceState(parcelable)
}
layoutManagerState = null
homeViewModel?.layoutManagerState = null
}
}
}
@ -279,7 +266,9 @@ class HomeFragment : Fragment(), CoroutineScope {
private fun handleTabAction(action: TabAction) {
Do exhaustive when (action) {
is TabAction.SaveTabGroup -> {
if ((activity as HomeActivity).browsingModeManager.isPrivate) { return }
if ((activity as HomeActivity).browsingModeManager.isPrivate) {
return
}
showCollectionCreationFragment(action.selectedTabSessionId)
}
is TabAction.Select -> {
@ -393,8 +382,12 @@ class HomeFragment : Fragment(), CoroutineScope {
override fun onPause() {
super.onPause()
layoutManagerState = sessionControlComponent.view.layoutManager?.onSaveInstanceState()
motionLayoutProgress = homeLayout?.progress ?: 0F
val homeViewModel = activity?.run {
ViewModelProviders.of(this).get(HomeScreenViewModel::class.java)
}
homeViewModel?.layoutManagerState =
sessionControlComponent.view.layoutManager?.onSaveInstanceState()
homeViewModel?.motionLayoutProgress = homeLayout?.progress ?: 0F
sessionObserver?.let {
requireComponents.core.sessionManager.unregister(it)
}
@ -577,12 +570,12 @@ class HomeFragment : Fragment(), CoroutineScope {
Mode.Onboarding
} else if ((activity as HomeActivity).browsingModeManager.isPrivate) {
Mode.Private
} else { Mode.Normal }
} else {
Mode.Normal
}
companion object {
private const val toolbarPaddingDp = 12f
private const val KEY_MOTION_LAYOUT_PROGRESS = "motionLayout.progress"
private const val KEY_LAYOUT_MANAGER_STATE = "layoutManager.state"
private const val MOTION_LAYOUT_PROGRESS_ROUND_POINT = 0.25f
}
}

View File

@ -0,0 +1,13 @@
package org.mozilla.fenix.home
/* 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/. */
import android.os.Parcelable
import androidx.lifecycle.ViewModel
class HomeScreenViewModel : ViewModel() {
var layoutManagerState: Parcelable? = null
var motionLayoutProgress = 0F
}