The rule editor says when its block was removed, not that the views can't be written (#172)
This commit is contained in:
1 parent
8335ede8f7
commit
aa4d4934d5
4 files changed
+37
-14
No files matched your search
@@ -710,6 +710,9 @@ class SettingsPrefs @Inject constructor(
|
||||
return written
|
||||
}
|
||||
|
||||
/** Whether the views are stored in a form this version can write. */
|
||||
suspend fun writableViews(): Boolean = ViewCodec.writable(dataStore.data.first()[VIEWS])
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
||||
@@ -39,6 +39,7 @@ fun RuleEditScreen(
|
||||
val lists by viewModel.lists.collectAsStateWithLifecycle()
|
||||
val tagsInUse by viewModel.tags.collectAsStateWithLifecycle()
|
||||
val saveFailed by viewModel.saveFailed.collectAsStateWithLifecycle()
|
||||
val blockGone by viewModel.blockGone.collectAsStateWithLifecycle()
|
||||
val save = { viewModel.save(onDone) }
|
||||
val title = stringResource(if (purpose == RulePurpose.SCOPE) R.string.rule_scope_title else R.string.rule_source_title)
|
||||
|
||||
@@ -55,9 +56,9 @@ fun RuleEditScreen(
|
||||
},
|
||||
) {
|
||||
val current = state ?: return@CollapsingScaffold
|
||||
if (saveFailed) {
|
||||
if (saveFailed || blockGone) {
|
||||
Text(
|
||||
stringResource(R.string.view_save_failed),
|
||||
stringResource(if (blockGone) R.string.block_gone_while_editing else R.string.view_save_failed),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier.padding(horizontal = GroupedListInset).padding(bottom = 8.dp),
|
||||
|
||||
@@ -64,9 +64,14 @@ class RuleEditViewModel @Inject constructor(
|
||||
|
||||
private val _saveFailed = MutableStateFlow(false)
|
||||
|
||||
/** Set when the block couldn't be written: the views are another version's, or it is gone. */
|
||||
/** Set when the block couldn't be written: the views are another version's. */
|
||||
val saveFailed: StateFlow<Boolean> = _saveFailed.asStateFlow()
|
||||
|
||||
private val _blockGone = MutableStateFlow(false)
|
||||
|
||||
/** Set when the block was removed while its rules were edited; there is nothing to save them to. */
|
||||
val blockGone: StateFlow<Boolean> = _blockGone.asStateFlow()
|
||||
|
||||
val lists: StateFlow<List<TaskList>> = repository.taskLists()
|
||||
.catch { emit(emptyList()) }
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
|
||||
@@ -157,12 +162,17 @@ class RuleEditViewModel @Inject constructor(
|
||||
is Target.Result -> onDone(if (state.saveAsFilter) asFilter() else rules)
|
||||
is Target.Block -> {
|
||||
var written = writeSource(target, rules.source)
|
||||
if (written && state.saveAsFilter) written = writeSource(target, asFilter().source)
|
||||
if (written) {
|
||||
onDone(null)
|
||||
} else {
|
||||
saving = false
|
||||
_saveFailed.value = true
|
||||
if (written == WriteOutcome.WRITTEN && state.saveAsFilter) written = writeSource(target, asFilter().source)
|
||||
when (written) {
|
||||
WriteOutcome.WRITTEN -> onDone(null)
|
||||
WriteOutcome.GONE -> {
|
||||
saving = false
|
||||
_blockGone.value = true
|
||||
}
|
||||
WriteOutcome.REFUSED -> {
|
||||
saving = false
|
||||
_saveFailed.value = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,13 +181,20 @@ class RuleEditViewModel @Inject constructor(
|
||||
|
||||
private var savedFilterId: Long? = null
|
||||
|
||||
/** Points the block at [source]; false when it can't be written or is gone. */
|
||||
private suspend fun writeSource(target: Target.Block, source: BlockSource): Boolean {
|
||||
private enum class WriteOutcome { WRITTEN, GONE, REFUSED }
|
||||
|
||||
/** Points the block at [source]: written, the block (or its view) is gone, or the views can't be written. */
|
||||
private suspend fun writeSource(target: Target.Block, source: BlockSource): WriteOutcome {
|
||||
var found = false
|
||||
val written = settingsPrefs.updateView(target.viewId) { view ->
|
||||
found = view.blocks.any { it.id == target.blockId }
|
||||
view.copy(blocks = view.blocks.map { if (it.id == target.blockId) it.copy(source = source) else it })
|
||||
}
|
||||
return written && found
|
||||
return when {
|
||||
written && found -> WriteOutcome.WRITTEN
|
||||
// updateView refuses a view it can't find as it does a store it can't write.
|
||||
!written && !settingsPrefs.writableViews() -> WriteOutcome.REFUSED
|
||||
else -> WriteOutcome.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,8 @@ class RuleEditViewModelTest {
|
||||
var done = false
|
||||
vm.save { done = true }
|
||||
assertThat(done).isFalse()
|
||||
assertThat(vm.saveFailed.value).isTrue()
|
||||
assertThat(vm.blockGone.value).isTrue()
|
||||
assertThat(vm.saveFailed.value).isFalse()
|
||||
assertThat(runBlocking { prefs.savedFilters.first() }).isEmpty()
|
||||
}
|
||||
|
||||
@@ -150,7 +151,8 @@ class RuleEditViewModelTest {
|
||||
var done = false
|
||||
vm.save { done = true }
|
||||
assertThat(done).isFalse()
|
||||
assertThat(vm.saveFailed.value).isTrue()
|
||||
assertThat(vm.blockGone.value).isTrue()
|
||||
assertThat(vm.saveFailed.value).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user