Remember low priority expand status across restarts

This commit is contained in:
SpiritCroc 2020-08-19 10:03:53 +02:00
parent 72faa81e2a
commit c01d98d308
3 changed files with 48 additions and 0 deletions

View File

@ -96,6 +96,12 @@ class RoomListFragment @Inject constructor(
super.onPrepareOptionsMenu(menu)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
context?.let { roomListViewModel.initWithContext(it, roomListParams.displayMode) }
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupCreateRoomButton()
@ -118,6 +124,14 @@ class RoomListFragment @Inject constructor(
.disposeOnDestroyView()
}
override fun onPause() {
super.onPause()
withState(roomListViewModel) {
state -> context?.let { state.persistWithContext(it, roomListParams.displayMode) }
}
}
override fun showFailure(throwable: Throwable) {
showErrorInSnackbar(throwable)
}

View File

@ -16,6 +16,7 @@
package im.vector.riotx.features.home.room.list
import android.content.Context
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
@ -61,6 +62,13 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
observeMembershipChanges()
}
fun initWithContext(context: Context, displayMode: RoomListDisplayMode) {
setState {
// RoomListViewState.initWithContext
this.initWithContext(context, displayMode)
}
}
override fun handle(action: RoomListAction) {
when (action) {
is RoomListAction.SelectRoom -> handleSelectRoom(action)

View File

@ -16,7 +16,10 @@
package im.vector.riotx.features.home.room.list
import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.StringRes
import androidx.preference.PreferenceManager
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.Uninitialized
@ -41,8 +44,31 @@ data class RoomListViewState(
val isServerNoticeRoomsExpanded: Boolean = true
) : MvRxState {
companion object {
const val ROOM_LIST_ROOM_EXPANDED_LOW_PRIORITY_PREFIX = "ROOM_LIST_ROOM_EXPANDED_LOW_PRIORITY_"
}
constructor(args: RoomListParams) : this(displayMode = args.displayMode)
private fun getSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
private fun getRoomListExpandedLowPriorityPref(displayMode: RoomListDisplayMode): String {
return ROOM_LIST_ROOM_EXPANDED_LOW_PRIORITY_PREFIX + displayMode.toString()
}
fun initWithContext(context: Context, displayMode: RoomListDisplayMode): RoomListViewState {
val sp = getSharedPreferences(context)
val pref = getRoomListExpandedLowPriorityPref(displayMode)
return copy(isLowPriorityRoomsExpanded = sp.getBoolean(pref, isLowPriorityRoomsExpanded))
}
fun persistWithContext(context: Context, displayMode: RoomListDisplayMode) {
val sp = getSharedPreferences(context)
val pref = getRoomListExpandedLowPriorityPref(displayMode)
sp.edit().putBoolean(pref, isLowPriorityRoomsExpanded).apply()
}
fun isCategoryExpanded(roomCategory: RoomCategory): Boolean {
return when (roomCategory) {
RoomCategory.INVITE -> isInviteExpanded