feature: An option to persist last selected sorting method #464
This commit is contained in:
parent
e857e1fd56
commit
eee65df4b7
@ -307,6 +307,7 @@
|
|||||||
<string name="file_action_view_cipher_title">View parent item</string>
|
<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_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_sync_vault_title">Sync vault</string>
|
||||||
<string name="vault_action_lock_vault_title">Lock vault</string>
|
<string name="vault_action_lock_vault_title">Lock vault</string>
|
||||||
<string name="vault_action_rename_folder_title">Rename folder</string>
|
<string name="vault_action_rename_folder_title">Rename folder</string>
|
||||||
|
@ -137,6 +137,7 @@ import kotlinx.coroutines.flow.onEach
|
|||||||
import kotlinx.coroutines.flow.shareIn
|
import kotlinx.coroutines.flow.shareIn
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import org.kodein.di.DirectDI
|
import org.kodein.di.DirectDI
|
||||||
import org.kodein.di.compose.localDI
|
import org.kodein.di.compose.localDI
|
||||||
import org.kodein.di.direct
|
import org.kodein.di.direct
|
||||||
@ -165,6 +166,16 @@ data class ComparatorHolder(
|
|||||||
favourites = map["favourites"].toString() == "true",
|
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(
|
fun toMap() = mapOf(
|
||||||
@ -369,16 +380,18 @@ fun vaultListScreenState(
|
|||||||
}
|
}
|
||||||
.launchIn(this)
|
.launchIn(this)
|
||||||
|
|
||||||
val showKeyboardSink = if (args.canAlwaysShowKeyboard) {
|
val showKeyboardSink = mutablePersistedFlow(
|
||||||
mutablePersistedFlow(
|
|
||||||
key = "keyboard",
|
key = "keyboard",
|
||||||
storage = storage,
|
storage = if (args.canAlwaysShowKeyboard) {
|
||||||
|
storage
|
||||||
|
} else PersistedStorage.InMemory,
|
||||||
) { false }
|
) { false }
|
||||||
} else {
|
val rememberSortSink = mutablePersistedFlow(
|
||||||
mutablePersistedFlow(
|
key = "sort_persistent_enabled",
|
||||||
key = "keyboard",
|
storage = if (args.canAlwaysShowKeyboard) {
|
||||||
|
storage
|
||||||
|
} else PersistedStorage.InMemory,
|
||||||
) { false }
|
) { false }
|
||||||
}
|
|
||||||
val syncFlow = syncSupervisor
|
val syncFlow = syncSupervisor
|
||||||
.get(AccountTask.SYNC)
|
.get(AccountTask.SYNC)
|
||||||
.map { accounts ->
|
.map { accounts ->
|
||||||
@ -389,23 +402,41 @@ fun vaultListScreenState(
|
|||||||
comparator = AlphabeticalSort,
|
comparator = AlphabeticalSort,
|
||||||
favourites = true,
|
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(
|
val sortSink = mutablePersistedFlow(
|
||||||
key = "sort",
|
key = "sort",
|
||||||
serialize = { json, value ->
|
serialize = ComparatorHolder::serialize,
|
||||||
value.toMap()
|
deserialize = ComparatorHolder::deserialize,
|
||||||
},
|
|
||||||
deserialize = { json, value ->
|
|
||||||
ComparatorHolder.of(value)
|
|
||||||
},
|
|
||||||
) {
|
) {
|
||||||
if (args.sort != null) {
|
if (args.sort != null) {
|
||||||
ComparatorHolder(
|
ComparatorHolder(
|
||||||
comparator = args.sort,
|
comparator = args.sort,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
if (rememberSortSink.value) {
|
||||||
|
sortPersistentSink.value
|
||||||
} else {
|
} else {
|
||||||
sortDefault
|
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
|
var scrollPositionKey: Any? = null
|
||||||
val scrollPositionSink = mutablePersistedFlow<OhOhOh>("scroll_state") { OhOhOh() }
|
val scrollPositionSink = mutablePersistedFlow<OhOhOh>("scroll_state") { OhOhOh() }
|
||||||
@ -488,8 +519,28 @@ fun vaultListScreenState(
|
|||||||
onClick = showKeyboardSink::value::set.partially1(!showKeyboard),
|
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(
|
val actionGroup2Flow = combine(
|
||||||
actionAlwaysShowKeyboardFlow,
|
actionAlwaysShowKeyboardFlow,
|
||||||
|
actionRememberSortingFlow,
|
||||||
) { array ->
|
) { array ->
|
||||||
buildContextItems {
|
buildContextItems {
|
||||||
section {
|
section {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user