For #2389 - Save state on homeview when switching themes
parent
9d74697ab3
commit
9a76c11dae
|
@ -7,6 +7,7 @@ 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
|
||||
|
@ -74,6 +75,7 @@ class HomeFragment : Fragment(), CoroutineScope {
|
|||
private var homeMenu: HomeMenu? = null
|
||||
|
||||
var deleteSessionJob: (suspend () -> Unit)? = null
|
||||
private var layoutManagerSate: Parcelable? = null
|
||||
|
||||
private val onboarding by lazy { FenixOnboarding(requireContext()) }
|
||||
private lateinit var sessionControlComponent: SessionControlComponent
|
||||
|
@ -117,6 +119,7 @@ class HomeFragment : Fragment(), CoroutineScope {
|
|||
ActionBusFactory.get(this).logMergedObservables()
|
||||
val activity = activity as HomeActivity
|
||||
DefaultThemeManager.applyStatusBarTheme(activity.window, activity.themeManager, activity)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
|
@ -180,6 +183,25 @@ class HomeFragment : Fragment(), CoroutineScope {
|
|||
homeDividerShadow.bringToFront()
|
||||
}
|
||||
|
||||
override fun onViewStateRestored(savedInstanceState: Bundle?) {
|
||||
savedInstanceState?.apply {
|
||||
layoutManagerSate = getParcelable(KEY_LAYOUT_MANAGER_STATE)
|
||||
homeLayout.progress = getFloat(KEY_MOTION_LAYOUT_PROGRESS)
|
||||
}
|
||||
|
||||
super.onViewStateRestored(savedInstanceState)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
sessionControlComponent.view.layoutManager!!.onSaveInstanceState()!!.apply {
|
||||
outState.putParcelable(KEY_LAYOUT_MANAGER_STATE, this)
|
||||
}
|
||||
|
||||
outState.putFloat(KEY_MOTION_LAYOUT_PROGRESS, homeLayout.progress)
|
||||
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
homeMenu = null
|
||||
job.cancel()
|
||||
|
@ -201,6 +223,13 @@ class HomeFragment : Fragment(), CoroutineScope {
|
|||
is SessionControlAction.Tab -> handleTabAction(it.action)
|
||||
is SessionControlAction.Collection -> handleCollectionAction(it.action)
|
||||
is SessionControlAction.Onboarding -> handleOnboardingAction(it.action)
|
||||
is SessionControlAction.ReloadData -> {
|
||||
layoutManagerSate?.also { parcelable ->
|
||||
sessionControlComponent.view.layoutManager?.onRestoreInstanceState(parcelable)
|
||||
}
|
||||
|
||||
layoutManagerSate = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -513,5 +542,7 @@ class HomeFragment : Fragment(), CoroutineScope {
|
|||
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -96,6 +96,7 @@ sealed class SessionControlAction : Action {
|
|||
data class Tab(val action: TabAction) : SessionControlAction()
|
||||
data class Collection(val action: CollectionAction) : SessionControlAction()
|
||||
data class Onboarding(val action: OnboardingAction) : SessionControlAction()
|
||||
object ReloadData : SessionControlAction()
|
||||
}
|
||||
|
||||
fun Observer<SessionControlAction>.onNext(tabAction: TabAction) {
|
||||
|
|
|
@ -109,7 +109,6 @@ class SessionControlUIView(
|
|||
.findViewById(R.id.home_component)
|
||||
|
||||
private val sessionControlAdapter = SessionControlAdapter(actionEmitter)
|
||||
private var expandedCollections = setOf<Long>()
|
||||
|
||||
init {
|
||||
view.apply {
|
||||
|
@ -127,10 +126,6 @@ class SessionControlUIView(
|
|||
|
||||
override fun updateView() = Consumer<SessionControlState> {
|
||||
sessionControlAdapter.reloadData(it.toAdapterList(), it.expandedCollections)
|
||||
expandedCollections = it.expandedCollections
|
||||
// There is a current bug in the combination of MotionLayout~alhpa4 and RecyclerView where it doesn't think
|
||||
// it has to redraw itself. For some reason calling scrollBy forces this to happen every time
|
||||
// https://stackoverflow.com/a/42549611
|
||||
view.scrollBy(0, 0)
|
||||
actionEmitter.onNext(SessionControlAction.ReloadData)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,6 @@ class OnboardingThemePickerViewHolder(private val view: View) : RecyclerView.Vie
|
|||
private fun setNewTheme(mode: Int) {
|
||||
if (AppCompatDelegate.getDefaultNightMode() == mode) return
|
||||
AppCompatDelegate.setDefaultNightMode(mode)
|
||||
view.context?.asActivity()?.recreate()
|
||||
view.context?.components?.core?.let {
|
||||
it.engine.settings.preferredColorScheme = it.getPreferredColorScheme()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue