Move currentDisplayMode to the ViewState

This commit is contained in:
Benoit Marty 2019-09-24 11:24:03 +02:00
parent 6890f83810
commit af433266c8
3 changed files with 18 additions and 16 deletions

View file

@ -51,8 +51,6 @@ data class HomeDetailParams(
) : Parcelable ) : Parcelable
private const val CURRENT_DISPLAY_MODE = "CURRENT_DISPLAY_MODE"
private const val INDEX_CATCHUP = 0 private const val INDEX_CATCHUP = 0
private const val INDEX_PEOPLE = 1 private const val INDEX_PEOPLE = 1
private const val INDEX_ROOMS = 2 private const val INDEX_ROOMS = 2
@ -61,7 +59,6 @@ class HomeDetailFragment : VectorBaseFragment(), KeysBackupBanner.Delegate {
private val params: HomeDetailParams by args() private val params: HomeDetailParams by args()
private val unreadCounterBadgeViews = arrayListOf<UnreadCounterBadgeView>() private val unreadCounterBadgeViews = arrayListOf<UnreadCounterBadgeView>()
private lateinit var currentDisplayMode: RoomListFragment.DisplayMode
private val viewModel: HomeDetailViewModel by fragmentViewModel() private val viewModel: HomeDetailViewModel by fragmentViewModel()
private lateinit var navigationViewModel: HomeNavigationViewModel private lateinit var navigationViewModel: HomeNavigationViewModel
@ -80,15 +77,16 @@ class HomeDetailFragment : VectorBaseFragment(), KeysBackupBanner.Delegate {
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
currentDisplayMode = savedInstanceState?.getSerializable(CURRENT_DISPLAY_MODE) as? RoomListFragment.DisplayMode
?: RoomListFragment.DisplayMode.HOME
navigationViewModel = ViewModelProviders.of(requireActivity()).get(HomeNavigationViewModel::class.java) navigationViewModel = ViewModelProviders.of(requireActivity()).get(HomeNavigationViewModel::class.java)
switchDisplayMode(currentDisplayMode)
setupBottomNavigationView() setupBottomNavigationView()
setupToolbar() setupToolbar()
setupKeysBackupBanner() setupKeysBackupBanner()
viewModel.selectSubscribe(this, HomeDetailViewState::displayMode) { displayMode ->
switchDisplayMode(displayMode)
}
} }
private fun setupKeysBackupBanner() { private fun setupKeysBackupBanner() {
@ -126,11 +124,6 @@ class HomeDetailFragment : VectorBaseFragment(), KeysBackupBanner.Delegate {
} }
override fun onSaveInstanceState(outState: Bundle) {
outState.putSerializable(CURRENT_DISPLAY_MODE, currentDisplayMode)
super.onSaveInstanceState(outState)
}
private fun setupToolbar() { private fun setupToolbar() {
val parentActivity = vectorBaseActivity val parentActivity = vectorBaseActivity
if (parentActivity is ToolbarConfigurable) { if (parentActivity is ToolbarConfigurable) {
@ -156,10 +149,7 @@ class HomeDetailFragment : VectorBaseFragment(), KeysBackupBanner.Delegate {
R.id.bottom_action_rooms -> RoomListFragment.DisplayMode.ROOMS R.id.bottom_action_rooms -> RoomListFragment.DisplayMode.ROOMS
else -> RoomListFragment.DisplayMode.HOME else -> RoomListFragment.DisplayMode.HOME
} }
if (currentDisplayMode != displayMode) { viewModel.switchDisplayMode(displayMode)
currentDisplayMode = displayMode
switchDisplayMode(displayMode)
}
true true
} }

View file

@ -24,10 +24,12 @@ import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import im.vector.matrix.rx.rx import im.vector.matrix.rx.rx
import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.features.home.room.list.RoomListFragment
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
/** /**
* View model used to update the home bottom bar notification counts * View model used to update the home bottom bar notification counts, observe the sync state and
* change the selected room list view
*/ */
class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: HomeDetailViewState, class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: HomeDetailViewState,
private val session: Session, private val session: Session,
@ -53,6 +55,14 @@ class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: Ho
observeRoomSummaries() observeRoomSummaries()
} }
fun switchDisplayMode(displayMode: RoomListFragment.DisplayMode) = withState { state ->
if (state.displayMode != displayMode) {
setState {
copy(displayMode = displayMode)
}
}
}
// PRIVATE METHODS ***************************************************************************** // PRIVATE METHODS *****************************************************************************
private fun observeSyncState() { private fun observeSyncState() {

View file

@ -18,8 +18,10 @@ package im.vector.riotx.features.home
import com.airbnb.mvrx.MvRxState import com.airbnb.mvrx.MvRxState
import im.vector.matrix.android.api.session.sync.SyncState import im.vector.matrix.android.api.session.sync.SyncState
import im.vector.riotx.features.home.room.list.RoomListFragment
data class HomeDetailViewState( data class HomeDetailViewState(
val displayMode: RoomListFragment.DisplayMode = RoomListFragment.DisplayMode.HOME,
val notificationCountCatchup: Int = 0, val notificationCountCatchup: Int = 0,
val notificationHighlightCatchup: Boolean = false, val notificationHighlightCatchup: Boolean = false,
val notificationCountPeople: Int = 0, val notificationCountPeople: Int = 0,