From f99f4a4dc38eb3d06a332a69f2630f2b2c97813b Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 27 Nov 2026 17:37:46 +0100 Subject: [PATCH] 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) --- .../agendula/ui/lists/ListsScreen.kt | 5 +- .../agendula/ui/lists/ListsViewModel.kt | 77 +++++++++++++++++-- .../agendula/ui/navigation/AgendulaNavHost.kt | 5 +- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt index d062da3..ec7f2e8 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt @@ -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, diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt index 4ee3ca0..c5d9c12 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt @@ -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() + private val draftFilters = mutableListOf() + + private fun deleteFilters(ids: List) { + 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(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.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(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" diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt index c325df9..ae18e21 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt @@ -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) }, ) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 60c662c..7857f1f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1204,7 +1204,6 @@ Smart lists Custom rules… Lists, tags, due dates and priority - Custom rules A list in this view’s scope was deleted. That part of the scope now matches nothing. Edit scope @@ -1306,6 +1305,7 @@ A title to group blocks Show tasks from Custom rules + This block was removed meanwhile, so there is nothing to save these rules to. Nothing to show right now Nothing completed yet No text yet