Custom rules from edit mode: a discard takes back filters saved for it, a pending add survives the process, a shortcut to another view ends editing; Edit layout from Edit view only pops while shown (#59)

This commit is contained in:
makiolaj committed 2026-11-27 17:37:46 +01:00
1 parent aa4d4934d5
commit f99f4a4dc3
4 files changed
+78 -11

No files matched your search

@@ -397,13 +397,14 @@ fun ListsScreen(
)
}
if (content == null || !editing) return
// Custom rules come back from their editor for the block being added, or the settings open.
// Custom rules come back from their editor for the block being added, or the settings open;
// taken also outside edit mode, which a process restarted meanwhile has left.
LaunchedEffect(rulesResult) {
val source = rulesResult ?: return@LaunchedEffect
viewModel.takeRulesResult(source)
onRulesResultTaken()
}
if (content == null || !editing) return
if (addingBlock) {
AddBlockPicker(
blocks = content.view.blocks,
@@ -1,5 +1,6 @@
package de.jeanlucmakiola.agendula.ui.lists
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -350,6 +351,7 @@ class ListsViewModel @Inject constructor(
private val manualSync: ManualSync,
private val settingsPrefs: SettingsPrefs,
private val session: ViewSession,
private val savedState: SavedStateHandle,
) : ViewModel() {
/** Pull to sync (#73); off without an account. */
@@ -465,8 +467,26 @@ class ListsViewModel @Inject constructor(
/** Leaves the layout editor keeping what was changed in it. */
fun stopEditingLayout() {
session.stopEditingLayout()
dropDraftFilters()
_blockDraft.value = null
_layoutAtStart.value = null
sessionFilters.clear()
}
// Filters the rule editor saved for this editing session, and for the open block settings:
// a discard, or settings left without Save, takes them back.
private val sessionFilters = mutableListOf<Long>()
private val draftFilters = mutableListOf<Long>()
private fun deleteFilters(ids: List<Long>) {
if (ids.isEmpty()) return
val gone = ids.toList()
viewModelScope.launch { gone.forEach { runCatching { settingsPrefs.deleteFilter(it) } } }
}
private fun dropDraftFilters() {
deleteFilters(draftFilters)
draftFilters.clear()
}
private val _layoutAtStart = MutableStateFlow<LayoutAtStart?>(null)
@@ -474,9 +494,17 @@ class ListsViewModel @Inject constructor(
/** The blocks of the view being edited as they were when its editing began. */
val layoutAtStart: StateFlow<LayoutAtStart?> = _layoutAtStart.asStateFlow()
/** Notes [view]'s blocks as the ones a discard goes back to, once per editing session. */
/**
* Notes [view]'s blocks as the ones a discard goes back to, once per editing
* session. Another view shown mid-session, by a shortcut, ends the session,
* keeping its edits: a discard could no longer say which view it undoes.
*/
fun beginEditing(view: View) {
if (_layoutAtStart.value?.viewId != view.id) _layoutAtStart.value = LayoutAtStart(view.id, view.blocks)
val start = _layoutAtStart.value
when {
start == null -> _layoutAtStart.value = LayoutAtStart(view.id, view.blocks)
start.viewId != view.id -> stopEditingLayout()
}
}
/** Leaves the layout editor and puts the blocks back as they were when it opened. */
@@ -484,6 +512,8 @@ class ListsViewModel @Inject constructor(
val start = _layoutAtStart.value
removedTimer?.cancel()
_removed.value = null
deleteFilters(sessionFilters)
sessionFilters.clear()
stopEditingLayout()
if (start != null) editBlocks(start.viewId) { start.blocks }
}
@@ -553,25 +583,58 @@ class ListsViewModel @Inject constructor(
fun saveDraft() {
val draft = _blockDraft.value ?: return
_blockDraft.value = null
sessionFilters += draftFilters
draftFilters.clear()
editBlocks(draft.viewId) { it.replaced(draft.block) }
}
fun closeDraft() {
dropDraftFilters()
_blockDraft.value = null
}
private var rulesRequest: RulesRequest? = null
/**
* What the custom rules the editor is opened for will go to. Kept in the saved
* state, as the editor's result is, so a block being added survives the process.
*/
private var rulesRequest: RulesRequest?
get() = savedState.get<String>(KEY_RULES_REQUEST)?.let { text ->
when {
text == "draft" -> RulesRequest.Draft
else -> text.removePrefix("add:").split(':').takeIf { it.size == 2 }?.let { (view, type) ->
val blockType = BlockType.entries.firstOrNull { it.name == type }
view.toLongOrNull()?.let { id -> blockType?.let { RulesRequest.Add(id, it) } }
}
}
}
set(value) {
savedState[KEY_RULES_REQUEST] = when (value) {
is RulesRequest.Add -> "add:${value.viewId}:${value.type.name}"
RulesRequest.Draft -> "draft"
null -> null
}
}
/** Notes what the custom rules the editor is opened for will go to. */
fun awaitRulesFor(request: RulesRequest) {
rulesRequest = request
}
/** Puts [source], back from the rule editor, where [awaitRulesFor] said. */
/**
* Puts [source], back from the rule editor, where [awaitRulesFor] said. A
* filter it was saved as goes with what it went to, if that is thrown away.
*/
fun takeRulesResult(source: BlockSource) {
val created = (source as? BlockSource.Filter)?.filterId
when (val request = rulesRequest) {
is RulesRequest.Add -> addBlock(request.viewId, request.type, source)
RulesRequest.Draft -> updateDraft { it.copy(source = source) }
is RulesRequest.Add -> {
addBlock(request.viewId, request.type, source)
created?.let { sessionFilters += it }
}
RulesRequest.Draft -> if (_blockDraft.value != null) {
updateDraft { it.copy(source = source) }
created?.let { draftFilters += it }
}
null -> Unit
}
rulesRequest = null
@@ -609,3 +672,5 @@ class ListsViewModel @Inject constructor(
_writeFailure.value = outcome.asFailure(ListWriteFailure.SAVE)
}
}
private const val KEY_RULES_REQUEST = "rules_request"
@@ -199,8 +199,9 @@ fun AgendulaNavHost(
viewModel = vm,
onEditScope = { scope -> nav.navigate(Dest.RuleEdit.forResult(RulePurpose.SCOPE, scope)) },
onBack = { nav.popBackStack() },
// Home shows the view with its layout open, under whatever led here.
onEditLayout = { nav.popBackStack(Dest.LISTS, inclusive = false) },
// Home shows the view with its layout open, under whatever led here; not if
// another screen was opened from here while the view saved.
onEditLayout = { if (nav.currentBackStackEntry?.id == entry.id) nav.popBackStack(Dest.LISTS, inclusive = false) },
)
}
}
+1 -1
View File
@@ -1204,7 +1204,6 @@
<string name="view_source_smart">Smart lists</string>
<string name="view_source_custom">Custom rules…</string>
<string name="view_source_custom_hint">Lists, tags, due dates and priority</string>
<string name="view_source_custom_label">Custom rules</string>
<string name="view_scope_list_gone">A list in this view’s scope was deleted. That part of the scope now matches nothing.</string>
<string name="view_edit_scope">Edit scope</string>
<!-- A view's scope in one line, built from these pieces -->
@@ -1306,6 +1305,7 @@
<string name="block_heading_hint">A title to group blocks</string>
<string name="block_source_title">Show tasks from</string>
<string name="block_source_rules">Custom rules</string>
<string name="block_gone_while_editing">This block was removed meanwhile, so there is nothing to save these rules to.</string>
<string name="block_empty">Nothing to show right now</string>
<string name="block_recent_empty">Nothing completed yet</string>
<string name="block_heading_empty">No text yet</string>