feature: An option to persist last selected sorting method #464

This commit is contained in:
Artem Chepurnoy 2024-07-15 23:18:26 +03:00
parent e857e1fd56
commit eee65df4b7
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
2 changed files with 69 additions and 17 deletions

View File

@ -307,6 +307,7 @@
<string name="file_action_view_cipher_title">View parent item</string>
<string name="vault_action_always_show_keyboard_title">Always show keyboard</string>
<string name="vault_action_remember_sorting_title">Remember sorting method</string>
<string name="vault_action_sync_vault_title">Sync vault</string>
<string name="vault_action_lock_vault_title">Lock vault</string>
<string name="vault_action_rename_folder_title">Rename folder</string>

View File

@ -137,6 +137,7 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.kodein.di.DirectDI
import org.kodein.di.compose.localDI
import org.kodein.di.direct
@ -165,6 +166,16 @@ data class ComparatorHolder(
favourites = map["favourites"].toString() == "true",
)
}
fun deserialize(
json: Json,
value: Map<String, Any?>,
): ComparatorHolder = of(value)
fun serialize(
json: Json,
value: ComparatorHolder,
): Map<String, Any?> = value.toMap()
}
fun toMap() = mapOf(
@ -369,16 +380,18 @@ fun vaultListScreenState(
}
.launchIn(this)
val showKeyboardSink = if (args.canAlwaysShowKeyboard) {
mutablePersistedFlow(
val showKeyboardSink = mutablePersistedFlow(
key = "keyboard",
storage = storage,
storage = if (args.canAlwaysShowKeyboard) {
storage
} else PersistedStorage.InMemory,
) { false }
} else {
mutablePersistedFlow(
key = "keyboard",
val rememberSortSink = mutablePersistedFlow(
key = "sort_persistent_enabled",
storage = if (args.canAlwaysShowKeyboard) {
storage
} else PersistedStorage.InMemory,
) { false }
}
val syncFlow = syncSupervisor
.get(AccountTask.SYNC)
.map { accounts ->
@ -389,23 +402,41 @@ fun vaultListScreenState(
comparator = AlphabeticalSort,
favourites = true,
)
// Alternative sort sink that is stored on the
// disk storage. Mirrored from the in-memory sink.
val sortPersistentSink = mutablePersistedFlow(
key = "sort_persistent",
storage = storage,
serialize = ComparatorHolder::serialize,
deserialize = ComparatorHolder::deserialize,
) {
sortDefault
}
val sortSink = mutablePersistedFlow(
key = "sort",
serialize = { json, value ->
value.toMap()
},
deserialize = { json, value ->
ComparatorHolder.of(value)
},
serialize = ComparatorHolder::serialize,
deserialize = ComparatorHolder::deserialize,
) {
if (args.sort != null) {
ComparatorHolder(
comparator = args.sort,
)
} else {
if (rememberSortSink.value) {
sortPersistentSink.value
} else {
sortDefault
}
}
}
// Copy the in-memory sorting method into
// the persistent storage. We need it for
// 'Remember sorting method' option to work.
sortSink
.onEach { value ->
sortPersistentSink.value = value
}
.launchIn(screenScope)
var scrollPositionKey: Any? = null
val scrollPositionSink = mutablePersistedFlow<OhOhOh>("scroll_state") { OhOhOh() }
@ -488,8 +519,28 @@ fun vaultListScreenState(
onClick = showKeyboardSink::value::set.partially1(!showKeyboard),
)
}
val actionRememberSortingFlow = rememberSortSink
.map { rememberSorting ->
FlatItemAction(
leading = {
Icon(
Icons.Outlined.SortByAlpha,
null,
)
},
trailing = {
Switch(
checked = rememberSorting,
onCheckedChange = rememberSortSink::value::set,
)
},
title = Res.string.vault_action_remember_sorting_title.wrap(),
onClick = rememberSortSink::value::set.partially1(!rememberSorting),
)
}
val actionGroup2Flow = combine(
actionAlwaysShowKeyboardFlow,
actionRememberSortingFlow,
) { array ->
buildContextItems {
section {