Say so when deleting or reordering views can't be stored (#59)

Views stored by another version are left alone, and the last used view and
start screen no longer reset when the delete didn't happen. A view's block
changes in one edit.
This commit is contained in:
makiolaj committed 2026-11-27 13:59:25 +01:00
1 parent 56f81d9c2e
commit 710cafff3a
5 files changed
+86 -10

No files matched your search

@@ -660,18 +660,58 @@ class SettingsPrefs @Inject constructor(
return id
}
/** Deletes view [id], never Home; the last used view and the start destination fall back to Home. */
suspend fun deleteView(id: Long) {
if (id == View.HOME_ID) return
/**
* Deletes view [id], never Home; the last used view and the start destination
* fall back to Home. Returns false when the views are another version's and
* can't be written.
*/
suspend fun deleteView(id: Long): Boolean {
if (id == View.HOME_ID) return false
var written = false
dataStore.edit { p ->
if (!ViewCodec.writable(p[VIEWS])) return@edit
written = true
p[VIEWS] = ViewCodec.remove(p[VIEWS], id)
if (p[ACTIVE_VIEW] == id) p.remove(ACTIVE_VIEW)
if (decodeStart(p[START_DESTINATION], modeOf(p).name) == StartDestination.View(id)) p.remove(START_DESTINATION)
}
return written
}
/** Puts the views in the order of [ids]; Home stays first, as [views] reads it. */
suspend fun reorderViews(ids: List<Long>) = dataStore.edit { p -> p[VIEWS] = ViewCodec.reorder(p[VIEWS], ids) }
/** Puts the views in the order of [ids]; Home stays first, as [views] reads it. False when they can't be written. */
suspend fun reorderViews(ids: List<Long>): Boolean {
var written = false
dataStore.edit { p ->
if (!ViewCodec.writable(p[VIEWS])) return@edit
written = true
p[VIEWS] = ViewCodec.reorder(p[VIEWS], ids)
}
return written
}
/**
* Changes the stored view [id] by [transform], read and written in one edit.
* False when there is no such view or the views can't be written.
*/
suspend fun updateView(id: Long, transform: (View) -> View): Boolean {
var written = false
dataStore.edit { p ->
if (!ViewCodec.writable(p[VIEWS])) return@edit
val mode = modeOf(p).name
val view = ViewCodec.decode(p[VIEWS], mode).firstOrNull { it.id == id } ?: return@edit
p[VIEWS] = ViewCodec.upsert(p[VIEWS], transform(view), mode)
written = true
}
return written
}
/**
* The ids of every stored view, also of one whose line doesn't read; `null`
* while the views are another version's, when nothing can be said to be gone.
*/
val storedViewIds: Flow<Set<Long>?> = dataStore.data.map { p ->
if (ViewCodec.writable(p[VIEWS])) ViewCodec.ids(p[VIEWS]) + View.HOME_ID else null
}.distinctUntilChanged()
/** The view last shown; Home until another is. */
val activeViewId: Flow<Long> = dataStore.data.map { it[ACTIVE_VIEW] ?: View.HOME_ID }.distinctUntilChanged()
@@ -104,8 +104,11 @@ internal object ViewCodec {
/** Whether a layout stored as [raw] can be written: nothing stored, or this version's. */
fun writable(raw: String?): Boolean = Layout.of(raw) != null
/** The highest view id in [raw], read from every view line, also one that doesn't read otherwise. */
fun maxId(raw: String?): Long? = Layout.of(raw)?.chunks?.mapNotNull { it.id }?.maxOrNull()
/** The view ids in [raw], read from every view line, also one that doesn't read otherwise. */
fun ids(raw: String?): Set<Long> = Layout.of(raw)?.chunks?.mapNotNullTo(HashSet()) { it.id }.orEmpty()
/** The highest of [ids]. */
fun maxId(raw: String?): Long? = ids(raw).maxOrNull()
/** [raw] without the view [id]; another version's layout is returned as it is. */
fun remove(raw: String?, id: Long): String {
@@ -13,6 +13,9 @@ import androidx.compose.material.icons.rounded.DragHandle
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.foundation.layout.padding
import de.jeanlucmakiola.floret.components.GroupedListInset
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
@@ -46,6 +49,7 @@ fun ManageViewsScreen(
) {
val views by viewModel.views.collectAsStateWithLifecycle()
val listNames by viewModel.listNames.collectAsStateWithLifecycle()
val reorderFailed by viewModel.reorderFailed.collectAsStateWithLifecycle()
val home = views.firstOrNull()
val others = views.drop(1)
@@ -88,6 +92,14 @@ fun ManageViewsScreen(
onClick = { onEditView(view.id) },
)
}
if (reorderFailed) {
Text(
stringResource(R.string.view_save_failed),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.error,
modifier = Modifier.padding(horizontal = GroupedListInset).padding(top = 8.dp),
)
}
}
}
@@ -40,8 +40,13 @@ class ManageViewsViewModel @Inject constructor(
/** Puts the views in the order of [ids]; Home stays first. */
fun reorder(ids: List<Long>) = viewModelScope.launch {
settingsPrefs.reorderViews(ids)
if (!settingsPrefs.reorderViews(ids)) _reorderFailed.value = true
}
private val _reorderFailed = MutableStateFlow(false)
/** Set once a reorder couldn't be stored: the views are another version's. */
val reorderFailed: StateFlow<Boolean> = _reorderFailed.asStateFlow()
}
/**
@@ -121,8 +126,7 @@ class EditViewViewModel @Inject constructor(
fun delete(onDeleted: () -> Unit) {
val id = _draft.value?.id?.takeIf { it > 0 && it != View.HOME_ID } ?: return
viewModelScope.launch {
settingsPrefs.deleteView(id)
onDeleted()
if (settingsPrefs.deleteView(id)) onDeleted() else _saveFailed.value = true
}
}
}
@@ -114,4 +114,21 @@ class ViewPrefsTest {
assertThat(prefs.saveView(newView(0, "B"))).isNull()
assertThat(store.data.value[ViewsMigration.KEY]).isEqualTo("v2\nview|1|x|HOME|1|")
}
@Test
fun `views change in one edit, and not at all when another version's`() = runBlocking<Unit> {
val id = prefs.saveView(newView(0, "A"))!!
assertThat(prefs.updateView(id) { it.copy(name = "B") }).isTrue()
assertThat(prefs.views.first()[1].name).isEqualTo("B")
assertThat(prefs.updateView(99) { it }).isFalse()
assertThat(prefs.storedViewIds.first()).containsExactly(View.HOME_ID, id)
val store = MemoryDataStore()
val other = SettingsPrefs(store, ProviderResolver(NoProviders))
store.data.value = androidx.datastore.preferences.core.preferencesOf(ViewsMigration.KEY to "v2\nview|5|x|HOME|1|")
assertThat(other.deleteView(5)).isFalse()
assertThat(other.reorderViews(listOf(5L))).isFalse()
assertThat(other.updateView(View.HOME_ID) { it }).isFalse()
assertThat(other.storedViewIds.first()).isNull()
}
}