Remember expand status of sections in the overview again

Change-Id: I3648e959de8a12d525d73da642b6ce6ae6a5ba7a
This commit is contained in:
SpiritCroc 2021-04-14 16:31:45 +02:00
parent 2020765b77
commit 0aee3cf68c
3 changed files with 50 additions and 10 deletions

View File

@ -30,4 +30,5 @@ sealed class RoomListAction : VectorViewModelAction {
data class ToggleTag(val roomId: String, val tag: String) : RoomListAction() data class ToggleTag(val roomId: String, val tag: String) : RoomListAction()
data class LeaveRoom(val roomId: String) : RoomListAction() data class LeaveRoom(val roomId: String) : RoomListAction()
data class SetMarkedUnread(val roomId: String, val markedUnread: Boolean) : RoomListAction() data class SetMarkedUnread(val roomId: String, val markedUnread: Boolean) : RoomListAction()
data class SetSectionExpanded(val section: RoomsSection, val expanded: Boolean) : RoomListAction()
} }

View File

@ -17,6 +17,7 @@
package im.vector.app.features.home.room.list package im.vector.app.features.home.room.list
import android.content.DialogInterface import android.content.DialogInterface
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import android.view.LayoutInflater import android.view.LayoutInflater
@ -25,6 +26,7 @@ import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.ConcatAdapter import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -131,6 +133,12 @@ class RoomListFragment @Inject constructor(
} }
} }
override fun onPause() {
super.onPause()
persistExpandStatus()
}
private fun refreshCollapseStates() { private fun refreshCollapseStates() {
var contentInsertIndex = 1 var contentInsertIndex = 1
roomListViewModel.sections.forEachIndexed { index, roomsSection -> roomListViewModel.sections.forEachIndexed { index, roomsSection ->
@ -157,16 +165,6 @@ class RoomListFragment @Inject constructor(
} }
} }
override fun onPause() {
super.onPause()
/* TODO-SC-merge: remember expand state for priority headers
withState(roomListViewModel) {
state -> context?.let { state.persistWithContext(it, roomListParams.displayMode) }
}
*/
}
override fun showFailure(throwable: Throwable) { override fun showFailure(throwable: Throwable) {
showErrorInSnackbar(throwable) showErrorInSnackbar(throwable)
} }
@ -308,6 +306,11 @@ class RoomListFragment @Inject constructor(
this.concatAdapter = concatAdapter this.concatAdapter = concatAdapter
views.roomListView.adapter = concatAdapter views.roomListView.adapter = concatAdapter
// Load initial expand statuses from settings
roomListViewModel.sections.forEach { section ->
roomListViewModel.handle(RoomListAction.SetSectionExpanded(section, shouldInitiallyExpand(section)))
}
} }
private val showFabRunnable = Runnable { private val showFabRunnable = Runnable {
@ -463,4 +466,35 @@ class RoomListFragment @Inject constructor(
override fun createRoom(initialName: String) { override fun createRoom(initialName: String) {
navigator.openCreateRoom(requireActivity(), initialName) navigator.openCreateRoom(requireActivity(), initialName)
} }
// SC addition: remember expanded sections across restarts
companion object {
const val ROOM_LIST_ROOM_EXPANDED_ANY_PREFIX = "ROOM_LIST_ROOM_EXPANDED_"
}
fun persistExpandStatus() {
val spEdit = getSharedPreferences().edit()
roomListViewModel.sections.forEach{section ->
val isExpanded = section.isExpanded.value
if (isExpanded != null) {
val pref = getRoomListExpandedPref(section)
spEdit.putBoolean(pref, isExpanded)
}
}
spEdit.apply()
}
private fun shouldInitiallyExpand(section: RoomsSection): Boolean {
val sp = getSharedPreferences()
val pref = getRoomListExpandedPref(section)
return sp.getBoolean(pref, true)
}
private fun getSharedPreferences(): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
private fun getRoomListExpandedPref(section: RoomsSection): String {
return ROOM_LIST_ROOM_EXPANDED_ANY_PREFIX + section.sectionName + roomListParams.displayMode.toString()
}
} }

View File

@ -189,6 +189,7 @@ class RoomListViewModel @Inject constructor(
is RoomListAction.ChangeRoomNotificationState -> handleChangeNotificationMode(action) is RoomListAction.ChangeRoomNotificationState -> handleChangeNotificationMode(action)
is RoomListAction.ToggleTag -> handleToggleTag(action) is RoomListAction.ToggleTag -> handleToggleTag(action)
is RoomListAction.SetMarkedUnread -> handleSetMarkedUnread(action) is RoomListAction.SetMarkedUnread -> handleSetMarkedUnread(action)
is RoomListAction.SetSectionExpanded -> handleSetSectionExpanded(action.section, action.expanded)
is RoomListAction.ToggleSection -> handleToggleSection(action.section) is RoomListAction.ToggleSection -> handleToggleSection(action.section)
}.exhaustive }.exhaustive
} }
@ -254,6 +255,10 @@ class RoomListViewModel @Inject constructor(
*/ */
} }
private fun handleSetSectionExpanded(roomSection: RoomsSection, expanded: Boolean) {
roomSection.isExpanded.postValue(expanded)
}
private fun handleFilter(action: RoomListAction.FilterWith) { private fun handleFilter(action: RoomListAction.FilterWith) {
setState { setState {
copy( copy(