From cfccb997fdf9d7bb60573db9909c6fa4fa421d6d Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Fri, 10 May 2019 17:20:45 -0500 Subject: [PATCH] For #1909: Tabs disappear intermittently (#2426) --- .../org/mozilla/fenix/home/HomeFragment.kt | 44 ++++++++++++++++++- .../sessioncontrol/SessionControlComponent.kt | 21 ++++++--- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 48c59fc67d..ca0ae25b9a 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -166,6 +166,45 @@ class HomeFragment : Fragment(), CoroutineScope { homeDividerShadow.bringToFront() } + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + val state = sessionControlComponent.stateObservable.blockingFirst() + outState.putParcelableArrayList( + KEY_TABS, + ArrayList(state.tabs) + ) + outState.putParcelableArrayList( + KEY_COLLECTIONS, + ArrayList(state.collections) + ) + val modeInt = if (state.mode is Mode.Private) 0 else 1 + outState.putInt(KEY_MODE, modeInt) + } + + override fun onViewStateRestored(savedInstanceState: Bundle?) { + super.onViewStateRestored(savedInstanceState) + if (savedInstanceState != null) { + getManagedEmitter().onNext( + SessionControlChange.TabsChange( + (savedInstanceState.getParcelableArrayList( + KEY_TABS + ) ?: arrayListOf()).toList() + ) + ) + getManagedEmitter().onNext( + SessionControlChange.CollectionsChange( + (savedInstanceState.getParcelableArrayList( + KEY_COLLECTIONS + ) ?: arrayListOf()).toList() + ) + ) + val mode = if (savedInstanceState.getInt(KEY_MODE) == 0) Mode.Private else Mode.Normal + getManagedEmitter().onNext( + SessionControlChange.ModeChange(mode) + ) + } + } + override fun onDestroyView() { homeMenu = null job.cancel() @@ -469,6 +508,9 @@ class HomeFragment : Fragment(), CoroutineScope { } companion object { - const val toolbarPaddingDp = 12f + private const val toolbarPaddingDp = 12f + private const val KEY_TABS = "tabs" + private const val KEY_COLLECTIONS = "collections" + private const val KEY_MODE = "mode" } } diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlComponent.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlComponent.kt index 09f6435322..9f692aff63 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlComponent.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlComponent.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.home.sessioncontrol import android.graphics.Bitmap +import android.os.Parcelable import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModel @@ -13,6 +14,7 @@ import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.RecyclerView import io.reactivex.Observable import io.reactivex.Observer +import kotlinx.android.parcel.Parcelize import org.mozilla.fenix.mvi.Action import org.mozilla.fenix.mvi.ActionBusFactory import org.mozilla.fenix.mvi.Change @@ -32,19 +34,25 @@ class SessionControlComponent( bus.getSafeManagedObservable(SessionControlChange::class.java) ) { + var stateObservable: Observable + lateinit var viewModel: SessionControlViewModel + override fun initView() = SessionControlUIView(container, actionEmitter, changesObservable) val view: RecyclerView get() = uiView.view as RecyclerView - override fun render(): Observable = - ViewModelProviders.of(owner, SessionControlViewModel.Factory(initialState, changesObservable)) - .get(SessionControlViewModel::class.java).render(uiView) + override fun render(): Observable { + viewModel = ViewModelProviders.of(owner, SessionControlViewModel.Factory(initialState, changesObservable)) + .get(SessionControlViewModel::class.java) + return viewModel.render(uiView) + } init { - render() + stateObservable = render() } } +@Parcelize data class Tab( val sessionId: String, val url: String, @@ -52,15 +60,16 @@ data class Tab( val title: String, val selected: Boolean? = null, val thumbnail: Bitmap? = null -) +) : Parcelable +@Parcelize data class TabCollection( val id: Int, val title: String, val tabs: MutableList, val iconColor: Int = 0, var expanded: Boolean = false -) +) : Parcelable sealed class Mode { object Normal : Mode()