Start screen survives a rotation, view shortcuts keep forms and come back (#141, #59)

The start destination is worked out in the app scope. A view shortcut no
longer pops an open form, a pin only leaves the screen it was made on, and
a pinned shortcut disabled while its view didn't read is enabled again.
This commit is contained in:
makiolaj committed 2026-11-27 13:59:25 +01:00
1 parent 710cafff3a
commit 1a2c41cc08
4 files changed
+62 -26

No files matched your search

@@ -40,6 +40,7 @@ import de.jeanlucmakiola.agendula.ui.views.ViewSession
import de.jeanlucmakiola.floret.crash.CrashReportDialog
import de.jeanlucmakiola.floret.crash.CrashReporter
import de.jeanlucmakiola.floret.crash.submitCrashReport
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
@@ -87,11 +88,12 @@ class MainActivity : ComponentActivity() {
enableEdgeToEdge()
// Every view is a launcher shortcut, kept up with the views as they change.
lifecycleScope.launch {
settingsPrefs.views
.map { views -> views.map { Triple(it.id, it.name, it.icon) } to views }
.distinctUntilChanged { a, b -> a.first == b.first }
.collect { (_, views) ->
withContext(Dispatchers.Default) { AppShortcuts.publish(this@MainActivity, views) }
combine(settingsPrefs.views, settingsPrefs.storedViewIds) { views, stored -> views to stored }
.distinctUntilChanged { a, b ->
a.second == b.second && a.first.map { Triple(it.id, it.name, it.icon) } == b.first.map { Triple(it.id, it.name, it.icon) }
}
.collect { (views, stored) ->
withContext(Dispatchers.Default) { AppShortcuts.publish(this@MainActivity, views, stored) }
}
}
@@ -110,15 +112,19 @@ class MainActivity : ComponentActivity() {
// A plain launch opens the start destination (#141); anything else opens what it asked for.
if (navRequest == null) {
viewSession.beginStart()
lifecycleScope.launch {
val push = when (val filter = viewSession.start()) {
is TaskFilter.OfList -> NavRequest.OpenList(filter.listId, overHomeOnly = true)
is TaskFilter.Saved -> NavRequest.OpenFilter(filter.id, overHomeOnly = true)
else -> null
}
// Not over something the user, or another intent, opened meanwhile.
if (push != null && !navigatedSinceLaunch) navRequest = push
}
}
// Collected on every creation: a start destination worked out across a rotation still opens.
lifecycleScope.launch {
viewSession.startPush.collect { filter ->
val push = when (filter) {
is TaskFilter.OfList -> NavRequest.OpenList(filter.listId, overHomeOnly = true)
is TaskFilter.Saved -> NavRequest.OpenFilter(filter.id, overHomeOnly = true)
else -> return@collect
}
viewSession.takeStartPush()
// Not over something another intent opened meanwhile; the nav host checks the user's own steps.
if (!navigatedSinceLaunch) navRequest = push
}
}
@@ -110,8 +110,11 @@ fun AgendulaNavHost(
launchSingleTop = true
}
is NavRequest.Import -> nav.navigate(Dest.Import.build(navRequest.uri))
// The view itself was switched to as the request arrived; this only uncovers home.
is NavRequest.OpenView -> nav.popBackStack(Dest.LISTS, inclusive = false)
// The view was switched to as the request arrived. Home is uncovered unless
// a form is open, whose draft that would throw away; then the view waits below it.
is NavRequest.OpenView -> if (nav.currentDestination?.route !in FORMS) {
nav.popBackStack(Dest.LISTS, inclusive = false)
}
}
val returnHome = (navRequest as? NavRequest.NewTask)?.returnHome == true ||
(navRequest as? NavRequest.OpenTask)?.returnHome == true
@@ -138,7 +141,7 @@ fun AgendulaNavHost(
ListsScreen(
onOpenFilter = { filter, scopeViewId -> nav.navigate(Dest.TaskList.build(filter, scopeViewId)) },
onOpenTask = { task -> nav.navigate(task.detailRoute()) },
onNewTask = { listId -> nav.navigate(Dest.TaskEdit.buildNew(presetListId = listId)) },
onNewTask = { listId, tag -> nav.navigate(Dest.TaskEdit.buildNew(presetListId = listId, tag = tag)) },
onNewFilter = { nav.navigate(Dest.FilterEdit.build()) },
onOpenSettings = { nav.navigate(Dest.SETTINGS) },
onManageViews = { nav.navigate(Dest.MANAGE_VIEWS) },
@@ -216,7 +219,10 @@ fun AgendulaNavHost(
},
onEditFilter = { id -> nav.navigate(Dest.FilterEdit.build(id)) },
// The new view shows on home, so the screen it was pinned from goes.
onPinned = { nav.popBackStack(Dest.LISTS, inclusive = false) },
// Only while this screen is still the one shown; the user may have moved on.
onPinned = {
if (nav.currentBackStackEntry?.id == entry.id) nav.popBackStack(Dest.LISTS, inclusive = false)
},
onBack = { nav.popBackStack() },
backClosesSearch = !detailShown,
)
@@ -313,6 +319,9 @@ fun AgendulaNavHost(
}
}
/** Routes holding a draft that leaving would lose. */
private val FORMS = setOf(Dest.TaskEdit.route, Dest.FilterEdit.route, Dest.EditView.route)
/** A task's detail route, pinned to its occurrence when it is one of a series. */
private fun Task.detailRoute(): String = Dest.TaskDetail.build(taskId, occurrenceStart?.toEpochMilliseconds())
@@ -33,6 +33,7 @@ object AppShortcuts {
/** The views last published, for a republish that doesn't come with them; `null` before the first. */
@Volatile private var lastViews: List<View>? = null
@Volatile private var lastStored: Set<Long>? = null
/**
* Publishes the shortcuts with [views]. Without them, before the views were
@@ -40,7 +41,7 @@ object AppShortcuts {
* left as they are, rather than wiped.
*/
@Synchronized
fun publish(context: Context, views: List<View>? = lastViews) {
fun publish(context: Context, views: List<View>? = lastViews, stored: Set<Long>? = lastStored) {
val fixed = listOf(
ShortcutInfoCompat.Builder(context, "new_task")
.setShortLabel(context.getString(R.string.shortcut_new_task))
@@ -60,6 +61,7 @@ object AppShortcuts {
return
}
lastViews = views
lastStored = stored
val room = runCatching { ShortcutManagerCompat.getMaxShortcutCountPerActivity(context) }.getOrDefault(fixed.size)
val all = views.mapIndexed { index, view ->
val label = view.name.ifBlank { context.getString(unnamedViewLabel(view.id)) }
@@ -77,8 +79,14 @@ object AppShortcuts {
.map { it.id }
.filter { it.startsWith("view_") }
.toSet()
val (present, gone) = pinned.partition { id -> all.any { it.id == id } }
if (present.isNotEmpty()) ShortcutManagerCompat.updateShortcuts(context, all.filter { it.id in present })
val (present, missing) = pinned.partition { id -> all.any { it.id == id } }
if (present.isNotEmpty()) {
// One disabled while it didn't read comes back with its view.
ShortcutManagerCompat.enableShortcuts(context, all.filter { it.id in present })
ShortcutManagerCompat.updateShortcuts(context, all.filter { it.id in present })
}
// Only a view no longer stored at all is gone; while the views are another version's, none is.
val gone = if (stored == null) emptyList() else missing.filter { it.removePrefix("view_").toLongOrNull() !in stored }
if (gone.isNotEmpty()) {
ShortcutManagerCompat.disableShortcuts(context, gone, context.getString(R.string.view_shortcut_gone))
}
@@ -44,16 +44,29 @@ class ViewSession @Inject constructor(
appScope.launch { prefs.setActiveView(viewId) }
}
/** Call before the first frame of a fresh launch, then [start]. */
fun beginStart() {
_starting.value = true
private val _startPush = MutableStateFlow<TaskFilter?>(null)
/**
* The list or filter the start destination opens over home, until [takeStartPush].
* Held here so an activity recreated meanwhile still opens it.
*/
val startPush: StateFlow<TaskFilter?> = _startPush.asStateFlow()
fun takeStartPush() {
_startPush.value = null
}
/**
* Opens the start destination: switches to its view, and returns the list or
* filter to open over it, if it is one that still exists.
* Opens the start destination on a fresh launch, before its first frame:
* switches to its view, then sets [startPush] when it is a list or filter
* that still exists. Runs in the app scope, so a rotation doesn't cut it short.
*/
suspend fun start(): TaskFilter? {
fun beginStart() {
_starting.value = true
appScope.launch { _startPush.value = start() }
}
private suspend fun start(): TaskFilter? {
val picked = chosen.value
try {
val destination = prefs.startDestination.first()