From 0e859622c6ba4839cf0076c6240b2e27f8f22653 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:07:51 +0100 Subject: [PATCH 1/8] fix(#530): open a widget's day on its own so back returns home --- .../jeanlucmakiola/calendula/MainActivity.kt | 5 +-- .../calendula/ui/CalendarHost.kt | 18 ++++----- .../calendula/ui/WidgetNavRequest.kt | 13 +++--- .../calendula/ui/common/CalendarView.kt | 12 +++++- .../calendula/ui/common/ViewBackStackTest.kt | 40 +++++++++++-------- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index 7069da18..41cc49f4 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -602,9 +602,8 @@ class MainActivity : AppCompatActivity() { } /** - * Open the day view anchored on [date], drilled in over the launching - * widget's [source] view (home-screen widgets). Backing out of the day - * returns to [source], then to the default home view. + * Open the day view anchored on [date] from a home-screen widget of kind + * [source]. Backing out of the day returns to the home screen (#530). */ fun openDateIntent(context: Context, date: LocalDate, source: CalendarView): Intent = Intent(context, MainActivity::class.java).apply { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt index a30ad787..bac3a0e9 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -92,6 +92,7 @@ import de.jeanlucmakiola.calendula.ui.common.LocalTimelineZoom import de.jeanlucmakiola.calendula.ui.common.ViewMorphScope import de.jeanlucmakiola.calendula.ui.common.rememberCalendarFadeSpec import de.jeanlucmakiola.calendula.ui.common.rememberViewMorphBoundsTransform +import de.jeanlucmakiola.calendula.ui.common.dateLaunchStack import de.jeanlucmakiola.calendula.ui.common.drillToDay import de.jeanlucmakiola.calendula.ui.common.drillToWeek import de.jeanlucmakiola.calendula.ui.common.rememberCalendarSlideSpec @@ -128,8 +129,9 @@ import kotlin.time.Instant * (see [selectView]); a widget launch resets the stack to its own view; a date * tap drills the day view on top. Pressing back pops one level, and the * base-level [BackHandler] hands off to the system to exit once only the home - * view remains. So back from a widget-opened screen returns to that widget's - * view, then home; and back through pill switches walks the views in reverse. + * view remains. So back from a widget-opened event returns to that widget's + * view, then home; back from a widget-opened day leaves the app at once; and + * back through pill switches walks the views in reverse. * * [requests] carries what notifications, widgets and other apps asked to * open. A tapped reminder's occurrence opens the detail overlay exactly like an @@ -403,8 +405,8 @@ fun CalendarHost( // A home-screen widget launch asks to open a date (→ day view), open an // event's detail, or start a create. Handled once and cleared, mirroring - // the detail request. Date/event opens root the stack in the widget's own - // view so backing out returns there (then home), not to the default. + // the detail request. An event open roots the stack in the widget's own + // view; a date open shows the day alone, so back leaves the app (#530). val widgetNavRequest = requests.nav LaunchedEffect(widgetNavRequest) { if (widgetNavRequest != null) { @@ -413,13 +415,11 @@ fun CalendarHost( } when (val req = widgetNavRequest) { is WidgetNavRequest.OpenDate -> { - // Drill the day view in over the widget's view: drop any overlay - // that would cover it, so the open doesn't land under Settings/form. + // Drop any overlay that would cover the day, so the open doesn't + // land under Settings/form. dismissCoveringOverlays() focus.navigateTo(LocalDate.parse(req.dateIso)) - // No widget source (an external date tap) roots over the default - // home view, so backing out of the day returns home then exits. - viewStack = viewBaseStack(defaultView, req.source ?: defaultView).drillToDay() + viewStack = dateLaunchStack(defaultView, req.source) requests.nav = null } is WidgetNavRequest.OpenEvent -> { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt index 985806bd..5e92c213 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt @@ -7,17 +7,16 @@ import de.jeanlucmakiola.calendula.ui.common.CalendarView * Parsed from the launch intent in MainActivity and consumed once by * [CalendarHost]. Every request carries the [source] view of the widget it came * from (the agenda widget → [CalendarView.Agenda], the month widget → - * [CalendarView.Month]) so the in-app back stack roots itself in that view: - * backing out of the opened date/event returns to the widget's own view, not the - * default home. (Reminder notifications are not widgets — they keep the separate + * [CalendarView.Month]); an opened event roots the in-app back stack in that + * view, so backing out returns there rather than to the default home. (Reminder notifications are not widgets — they keep the separate * detail-key channel and leave the base view untouched.) */ sealed interface WidgetNavRequest { /** - * Open the day view anchored on [dateIso] (an ISO `yyyy-MM-dd` date), over - * [source]. A null [source] means the request came from outside the app (a - * launcher/clock date tap, issue #9) rather than a widget, so it roots over - * the default home view instead of a widget's view. + * Open the day view anchored on [dateIso] (an ISO `yyyy-MM-dd` date). From a + * widget ([source] non-null) the day stands alone, so back returns to the home + * screen (#530). A null [source] means a launcher/clock date tap (issue #9) or + * the daily summary, which root it over the default home view. */ data class OpenDate(val dateIso: String, val source: CalendarView?) : WidgetNavRequest diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt index 7715ee27..1938937e 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt @@ -103,8 +103,8 @@ data class QuickSwitchConfig( } /** - * The top-level view back stack (bottom → top): the [default] home view always - * sits at the bottom. Pressing back pops one level until only the home view + * The top-level view back stack (bottom → top): the [default] home view sits at + * the bottom, except for a day opened from a widget (see [dateLaunchStack]). Pressing back pops one level until only the home view * remains, then the system exits the app. Three operations move it: * * - [selectView] — a lateral switch (the pill or the drawer). It keeps a visit @@ -126,6 +126,14 @@ fun List.selectView(target: CalendarView): List { fun viewBaseStack(default: CalendarView, source: CalendarView): List = if (source == default) listOf(default) else listOf(default, source) +/** + * The stack an outside date open resets to. From a widget ([source] non-null) + * the day view stands alone, so back returns to the home screen (#530); a + * launcher/clock or daily-summary tap drills it in over the [default] home view. + */ +fun dateLaunchStack(default: CalendarView, source: CalendarView?): List = + if (source != null) listOf(CalendarView.Day) else listOf(default).drillToDay() + /** Push the day view as a drill-in over the current stack (no-op if already on it). */ fun List.drillToDay(): List = if (lastOrNull() == CalendarView.Day) this else this + CalendarView.Day diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt index aae8d28d..56bdd4fa 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt @@ -6,8 +6,8 @@ import org.junit.jupiter.api.Test /** * The pure back-stack rules behind [CalendarHost]'s top-level navigation: * the home view is always the bottom, a lateral move keeps the stack one deep, - * and a date tap drills the day view on top. Verifies the back sequences the - * widget-UX fix promises (e.g. agenda widget → day → back → agenda → back → home). + * and a date tap drills the day view on top, except a widget's, which opens the + * day alone so back returns to the home screen (#530). */ class ViewBackStackTest { @@ -74,23 +74,31 @@ class ViewBackStackTest { } @Test - fun `agenda widget date tap backs out agenda then home`() { - // openDate(source = Agenda): root in Agenda over the Week home, then drill. - val stack = viewBaseStack(CalendarView.Week, CalendarView.Agenda).drillToDay() - assertThat(stack) - .containsExactly(CalendarView.Week, CalendarView.Agenda, CalendarView.Day) - .inOrder() - // Pressing back pops one level at a time down to the home view. - assertThat(stack.dropLast(1)).containsExactly(CalendarView.Week, CalendarView.Agenda).inOrder() - assertThat(stack.dropLast(2)).containsExactly(CalendarView.Week) + fun `a widget date tap opens the day alone so back leaves the app`() { + // #530: from any widget, the day is the whole stack. + assertThat(dateLaunchStack(CalendarView.Week, CalendarView.Month)) + .containsExactly(CalendarView.Day) + assertThat(dateLaunchStack(CalendarView.Week, CalendarView.Agenda)) + .containsExactly(CalendarView.Day) } @Test - fun `month widget date tap backs out month then home`() { - val stack = viewBaseStack(CalendarView.Week, CalendarView.Month).drillToDay() - assertThat(stack.last()).isEqualTo(CalendarView.Day) - assertThat(stack.dropLast(1).last()).isEqualTo(CalendarView.Month) - assertThat(stack.first()).isEqualTo(CalendarView.Week) + fun `an outside date tap drills the day over the home view`() { + assertThat(dateLaunchStack(CalendarView.Week, null)) + .containsExactly(CalendarView.Week, CalendarView.Day) + .inOrder() + assertThat(dateLaunchStack(CalendarView.Day, null)).containsExactly(CalendarView.Day) + } + + @Test + fun `switching views from a widget-opened day retraces back to it, then out`() { + val stack = dateLaunchStack(CalendarView.Week, CalendarView.Month) + .selectView(CalendarView.Month) + .selectView(CalendarView.Week) + assertThat(stack) + .containsExactly(CalendarView.Day, CalendarView.Month, CalendarView.Week) + .inOrder() + assertThat(stack.selectView(CalendarView.Day)).containsExactly(CalendarView.Day) } @Test From 3039d48aa46dbb81c77ad27128af79f810b55501 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:12:38 +0100 Subject: [PATCH 2/8] fix(#530): keep the home view under a widget's day, finish on back --- .../jeanlucmakiola/calendula/MainActivity.kt | 8 ++-- .../calendula/ui/CalendarHost.kt | 23 +++++++---- .../calendula/ui/WidgetNavRequest.kt | 19 ++++----- .../calendula/ui/common/CalendarView.kt | 16 ++------ .../calendula/ui/common/ViewBackStackTest.kt | 41 ++++++++----------- 5 files changed, 49 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index 41cc49f4..d1dc3d55 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -456,9 +456,9 @@ class MainActivity : AppCompatActivity() { } private fun Intent.navRequestOrNull(): WidgetNavRequest? { - // An external date tap (launcher/clock) has no widget source, so it opens - // the day view rooted over the default home view (OpenDate source = null). - calendarTimeDateOrNull()?.let { return WidgetNavRequest.OpenDate(it.toString(), source = null) } + // An external date tap (launcher/clock) opens the day view over the default + // home view, and backs out to it. + calendarTimeDateOrNull()?.let { return WidgetNavRequest.OpenDate(it.toString(), fromWidget = false) } // A widget header tap: open a top-level view with no date drill-in. The // empty string carried by [openViewIntent] means "the default home view". if (hasExtra(EXTRA_OPEN_VIEW)) { @@ -482,7 +482,7 @@ class MainActivity : AppCompatActivity() { source = source, ) source != null && getStringExtra(EXTRA_DATE_ISO) != null -> - WidgetNavRequest.OpenDate(getStringExtra(EXTRA_DATE_ISO)!!, source) + WidgetNavRequest.OpenDate(getStringExtra(EXTRA_DATE_ISO)!!, fromWidget = true) else -> null } } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt index bac3a0e9..2439c9e2 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -1,6 +1,7 @@ package de.jeanlucmakiola.calendula.ui import androidx.activity.compose.BackHandler +import androidx.activity.compose.LocalActivity import androidx.activity.compose.PredictiveBackHandler import androidx.compose.animation.AnimatedContent import androidx.compose.animation.AnimatedVisibility @@ -92,7 +93,6 @@ import de.jeanlucmakiola.calendula.ui.common.LocalTimelineZoom import de.jeanlucmakiola.calendula.ui.common.ViewMorphScope import de.jeanlucmakiola.calendula.ui.common.rememberCalendarFadeSpec import de.jeanlucmakiola.calendula.ui.common.rememberViewMorphBoundsTransform -import de.jeanlucmakiola.calendula.ui.common.dateLaunchStack import de.jeanlucmakiola.calendula.ui.common.drillToDay import de.jeanlucmakiola.calendula.ui.common.drillToWeek import de.jeanlucmakiola.calendula.ui.common.rememberCalendarSlideSpec @@ -130,8 +130,9 @@ import kotlin.time.Instant * tap drills the day view on top. Pressing back pops one level, and the * base-level [BackHandler] hands off to the system to exit once only the home * view remains. So back from a widget-opened event returns to that widget's - * view, then home; back from a widget-opened day leaves the app at once; and - * back through pill switches walks the views in reverse. + * view, then home; back from a widget-opened day leaves the app at once (#530), + * until the user navigates on from it; and back through pill switches walks the + * views in reverse. * * [requests] carries what notifications, widgets and other apps asked to * open. A tapped reminder's occurrence opens the detail overlay exactly like an @@ -166,6 +167,8 @@ fun CalendarHost( // True after backing out of a day opened from the view now shown, so the // agenda returns to where it was instead of today (#367). var returnedFromDay by remember { mutableStateOf(false) } + // A day opened from a widget leaves the app on back (#530), until the user moves on in it. + var exitOnBack by rememberSaveable { mutableStateOf(false) } val view = viewStack.last() // The date every view opens on and carries along (#184); tapping a day // anywhere focuses it and opens the day view there. @@ -184,6 +187,7 @@ fun CalendarHost( val switchTo: ((List) -> List) -> Unit = { move -> navigationVersion++ returnedFromDay = false + exitOnBack = false viewStack = move(viewStack) } val onSelectView: (CalendarView) -> Unit = { target -> switchTo { it.selectView(target) } } @@ -405,13 +409,14 @@ fun CalendarHost( // A home-screen widget launch asks to open a date (→ day view), open an // event's detail, or start a create. Handled once and cleared, mirroring - // the detail request. An event open roots the stack in the widget's own - // view; a date open shows the day alone, so back leaves the app (#530). + // the detail request. An event open roots the stack in the widget's own view + // so backing out returns there (then home); a widget's day leaves on back. val widgetNavRequest = requests.nav LaunchedEffect(widgetNavRequest) { if (widgetNavRequest != null) { navigationVersion++ returnedFromDay = false + exitOnBack = false } when (val req = widgetNavRequest) { is WidgetNavRequest.OpenDate -> { @@ -419,7 +424,8 @@ fun CalendarHost( // land under Settings/form. dismissCoveringOverlays() focus.navigateTo(LocalDate.parse(req.dateIso)) - viewStack = dateLaunchStack(defaultView, req.source) + viewStack = listOf(defaultView).drillToDay() + exitOnBack = req.fromWidget requests.nav = null } is WidgetNavRequest.OpenEvent -> { @@ -534,7 +540,10 @@ fun CalendarHost( val viewTransition = remember { SeekableTransitionState(view) } LaunchedEffect(view) { viewTransition.animateTo(view) } val reduceMotion = rememberReduceMotion() - val viewBackEnabled = !anyOverlayVisible && viewStack.size > 1 + val viewBackEnabled = !anyOverlayVisible && viewStack.size > 1 && !exitOnBack + // Finished rather than sent to the background, so the next launch opens on the home view. + val activity = LocalActivity.current + BackHandler(enabled = !anyOverlayVisible && exitOnBack) { activity?.finish() } if (reduceMotion) { BackHandler(enabled = viewBackEnabled) { navigationVersion++ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt index 5e92c213..22198ede 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt @@ -5,20 +5,19 @@ import de.jeanlucmakiola.calendula.ui.common.CalendarView /** * A navigation a home-screen widget asked the app to perform when launched. * Parsed from the launch intent in MainActivity and consumed once by - * [CalendarHost]. Every request carries the [source] view of the widget it came - * from (the agenda widget → [CalendarView.Agenda], the month widget → - * [CalendarView.Month]); an opened event roots the in-app back stack in that - * view, so backing out returns there rather than to the default home. (Reminder notifications are not widgets — they keep the separate - * detail-key channel and leave the base view untouched.) + * [CalendarHost]. An event open carries the [OpenEvent.source] view of the + * widget it came from (the agenda widget → [CalendarView.Agenda]) so backing out + * returns to that view, not the default home. (Reminder notifications are not + * widgets — they keep the separate detail-key channel and leave the base view + * untouched.) */ sealed interface WidgetNavRequest { /** - * Open the day view anchored on [dateIso] (an ISO `yyyy-MM-dd` date). From a - * widget ([source] non-null) the day stands alone, so back returns to the home - * screen (#530). A null [source] means a launcher/clock date tap (issue #9) or - * the daily summary, which root it over the default home view. + * Open the day view anchored on [dateIso] (an ISO `yyyy-MM-dd` date), over the + * default home view. [fromWidget] makes back leave the app at once (#530); a + * launcher/clock date tap (issue #9) or the daily summary backs out to home. */ - data class OpenDate(val dateIso: String, val source: CalendarView?) : WidgetNavRequest + data class OpenDate(val dateIso: String, val fromWidget: Boolean) : WidgetNavRequest /** Open one occurrence's detail (an agenda-widget event tap), over [source]. */ data class OpenEvent( diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt index 1938937e..34d0d449 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt @@ -103,8 +103,8 @@ data class QuickSwitchConfig( } /** - * The top-level view back stack (bottom → top): the [default] home view sits at - * the bottom, except for a day opened from a widget (see [dateLaunchStack]). Pressing back pops one level until only the home view + * The top-level view back stack (bottom → top): the [default] home view always + * sits at the bottom. Pressing back pops one level until only the home view * remains, then the system exits the app. Three operations move it: * * - [selectView] — a lateral switch (the pill or the drawer). It keeps a visit @@ -115,8 +115,8 @@ data class QuickSwitchConfig( * - [viewBaseStack] — the stack a widget launch resets to: just `[default]` when * the widget's view is the home view, else `[default, source]`. A widget entry * starts a fresh context rather than extending in-app history. - * - [drillToDay] — pushes the day view on top (a date tap from the month grid or - * a widget), unless the day view is already current. + * - [drillToDay] — pushes the day view on top (a date tap from the month grid, a + * widget or a launcher), unless the day view is already current. */ fun List.selectView(target: CalendarView): List { val existing = indexOf(target) @@ -126,14 +126,6 @@ fun List.selectView(target: CalendarView): List { fun viewBaseStack(default: CalendarView, source: CalendarView): List = if (source == default) listOf(default) else listOf(default, source) -/** - * The stack an outside date open resets to. From a widget ([source] non-null) - * the day view stands alone, so back returns to the home screen (#530); a - * launcher/clock or daily-summary tap drills it in over the [default] home view. - */ -fun dateLaunchStack(default: CalendarView, source: CalendarView?): List = - if (source != null) listOf(CalendarView.Day) else listOf(default).drillToDay() - /** Push the day view as a drill-in over the current stack (no-op if already on it). */ fun List.drillToDay(): List = if (lastOrNull() == CalendarView.Day) this else this + CalendarView.Day diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt index 56bdd4fa..36d02d2a 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt @@ -6,8 +6,8 @@ import org.junit.jupiter.api.Test /** * The pure back-stack rules behind [CalendarHost]'s top-level navigation: * the home view is always the bottom, a lateral move keeps the stack one deep, - * and a date tap drills the day view on top, except a widget's, which opens the - * day alone so back returns to the home screen (#530). + * and a date tap drills the day view on top. Verifies the back sequences, e.g. + * agenda → day → back → agenda → back → home. */ class ViewBackStackTest { @@ -74,31 +74,22 @@ class ViewBackStackTest { } @Test - fun `a widget date tap opens the day alone so back leaves the app`() { - // #530: from any widget, the day is the whole stack. - assertThat(dateLaunchStack(CalendarView.Week, CalendarView.Month)) - .containsExactly(CalendarView.Day) - assertThat(dateLaunchStack(CalendarView.Week, CalendarView.Agenda)) - .containsExactly(CalendarView.Day) - } - - @Test - fun `an outside date tap drills the day over the home view`() { - assertThat(dateLaunchStack(CalendarView.Week, null)) - .containsExactly(CalendarView.Week, CalendarView.Day) - .inOrder() - assertThat(dateLaunchStack(CalendarView.Day, null)).containsExactly(CalendarView.Day) - } - - @Test - fun `switching views from a widget-opened day retraces back to it, then out`() { - val stack = dateLaunchStack(CalendarView.Week, CalendarView.Month) - .selectView(CalendarView.Month) - .selectView(CalendarView.Week) + fun `a day drilled over a widget's view backs out to it, then home`() { + val stack = viewBaseStack(CalendarView.Week, CalendarView.Agenda).drillToDay() assertThat(stack) - .containsExactly(CalendarView.Day, CalendarView.Month, CalendarView.Week) + .containsExactly(CalendarView.Week, CalendarView.Agenda, CalendarView.Day) .inOrder() - assertThat(stack.selectView(CalendarView.Day)).containsExactly(CalendarView.Day) + // Pressing back pops one level at a time down to the home view. + assertThat(stack.dropLast(1)).containsExactly(CalendarView.Week, CalendarView.Agenda).inOrder() + assertThat(stack.dropLast(2)).containsExactly(CalendarView.Week) + } + + @Test + fun `a day drilled over the month backs out to it, then home`() { + val stack = viewBaseStack(CalendarView.Week, CalendarView.Month).drillToDay() + assertThat(stack.last()).isEqualTo(CalendarView.Day) + assertThat(stack.dropLast(1).last()).isEqualTo(CalendarView.Month) + assertThat(stack.first()).isEqualTo(CalendarView.Week) } @Test From ed4129580b98a9a9bff5780a725caecafac73f41 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:17:45 +0100 Subject: [PATCH 3/8] fix(#530): arm the exit only for a widget's day, skip it on replay --- .../jeanlucmakiola/calendula/MainActivity.kt | 11 +++++---- .../calendula/ui/CalendarHost.kt | 23 +++++++++++-------- .../calendula/ui/common/CalendarView.kt | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index d1dc3d55..21db94a9 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -150,7 +150,9 @@ class MainActivity : AppCompatActivity() { // specific event is what made a single reminder or widget tap re-open // that event on every later launch, an error screen once the event had // been deleted (#309); the user left it long ago, and the activity - // restores where they actually were along with its saved state. + // restores where they actually were along with its saved state. A date + // open is held back the same way, so a finished widget day (#530) comes + // back from Recents on the home view. // // Only those channels are held back. An .ics import or a prefilled // create form keeps no saved state of its own, so re-reading the intent @@ -163,7 +165,7 @@ class MainActivity : AppCompatActivity() { requests.editKey = intent.editEventKeyOrNull() } requests.nav = intent.navRequestOrNull() - ?.takeUnless { replaying && it is WidgetNavRequest.OpenEvent } + ?.takeUnless { replaying && (it is WidgetNavRequest.OpenEvent || it is WidgetNavRequest.OpenDate) } requests.importUri = intent.importUriOrNull() val insertOpenBefore = savedInstanceState?.getBoolean(KEY_INSERT_FORM_OPEN) == true requests.insert = intent.createRequestOrNull()?.takeIf { !replaying || insertOpenBefore } @@ -602,8 +604,9 @@ class MainActivity : AppCompatActivity() { } /** - * Open the day view anchored on [date] from a home-screen widget of kind - * [source]. Backing out of the day returns to the home screen (#530). + * Open the day view anchored on [date] from a home-screen widget; backing + * out of the day returns to the home screen (#530). [source] only keeps + * the PendingIntents of different widget kinds apart. */ fun openDateIntent(context: Context, date: LocalDate, source: CalendarView): Intent = Intent(context, MainActivity::class.java).apply { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt index 2439c9e2..d73e4191 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -126,10 +126,10 @@ import kotlin.time.Instant * * The stack's bottom is the user's [CalendarHostViewModel.defaultView] home view. * A lateral switch (pill / drawer) builds a visit history so back retraces it - * (see [selectView]); a widget launch resets the stack to its own view; a date - * tap drills the day view on top. Pressing back pops one level, and the - * base-level [BackHandler] hands off to the system to exit once only the home - * view remains. So back from a widget-opened event returns to that widget's + * (see [selectView]); a widget event or header launch resets the stack to the + * widget's view; a date tap or date launch drills the day view on top. Pressing + * back pops one level, and the base-level [BackHandler] hands off to the system + * to exit once only the home view remains. So back from a widget-opened event returns to that widget's * view, then home; back from a widget-opened day leaves the app at once (#530), * until the user navigates on from it; and back through pill switches walks the * views in reverse. @@ -187,8 +187,9 @@ fun CalendarHost( val switchTo: ((List) -> List) -> Unit = { move -> navigationVersion++ returnedFromDay = false - exitOnBack = false - viewStack = move(viewStack) + val moved = move(viewStack) + if (moved != viewStack) exitOnBack = false + viewStack = moved } val onSelectView: (CalendarView) -> Unit = { target -> switchTo { it.selectView(target) } } val onOpenDay: (LocalDate) -> Unit = { date -> @@ -416,7 +417,6 @@ fun CalendarHost( if (widgetNavRequest != null) { navigationVersion++ returnedFromDay = false - exitOnBack = false } when (val req = widgetNavRequest) { is WidgetNavRequest.OpenDate -> { @@ -433,6 +433,7 @@ fun CalendarHost( // detail over it (same key shape as a tapped event / reminder). dismissCoveringOverlays() viewStack = viewBaseStack(defaultView, req.source) + exitOnBack = false detailKey = longArrayOf(req.eventId, req.beginMillis, req.endMillis) requests.nav = null } @@ -444,6 +445,7 @@ fun CalendarHost( dismissCoveringOverlays() focus.navigateTo(currentDate()) viewStack = viewBaseStack(defaultView, req.view ?: defaultView) + exitOnBack = false requests.nav = null } is WidgetNavRequest.Create -> { @@ -540,10 +542,11 @@ fun CalendarHost( val viewTransition = remember { SeekableTransitionState(view) } LaunchedEffect(view) { viewTransition.animateTo(view) } val reduceMotion = rememberReduceMotion() - val viewBackEnabled = !anyOverlayVisible && viewStack.size > 1 && !exitOnBack - // Finished rather than sent to the background, so the next launch opens on the home view. val activity = LocalActivity.current - BackHandler(enabled = !anyOverlayVisible && exitOnBack) { activity?.finish() } + val leaveOnBack = !anyOverlayVisible && exitOnBack && activity != null + val viewBackEnabled = !anyOverlayVisible && viewStack.size > 1 && !leaveOnBack + // Finished rather than sent to the background, so the next launch opens on the home view. + BackHandler(enabled = leaveOnBack) { activity?.finish() } if (reduceMotion) { BackHandler(enabled = viewBackEnabled) { navigationVersion++ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt index 34d0d449..256ae93b 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarView.kt @@ -112,7 +112,7 @@ data class QuickSwitchConfig( * top; switching to one already in the stack pops back to it (collapsing the * loop, so the depth stays bounded by the number of distinct views). Selecting * the home view collapses to just `[default]`. - * - [viewBaseStack] — the stack a widget launch resets to: just `[default]` when + * - [viewBaseStack] — the stack a widget event or header launch resets to: just `[default]` when * the widget's view is the home view, else `[default, source]`. A widget entry * starts a fresh context rather than extending in-app history. * - [drillToDay] — pushes the day view on top (a date tap from the month grid, a From 2970ca08d0caa313cd1744d7979fa914e85c89c7 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:30:48 +0100 Subject: [PATCH 4/8] fix(#530): replay a date open that was never handled --- .../de/jeanlucmakiola/calendula/MainActivity.kt | 17 +++++++++++------ .../calendula/ui/common/ViewBackStackTest.kt | 8 ++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index 21db94a9..df6c0b2a 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -151,8 +151,8 @@ class MainActivity : AppCompatActivity() { // that event on every later launch, an error screen once the event had // been deleted (#309); the user left it long ago, and the activity // restores where they actually were along with its saved state. A date - // open is held back the same way, so a finished widget day (#530) comes - // back from Recents on the home view. + // open is held back the same way once handled, so a finished widget day + // (#530) comes back from Recents on the home view. // // Only those channels are held back. An .ics import or a prefilled // create form keeps no saved state of its own, so re-reading the intent @@ -164,8 +164,10 @@ class MainActivity : AppCompatActivity() { requests.detailKey = intent.detailKeyOrNull() ?: intent.viewEventKeyOrNull() requests.editKey = intent.editEventKeyOrNull() } - requests.nav = intent.navRequestOrNull() - ?.takeUnless { replaying && (it is WidgetNavRequest.OpenEvent || it is WidgetNavRequest.OpenDate) } + val navPendingBefore = savedInstanceState?.getBoolean(KEY_NAV_PENDING) == true + requests.nav = intent.navRequestOrNull()?.takeUnless { + replaying && (it is WidgetNavRequest.OpenEvent || (it is WidgetNavRequest.OpenDate && !navPendingBefore)) + } requests.importUri = intent.importUriOrNull() val insertOpenBefore = savedInstanceState?.getBoolean(KEY_INSERT_FORM_OPEN) == true requests.insert = intent.createRequestOrNull()?.takeIf { !replaying || insertOpenBefore } @@ -291,6 +293,7 @@ class MainActivity : AppCompatActivity() { super.onSaveInstanceState(outState) outState.putBoolean(KEY_INSERT_FORM_OPEN, insertFormOpen) outState.putBoolean(KEY_LAUNCHED_FOR_INSERT, launchedForInsert) + outState.putBoolean(KEY_NAV_PENDING, requests.nav != null) } override fun onNewIntent(intent: Intent) { @@ -566,6 +569,7 @@ class MainActivity : AppCompatActivity() { private val CALENDAR_EXTENSIONS = listOf(".ics", ".vcs") private const val KEY_INSERT_FORM_OPEN = "insert_form_open" private const val KEY_LAUNCHED_FOR_INSERT = "launched_for_insert" + private const val KEY_NAV_PENDING = "nav_pending" private const val EXTRA_EVENT_ID = "de.jeanlucmakiola.calendula.extra.EVENT_ID" private const val EXTRA_BEGIN_MILLIS = "de.jeanlucmakiola.calendula.extra.BEGIN" @@ -605,8 +609,9 @@ class MainActivity : AppCompatActivity() { /** * Open the day view anchored on [date] from a home-screen widget; backing - * out of the day returns to the home screen (#530). [source] only keeps - * the PendingIntents of different widget kinds apart. + * out of the day returns to the home screen (#530). [source] marks the + * launch as a widget's and keeps different widget kinds' PendingIntents + * apart; its value doesn't change where back goes. */ fun openDateIntent(context: Context, date: LocalDate, source: CalendarView): Intent = Intent(context, MainActivity::class.java).apply { diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt index 36d02d2a..6558671f 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt @@ -92,6 +92,14 @@ class ViewBackStackTest { assertThat(stack.first()).isEqualTo(CalendarView.Week) } + @Test + fun `a date launch drills the day over the home view only`() { + assertThat(listOf(CalendarView.Week).drillToDay()) + .containsExactly(CalendarView.Week, CalendarView.Day) + .inOrder() + assertThat(listOf(CalendarView.Day).drillToDay()).containsExactly(CalendarView.Day) + } + @Test fun `a widget whose view is the home view does not duplicate the home`() { // Default = Agenda, agenda widget event tap → just the home, no extra layer. From cd7474f314d853a5ab76c5424f2919423da82a93 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 15:18:48 +0100 Subject: [PATCH 5/8] fix(#530): review round 3 - widget day backs to home warm, pending nav restored as sent --- .../jeanlucmakiola/calendula/MainActivity.kt | 55 ++++++++++++------- .../calendula/ui/CalendarHost.kt | 25 +++++---- .../calendula/ui/ExternalRequests.kt | 11 +++- .../calendula/ui/WidgetNavRequest.kt | 46 ++++++++++++++++ .../calendula/widget/WidgetParts.kt | 4 +- .../calendula/widget/date/DateWidget.kt | 3 +- .../calendula/widget/month/MonthGridViews.kt | 3 +- .../calendula/widget/split/MonthDayWidget.kt | 2 +- .../calendula/widget/today/TodayWidget.kt | 2 +- .../calendula/widget/week/WeekStripWidget.kt | 3 +- .../calendula/ui/WidgetNavRequestTest.kt | 49 +++++++++++++++++ .../calendula/ui/common/ViewBackStackTest.kt | 11 +--- 12 files changed, 162 insertions(+), 52 deletions(-) create mode 100644 app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index df6c0b2a..1adcf620 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -48,6 +48,8 @@ import de.jeanlucmakiola.calendula.ui.common.rememberTimelineZoom import de.jeanlucmakiola.calendula.ui.common.LocalSoftenColors import de.jeanlucmakiola.calendula.ui.common.LocalUse24HourFormat import de.jeanlucmakiola.calendula.ui.WidgetNavRequest +import de.jeanlucmakiola.calendula.ui.decodeWidgetNavRequest +import de.jeanlucmakiola.calendula.ui.encode import de.jeanlucmakiola.calendula.ui.common.CalendarView import de.jeanlucmakiola.calendula.ui.detail.EventDetailViewModel.Companion.NO_OCCURRENCE_TIME import de.jeanlucmakiola.calendula.ui.edit.ImportSource @@ -92,6 +94,7 @@ class MainActivity : AppCompatActivity() { insertFormOpen = false if (launchedForInsert) finish() }, + onLeave = { moveTaskToBack(true) }, ) // Whether such a form is still open. Only then does a recreation re-read the @@ -150,9 +153,10 @@ class MainActivity : AppCompatActivity() { // specific event is what made a single reminder or widget tap re-open // that event on every later launch, an error screen once the event had // been deleted (#309); the user left it long ago, and the activity - // restores where they actually were along with its saved state. A date - // open is held back the same way once handled, so a finished widget day - // (#530) comes back from Recents on the home view. + // restores where they actually were along with its saved state. Every + // widget/shortcut navigation is held back the same way; one still pending + // at the recreation is restored from the saved state instead, so it's the + // request that actually arrived last, not the launch intent's (#530). // // Only those channels are held back. An .ics import or a prefilled // create form keeps no saved state of its own, so re-reading the intent @@ -164,10 +168,12 @@ class MainActivity : AppCompatActivity() { requests.detailKey = intent.detailKeyOrNull() ?: intent.viewEventKeyOrNull() requests.editKey = intent.editEventKeyOrNull() } - val navPendingBefore = savedInstanceState?.getBoolean(KEY_NAV_PENDING) == true - requests.nav = intent.navRequestOrNull()?.takeUnless { - replaying && (it is WidgetNavRequest.OpenEvent || (it is WidgetNavRequest.OpenDate && !navPendingBefore)) + requests.nav = if (replaying) { + savedInstanceState?.getString(KEY_PENDING_NAV)?.let(::decodeWidgetNavRequest) + } else { + intent.navRequestOrNull() } + requests.leaveOnBack = savedInstanceState?.getBoolean(KEY_LEAVE_ON_BACK) == true requests.importUri = intent.importUriOrNull() val insertOpenBefore = savedInstanceState?.getBoolean(KEY_INSERT_FORM_OPEN) == true requests.insert = intent.createRequestOrNull()?.takeIf { !replaying || insertOpenBefore } @@ -282,6 +288,12 @@ class MainActivity : AppCompatActivity() { calendarRepository.refresh() } + override fun onStop() { + super.onStop() + // Once the user has left, a later return is no longer the widget's day. + if (!isChangingConfigurations) requests.leaveOnBack = false + } + override fun onResume() { super.onResume() // Reaching a running UI means startup succeeded; reset the loop trail. @@ -293,7 +305,8 @@ class MainActivity : AppCompatActivity() { super.onSaveInstanceState(outState) outState.putBoolean(KEY_INSERT_FORM_OPEN, insertFormOpen) outState.putBoolean(KEY_LAUNCHED_FOR_INSERT, launchedForInsert) - outState.putBoolean(KEY_NAV_PENDING, requests.nav != null) + outState.putString(KEY_PENDING_NAV, requests.nav?.encode()) + outState.putBoolean(KEY_LEAVE_ON_BACK, requests.leaveOnBack) } override fun onNewIntent(intent: Intent) { @@ -486,7 +499,7 @@ class MainActivity : AppCompatActivity() { endMillis = getLongExtra(EXTRA_END_MILLIS, 0L), source = source, ) - source != null && getStringExtra(EXTRA_DATE_ISO) != null -> + getStringExtra(EXTRA_DATE_ISO) != null -> WidgetNavRequest.OpenDate(getStringExtra(EXTRA_DATE_ISO)!!, fromWidget = true) else -> null } @@ -569,7 +582,13 @@ class MainActivity : AppCompatActivity() { private val CALENDAR_EXTENSIONS = listOf(".ics", ".vcs") private const val KEY_INSERT_FORM_OPEN = "insert_form_open" private const val KEY_LAUNCHED_FOR_INSERT = "launched_for_insert" - private const val KEY_NAV_PENDING = "nav_pending" + private const val KEY_PENDING_NAV = "pending_nav" + + // Delivered to the running activity even when the task was started by + // this very intent, which NEW_TASK alone would only bring to the front. + private const val WIDGET_LAUNCH_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK or + Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP + private const val KEY_LEAVE_ON_BACK = "leave_on_back" private const val EXTRA_EVENT_ID = "de.jeanlucmakiola.calendula.extra.EVENT_ID" private const val EXTRA_BEGIN_MILLIS = "de.jeanlucmakiola.calendula.extra.BEGIN" @@ -609,18 +628,13 @@ class MainActivity : AppCompatActivity() { /** * Open the day view anchored on [date] from a home-screen widget; backing - * out of the day returns to the home screen (#530). [source] marks the - * launch as a widget's and keeps different widget kinds' PendingIntents - * apart; its value doesn't change where back goes. + * out of the day returns to the home screen (#530). */ - fun openDateIntent(context: Context, date: LocalDate, source: CalendarView): Intent = + fun openDateIntent(context: Context, date: LocalDate): Intent = Intent(context, MainActivity::class.java).apply { - // The source is part of the URI so widgets of different kinds - // tapping the same date don't share one PendingIntent. - data = "calendula://date/$date/${source.name}".toUri() + data = "calendula://date/$date".toUri() putExtra(EXTRA_DATE_ISO, date.toString()) - putExtra(EXTRA_SOURCE_VIEW, source.name) - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + addFlags(WIDGET_LAUNCH_FLAGS) } /** @@ -638,6 +652,7 @@ class MainActivity : AppCompatActivity() { ): Intent = eventDetailIntent(context, eventId, beginMillis, endMillis).apply { data = "calendula://event/$eventId/$beginMillis/${source.name}".toUri() putExtra(EXTRA_SOURCE_VIEW, source.name) + addFlags(WIDGET_LAUNCH_FLAGS) } /** @@ -660,7 +675,7 @@ class MainActivity : AppCompatActivity() { data = "calendula://create/$date".toUri() putExtra(EXTRA_CREATE, true) putExtra(EXTRA_DATE_ISO, date.toString()) - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + addFlags(WIDGET_LAUNCH_FLAGS) } /** @@ -674,7 +689,7 @@ class MainActivity : AppCompatActivity() { Intent(context, MainActivity::class.java).apply { data = "calendula://view/${view?.name ?: "default"}".toUri() putExtra(EXTRA_OPEN_VIEW, view?.name ?: "") - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + addFlags(WIDGET_LAUNCH_FLAGS) } } } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt index d73e4191..28ea4b67 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -1,7 +1,6 @@ package de.jeanlucmakiola.calendula.ui import androidx.activity.compose.BackHandler -import androidx.activity.compose.LocalActivity import androidx.activity.compose.PredictiveBackHandler import androidx.compose.animation.AnimatedContent import androidx.compose.animation.AnimatedVisibility @@ -167,8 +166,6 @@ fun CalendarHost( // True after backing out of a day opened from the view now shown, so the // agenda returns to where it was instead of today (#367). var returnedFromDay by remember { mutableStateOf(false) } - // A day opened from a widget leaves the app on back (#530), until the user moves on in it. - var exitOnBack by rememberSaveable { mutableStateOf(false) } val view = viewStack.last() // The date every view opens on and carries along (#184); tapping a day // anywhere focuses it and opens the day view there. @@ -188,7 +185,7 @@ fun CalendarHost( navigationVersion++ returnedFromDay = false val moved = move(viewStack) - if (moved != viewStack) exitOnBack = false + requests.leaveOnBack = keepsLeaveOnBack(requests.leaveOnBack, viewStack, moved) viewStack = moved } val onSelectView: (CalendarView) -> Unit = { target -> switchTo { it.selectView(target) } } @@ -425,7 +422,7 @@ fun CalendarHost( dismissCoveringOverlays() focus.navigateTo(LocalDate.parse(req.dateIso)) viewStack = listOf(defaultView).drillToDay() - exitOnBack = req.fromWidget + requests.leaveOnBack = req.leavesOnBack requests.nav = null } is WidgetNavRequest.OpenEvent -> { @@ -433,7 +430,7 @@ fun CalendarHost( // detail over it (same key shape as a tapped event / reminder). dismissCoveringOverlays() viewStack = viewBaseStack(defaultView, req.source) - exitOnBack = false + requests.leaveOnBack = false detailKey = longArrayOf(req.eventId, req.beginMillis, req.endMillis) requests.nav = null } @@ -445,7 +442,7 @@ fun CalendarHost( dismissCoveringOverlays() focus.navigateTo(currentDate()) viewStack = viewBaseStack(defaultView, req.view ?: defaultView) - exitOnBack = false + requests.leaveOnBack = false requests.nav = null } is WidgetNavRequest.Create -> { @@ -542,11 +539,17 @@ fun CalendarHost( val viewTransition = remember { SeekableTransitionState(view) } LaunchedEffect(view) { viewTransition.animateTo(view) } val reduceMotion = rememberReduceMotion() - val activity = LocalActivity.current - val leaveOnBack = !anyOverlayVisible && exitOnBack && activity != null + val leaveOnBack = !anyOverlayVisible && requests.leaveOnBack val viewBackEnabled = !anyOverlayVisible && viewStack.size > 1 && !leaveOnBack - // Finished rather than sent to the background, so the next launch opens on the home view. - BackHandler(enabled = leaveOnBack) { activity?.finish() } + // The app goes to the background on the home view, so coming back shows home. + BackHandler(enabled = leaveOnBack) { + requests.onLeave() + requests.leaveOnBack = false + navigationVersion++ + returnedFromDay = false + focus.navigateTo(currentDate()) + viewStack = listOf(defaultView) + } if (reduceMotion) { BackHandler(enabled = viewBackEnabled) { navigationVersion++ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt index 8993e10a..3efd1352 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt @@ -15,10 +15,14 @@ import de.jeanlucmakiola.calendula.ui.edit.ImportSource * it, so a later recomposition can't act on it twice. * * [onExternalFormClosed] runs when a form opened for [insert] closes, so the - * activity can hand control back to the app that asked. + * activity can hand control back to the app that asked. [onLeave] sends the app + * to the background when back leaves a widget's day ([leaveOnBack]). */ @Stable -class ExternalRequests(val onExternalFormClosed: () -> Unit = {}) { +class ExternalRequests( + val onExternalFormClosed: () -> Unit = {}, + val onLeave: () -> Unit = {}, +) { /** A tapped reminder notification's occurrence (eventId, begin, end), for the detail screen. */ var detailKey by mutableStateOf(null) @@ -37,6 +41,9 @@ class ExternalRequests(val onExternalFormClosed: () -> Unit = {}) { /** An external "edit this event" (ACTION_EDIT), in the detail key's shape. */ var editKey by mutableStateOf(null) + /** Back leaves the app at once: a widget's day is showing (#530). Cleared when the app goes to the background. */ + var leaveOnBack by mutableStateOf(false) + /** Whether these came from re-reading the launch intent on a recreation. */ var replayed = false } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt index 22198ede..7a934599 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt @@ -39,3 +39,49 @@ sealed interface WidgetNavRequest { */ data class OpenView(val view: CalendarView?) : WidgetNavRequest } + +/** Whether back leaves the app straight from what this request opens (a widget's day, #530). */ +val WidgetNavRequest.leavesOnBack: Boolean + get() = this is WidgetNavRequest.OpenDate && fromWidget + +/** + * Whether a widget day's leave-on-back stays armed across a move from [before] to + * [after]: only while the user hasn't navigated to another view. + */ +fun keepsLeaveOnBack(armed: Boolean, before: List, after: List): Boolean = + armed && before == after + +/** A request as one string, so a still-pending one survives a recreation. */ +fun WidgetNavRequest.encode(): String = when (this) { + is WidgetNavRequest.OpenDate -> listOf("date", dateIso, fromWidget.toString()) + is WidgetNavRequest.OpenEvent -> listOf("event", eventId, beginMillis, endMillis, source.name) + is WidgetNavRequest.Create -> listOf("create", dateIso.orEmpty()) + is WidgetNavRequest.OpenView -> listOf("view", view?.name.orEmpty()) +}.joinToString(SEPARATOR) + +/** The request [encode] wrote, or null for anything it didn't. */ +fun decodeWidgetNavRequest(encoded: String): WidgetNavRequest? { + val parts = encoded.split(SEPARATOR) + fun view(name: String) = CalendarView.entries.firstOrNull { it.name == name } + return when (parts.firstOrNull()) { + "date" -> if (parts.size == 3) WidgetNavRequest.OpenDate(parts[1], parts[2].toBoolean()) else null + "event" -> if (parts.size == 5) { + val id = parts[1].toLongOrNull() + val begin = parts[2].toLongOrNull() + val end = parts[3].toLongOrNull() + val source = view(parts[4]) + if (id != null && begin != null && end != null && source != null) { + WidgetNavRequest.OpenEvent(id, begin, end, source) + } else { + null + } + } else { + null + } + "create" -> if (parts.size == 2) WidgetNavRequest.Create(parts[1].ifEmpty { null }) else null + "view" -> if (parts.size == 2) WidgetNavRequest.OpenView(view(parts[1])) else null + else -> null + } +} + +private const val SEPARATOR = "|" diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/WidgetParts.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/WidgetParts.kt index a894c46d..10e2ce36 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/WidgetParts.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/WidgetParts.kt @@ -39,9 +39,9 @@ internal fun EventInstance.openIntent(context: Context, source: CalendarView) = source = source, ) -/** Opens [date] in the app's agenda. */ +/** Opens [date]'s day view. */ internal fun openDayAction(context: Context, date: LocalDate) = - actionStartActivity(MainActivity.openDateIntent(context, date, CalendarView.Agenda)) + actionStartActivity(MainActivity.openDateIntent(context, date)) /** An event's time on [day]: "All day", "Starts 14:00", "Ends 9:00" or "14:00 – 15:30". */ internal fun widgetTimeLabel(context: Context, event: EventInstance, day: LocalDate, is24Hour: Boolean): String = diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/date/DateWidget.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/date/DateWidget.kt index aa78587e..131657df 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/date/DateWidget.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/date/DateWidget.kt @@ -33,7 +33,6 @@ import androidx.glance.text.Text import androidx.glance.text.TextAlign import androidx.glance.text.TextStyle import de.jeanlucmakiola.calendula.MainActivity -import de.jeanlucmakiola.calendula.ui.common.CalendarView import de.jeanlucmakiola.calendula.widget.CalendulaWidgetReceiver import de.jeanlucmakiola.calendula.widget.CalendulaWidgetRoot import de.jeanlucmakiola.calendula.widget.WidgetAppearance @@ -99,7 +98,7 @@ internal fun DateWidgetContent(today: LocalDate, appearance: WidgetAppearance, a Column( modifier = GlanceModifier .fillMaxSize() - .clickable(actionStartActivity(MainActivity.openDateIntent(context, today, CalendarView.Day))) + .clickable(actionStartActivity(MainActivity.openDateIntent(context, today))) .semantics { contentDescription = label }, horizontalAlignment = Alignment.CenterHorizontally, verticalAlignment = Alignment.CenterVertically, diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt index c7df3985..a1ae8808 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/month/MonthGridViews.kt @@ -14,7 +14,6 @@ import de.jeanlucmakiola.calendula.MainActivity import de.jeanlucmakiola.calendula.R import de.jeanlucmakiola.calendula.domain.EventInstance import de.jeanlucmakiola.calendula.domain.isCrossedOut -import de.jeanlucmakiola.calendula.ui.common.CalendarView import de.jeanlucmakiola.calendula.ui.common.eventFill import de.jeanlucmakiola.calendula.ui.common.eventInk import de.jeanlucmakiola.floret.glance.RemoteColor @@ -161,7 +160,7 @@ private fun span(text: String, span: Any): CharSequence = private fun openDayIntent(context: Context, date: LocalDate): PendingIntent = PendingIntent.getActivity( context, date.toEpochDays().toInt(), - MainActivity.openDateIntent(context, date, CalendarView.Month), + MainActivity.openDateIntent(context, date), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, ) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/split/MonthDayWidget.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/split/MonthDayWidget.kt index d7e377c3..a2e42e6e 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/split/MonthDayWidget.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/split/MonthDayWidget.kt @@ -415,7 +415,7 @@ private fun DayList(day: LocalDate, data: MonthDayData.Ready, preview: Boolean) val events = groupAgendaDays(day, day, source.instances, zone).firstOrNull()?.events.orEmpty() .let { all -> if (data.options.pastDisplay == PastEventDisplay.HIDE) all.filterNot { it.hasEnded(data.now) } else all } val metrics = compactRowMetrics(data.options.textSize, context.resources.configuration.fontScale) - val openDay = actionStartActivity(MainActivity.openDateIntent(context, day, CalendarView.Month)) + val openDay = actionStartActivity(MainActivity.openDateIntent(context, day)) Column(modifier = GlanceModifier.fillMaxSize()) { val date = localizedDateFormatter(widgetLocale(), "EEEdMMM").format(day.toJavaLocalDate()) val label = when (day) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/today/TodayWidget.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/today/TodayWidget.kt index 584db6c7..3246ff40 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/today/TodayWidget.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/today/TodayWidget.kt @@ -245,7 +245,7 @@ private fun TodayPill(today: LocalDate) { .fillMaxHeight() .cornerRadius(PillWidth / 2) .background(GlanceTheme.colors.primary) - .clickable(actionStartActivity(MainActivity.openDateIntent(context, today, CalendarView.Day))) + .clickable(actionStartActivity(MainActivity.openDateIntent(context, today))) .semantics { contentDescription = localizedDateFormatter(locale, "EEEEdMMMM").format(today.toJavaLocalDate()) }, horizontalAlignment = Alignment.CenterHorizontally, verticalAlignment = Alignment.CenterVertically, diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/widget/week/WeekStripWidget.kt b/app/src/main/java/de/jeanlucmakiola/calendula/widget/week/WeekStripWidget.kt index cf856ca3..bb4ce5b5 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/widget/week/WeekStripWidget.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/widget/week/WeekStripWidget.kt @@ -46,7 +46,6 @@ import androidx.glance.text.TextStyle import androidx.glance.unit.ColorProvider import de.jeanlucmakiola.calendula.MainActivity import de.jeanlucmakiola.calendula.R -import de.jeanlucmakiola.calendula.ui.common.CalendarView import de.jeanlucmakiola.calendula.ui.common.eventAccent import de.jeanlucmakiola.calendula.ui.common.eventFill import de.jeanlucmakiola.calendula.widget.CalendulaWidgetReceiver @@ -155,7 +154,7 @@ private fun DayTile(date: LocalDate, data: DaysWidgetData.Ready) { .fillMaxSize() .cornerRadius(14.dp) .background(container) - .clickable(actionStartActivity(MainActivity.openDateIntent(context, date, CalendarView.Week))) + .clickable(actionStartActivity(MainActivity.openDateIntent(context, date))) .semantics { contentDescription = if (colors.isEmpty()) label else context.resources.getQuantityString( R.plurals.widget_day_with_events, events.size, label, events.size, diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt new file mode 100644 index 00000000..7930d878 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt @@ -0,0 +1,49 @@ +package de.jeanlucmakiola.calendula.ui + +import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.calendula.ui.common.CalendarView +import org.junit.jupiter.api.Test + +class WidgetNavRequestTest { + + @Test + fun `only a widget's day leaves on back`() { + assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = true).leavesOnBack).isTrue() + assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = false).leavesOnBack).isFalse() + assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Agenda).leavesOnBack).isFalse() + assertThat(WidgetNavRequest.OpenView(CalendarView.Month).leavesOnBack).isFalse() + assertThat(WidgetNavRequest.Create(null).leavesOnBack).isFalse() + } + + @Test + fun `leave on back stays armed only while the view stays put`() { + val day = listOf(CalendarView.Week, CalendarView.Day) + assertThat(keepsLeaveOnBack(true, day, day.toList())).isTrue() + assertThat(keepsLeaveOnBack(true, day, listOf(CalendarView.Week))).isFalse() + assertThat(keepsLeaveOnBack(true, day, day + CalendarView.Month)).isFalse() + assertThat(keepsLeaveOnBack(false, day, day)).isFalse() + } + + @Test + fun `every request survives the encode round trip`() { + val requests = listOf( + WidgetNavRequest.OpenDate("2026-11-25", fromWidget = true), + WidgetNavRequest.OpenDate("2026-11-25", fromWidget = false), + WidgetNavRequest.OpenEvent(42, 1_000, 2_000, CalendarView.Agenda), + WidgetNavRequest.Create("2026-11-25"), + WidgetNavRequest.Create(null), + WidgetNavRequest.OpenView(CalendarView.Month), + WidgetNavRequest.OpenView(null), + ) + requests.forEach { assertThat(decodeWidgetNavRequest(it.encode())).isEqualTo(it) } + } + + @Test + fun `malformed input decodes to nothing`() { + assertThat(decodeWidgetNavRequest("")).isNull() + assertThat(decodeWidgetNavRequest("date|2026-11-25")).isNull() + assertThat(decodeWidgetNavRequest("event|x|1|2|Agenda")).isNull() + assertThat(decodeWidgetNavRequest("event|1|1|2|Nope")).isNull() + assertThat(decodeWidgetNavRequest("other|1")).isNull() + } +} diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt index 6558671f..9d830249 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/ViewBackStackTest.kt @@ -74,7 +74,8 @@ class ViewBackStackTest { } @Test - fun `a day drilled over a widget's view backs out to it, then home`() { + fun `a day tapped in a widget event's view backs out to it, then home`() { + // Agenda widget event tap roots [Week, Agenda]; a day tapped there drills on top. val stack = viewBaseStack(CalendarView.Week, CalendarView.Agenda).drillToDay() assertThat(stack) .containsExactly(CalendarView.Week, CalendarView.Agenda, CalendarView.Day) @@ -84,14 +85,6 @@ class ViewBackStackTest { assertThat(stack.dropLast(2)).containsExactly(CalendarView.Week) } - @Test - fun `a day drilled over the month backs out to it, then home`() { - val stack = viewBaseStack(CalendarView.Week, CalendarView.Month).drillToDay() - assertThat(stack.last()).isEqualTo(CalendarView.Day) - assertThat(stack.dropLast(1).last()).isEqualTo(CalendarView.Month) - assertThat(stack.first()).isEqualTo(CalendarView.Week) - } - @Test fun `a date launch drills the day over the home view only`() { assertThat(listOf(CalendarView.Week).drillToDay()) From 8bdc88622678c3738d35f1e4820e0517eca1be7a Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 15:30:38 +0100 Subject: [PATCH 6/8] fix(#530): review round 4 - home view reset on return, leave hint instead of stop, no clear-top --- .../jeanlucmakiola/calendula/MainActivity.kt | 42 +++++++++++-------- .../calendula/ui/CalendarHost.kt | 8 +--- .../calendula/ui/ExternalRequests.kt | 2 +- .../calendula/ui/WidgetNavRequest.kt | 38 +---------------- .../calendula/ui/WidgetNavRequestTest.kt | 23 ---------- 5 files changed, 30 insertions(+), 83 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index 1adcf620..43d9550f 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -24,6 +24,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.core.content.IntentCompat import androidx.core.net.toUri +import androidx.core.os.BundleCompat import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.lifecycleScope @@ -48,8 +49,6 @@ import de.jeanlucmakiola.calendula.ui.common.rememberTimelineZoom import de.jeanlucmakiola.calendula.ui.common.LocalSoftenColors import de.jeanlucmakiola.calendula.ui.common.LocalUse24HourFormat import de.jeanlucmakiola.calendula.ui.WidgetNavRequest -import de.jeanlucmakiola.calendula.ui.decodeWidgetNavRequest -import de.jeanlucmakiola.calendula.ui.encode import de.jeanlucmakiola.calendula.ui.common.CalendarView import de.jeanlucmakiola.calendula.ui.detail.EventDetailViewModel.Companion.NO_OCCURRENCE_TIME import de.jeanlucmakiola.calendula.ui.edit.ImportSource @@ -94,7 +93,10 @@ class MainActivity : AppCompatActivity() { insertFormOpen = false if (launchedForInsert) finish() }, - onLeave = { moveTaskToBack(true) }, + onLeave = { + homeOnReturn = true + moveTaskToBack(true) + }, ) // Whether such a form is still open. Only then does a recreation re-read the @@ -105,6 +107,9 @@ class MainActivity : AppCompatActivity() { // app that asked instead of leaving the calendar on top of it. private var launchedForInsert = false + // Back left a widget's day for the home screen; the next start shows the home view. + private var homeOnReturn = false + // A captured crash report awaiting the user's decision, surfaced as a dialog // over the calendar on the next launch (the single-crash path). A startup // crash-loop is handled out of band, before setContent — see below. @@ -169,11 +174,12 @@ class MainActivity : AppCompatActivity() { requests.editKey = intent.editEventKeyOrNull() } requests.nav = if (replaying) { - savedInstanceState?.getString(KEY_PENDING_NAV)?.let(::decodeWidgetNavRequest) + savedInstanceState?.let { BundleCompat.getSerializable(it, KEY_PENDING_NAV, WidgetNavRequest::class.java) } } else { intent.navRequestOrNull() } requests.leaveOnBack = savedInstanceState?.getBoolean(KEY_LEAVE_ON_BACK) == true + homeOnReturn = savedInstanceState?.getBoolean(KEY_HOME_ON_RETURN) == true requests.importUri = intent.importUriOrNull() val insertOpenBefore = savedInstanceState?.getBoolean(KEY_INSERT_FORM_OPEN) == true requests.insert = intent.createRequestOrNull()?.takeIf { !replaying || insertOpenBefore } @@ -284,14 +290,20 @@ class MainActivity : AppCompatActivity() { override fun onStart() { super.onStart() + // Reset only now, so the screen doesn't change while the task animates away. + // A request arriving with this start (onNewIntent comes after) replaces it. + if (homeOnReturn) { + homeOnReturn = false + if (requests.nav == null) requests.nav = WidgetNavRequest.OpenView(null) + } // Changes made while we were away may never have notified us (#364). calendarRepository.refresh() } - override fun onStop() { - super.onStop() + override fun onUserLeaveHint() { + super.onUserLeaveHint() // Once the user has left, a later return is no longer the widget's day. - if (!isChangingConfigurations) requests.leaveOnBack = false + requests.leaveOnBack = false } override fun onResume() { @@ -305,8 +317,9 @@ class MainActivity : AppCompatActivity() { super.onSaveInstanceState(outState) outState.putBoolean(KEY_INSERT_FORM_OPEN, insertFormOpen) outState.putBoolean(KEY_LAUNCHED_FOR_INSERT, launchedForInsert) - outState.putString(KEY_PENDING_NAV, requests.nav?.encode()) + outState.putSerializable(KEY_PENDING_NAV, requests.nav) outState.putBoolean(KEY_LEAVE_ON_BACK, requests.leaveOnBack) + outState.putBoolean(KEY_HOME_ON_RETURN, homeOnReturn) } override fun onNewIntent(intent: Intent) { @@ -583,12 +596,8 @@ class MainActivity : AppCompatActivity() { private const val KEY_INSERT_FORM_OPEN = "insert_form_open" private const val KEY_LAUNCHED_FOR_INSERT = "launched_for_insert" private const val KEY_PENDING_NAV = "pending_nav" - - // Delivered to the running activity even when the task was started by - // this very intent, which NEW_TASK alone would only bring to the front. - private const val WIDGET_LAUNCH_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK or - Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP private const val KEY_LEAVE_ON_BACK = "leave_on_back" + private const val KEY_HOME_ON_RETURN = "home_on_return" private const val EXTRA_EVENT_ID = "de.jeanlucmakiola.calendula.extra.EVENT_ID" private const val EXTRA_BEGIN_MILLIS = "de.jeanlucmakiola.calendula.extra.BEGIN" @@ -634,7 +643,7 @@ class MainActivity : AppCompatActivity() { Intent(context, MainActivity::class.java).apply { data = "calendula://date/$date".toUri() putExtra(EXTRA_DATE_ISO, date.toString()) - addFlags(WIDGET_LAUNCH_FLAGS) + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } /** @@ -652,7 +661,6 @@ class MainActivity : AppCompatActivity() { ): Intent = eventDetailIntent(context, eventId, beginMillis, endMillis).apply { data = "calendula://event/$eventId/$beginMillis/${source.name}".toUri() putExtra(EXTRA_SOURCE_VIEW, source.name) - addFlags(WIDGET_LAUNCH_FLAGS) } /** @@ -675,7 +683,7 @@ class MainActivity : AppCompatActivity() { data = "calendula://create/$date".toUri() putExtra(EXTRA_CREATE, true) putExtra(EXTRA_DATE_ISO, date.toString()) - addFlags(WIDGET_LAUNCH_FLAGS) + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } /** @@ -689,7 +697,7 @@ class MainActivity : AppCompatActivity() { Intent(context, MainActivity::class.java).apply { data = "calendula://view/${view?.name ?: "default"}".toUri() putExtra(EXTRA_OPEN_VIEW, view?.name ?: "") - addFlags(WIDGET_LAUNCH_FLAGS) + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } } } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt index 28ea4b67..db1212bc 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -541,14 +541,10 @@ fun CalendarHost( val reduceMotion = rememberReduceMotion() val leaveOnBack = !anyOverlayVisible && requests.leaveOnBack val viewBackEnabled = !anyOverlayVisible && viewStack.size > 1 && !leaveOnBack - // The app goes to the background on the home view, so coming back shows home. + // The app goes to the background and comes back on the home view. BackHandler(enabled = leaveOnBack) { - requests.onLeave() requests.leaveOnBack = false - navigationVersion++ - returnedFromDay = false - focus.navigateTo(currentDate()) - viewStack = listOf(defaultView) + requests.onLeave() } if (reduceMotion) { BackHandler(enabled = viewBackEnabled) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt index 3efd1352..badc9e5c 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt @@ -41,7 +41,7 @@ class ExternalRequests( /** An external "edit this event" (ACTION_EDIT), in the detail key's shape. */ var editKey by mutableStateOf(null) - /** Back leaves the app at once: a widget's day is showing (#530). Cleared when the app goes to the background. */ + /** Back leaves the app at once: a widget's day is showing (#530). Cleared once the user leaves the app. */ var leaveOnBack by mutableStateOf(false) /** Whether these came from re-reading the launch intent on a recreation. */ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt index 7a934599..c0f99c33 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt @@ -1,6 +1,7 @@ package de.jeanlucmakiola.calendula.ui import de.jeanlucmakiola.calendula.ui.common.CalendarView +import java.io.Serializable /** * A navigation a home-screen widget asked the app to perform when launched. @@ -11,7 +12,7 @@ import de.jeanlucmakiola.calendula.ui.common.CalendarView * widgets — they keep the separate detail-key channel and leave the base view * untouched.) */ -sealed interface WidgetNavRequest { +sealed interface WidgetNavRequest : Serializable { /** * Open the day view anchored on [dateIso] (an ISO `yyyy-MM-dd` date), over the * default home view. [fromWidget] makes back leave the app at once (#530); a @@ -50,38 +51,3 @@ val WidgetNavRequest.leavesOnBack: Boolean */ fun keepsLeaveOnBack(armed: Boolean, before: List, after: List): Boolean = armed && before == after - -/** A request as one string, so a still-pending one survives a recreation. */ -fun WidgetNavRequest.encode(): String = when (this) { - is WidgetNavRequest.OpenDate -> listOf("date", dateIso, fromWidget.toString()) - is WidgetNavRequest.OpenEvent -> listOf("event", eventId, beginMillis, endMillis, source.name) - is WidgetNavRequest.Create -> listOf("create", dateIso.orEmpty()) - is WidgetNavRequest.OpenView -> listOf("view", view?.name.orEmpty()) -}.joinToString(SEPARATOR) - -/** The request [encode] wrote, or null for anything it didn't. */ -fun decodeWidgetNavRequest(encoded: String): WidgetNavRequest? { - val parts = encoded.split(SEPARATOR) - fun view(name: String) = CalendarView.entries.firstOrNull { it.name == name } - return when (parts.firstOrNull()) { - "date" -> if (parts.size == 3) WidgetNavRequest.OpenDate(parts[1], parts[2].toBoolean()) else null - "event" -> if (parts.size == 5) { - val id = parts[1].toLongOrNull() - val begin = parts[2].toLongOrNull() - val end = parts[3].toLongOrNull() - val source = view(parts[4]) - if (id != null && begin != null && end != null && source != null) { - WidgetNavRequest.OpenEvent(id, begin, end, source) - } else { - null - } - } else { - null - } - "create" -> if (parts.size == 2) WidgetNavRequest.Create(parts[1].ifEmpty { null }) else null - "view" -> if (parts.size == 2) WidgetNavRequest.OpenView(view(parts[1])) else null - else -> null - } -} - -private const val SEPARATOR = "|" diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt index 7930d878..be3bc927 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt @@ -23,27 +23,4 @@ class WidgetNavRequestTest { assertThat(keepsLeaveOnBack(true, day, day + CalendarView.Month)).isFalse() assertThat(keepsLeaveOnBack(false, day, day)).isFalse() } - - @Test - fun `every request survives the encode round trip`() { - val requests = listOf( - WidgetNavRequest.OpenDate("2026-11-25", fromWidget = true), - WidgetNavRequest.OpenDate("2026-11-25", fromWidget = false), - WidgetNavRequest.OpenEvent(42, 1_000, 2_000, CalendarView.Agenda), - WidgetNavRequest.Create("2026-11-25"), - WidgetNavRequest.Create(null), - WidgetNavRequest.OpenView(CalendarView.Month), - WidgetNavRequest.OpenView(null), - ) - requests.forEach { assertThat(decodeWidgetNavRequest(it.encode())).isEqualTo(it) } - } - - @Test - fun `malformed input decodes to nothing`() { - assertThat(decodeWidgetNavRequest("")).isNull() - assertThat(decodeWidgetNavRequest("date|2026-11-25")).isNull() - assertThat(decodeWidgetNavRequest("event|x|1|2|Agenda")).isNull() - assertThat(decodeWidgetNavRequest("event|1|1|2|Nope")).isNull() - assertThat(decodeWidgetNavRequest("other|1")).isNull() - } } From 8306e8c636561fc2ff77964a066b39475fbc5ee2 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 15:51:36 +0100 Subject: [PATCH 7/8] fix(#530): review round 5 - a widget's event detail also leaves on back --- .../calendula/ui/CalendarHost.kt | 24 ++++++++++++------- .../calendula/ui/WidgetNavRequest.kt | 17 +++++++------ .../calendula/ui/WidgetNavRequestTest.kt | 15 ++++++++++-- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt index db1212bc..cadfe298 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -128,10 +128,9 @@ import kotlin.time.Instant * (see [selectView]); a widget event or header launch resets the stack to the * widget's view; a date tap or date launch drills the day view on top. Pressing * back pops one level, and the base-level [BackHandler] hands off to the system - * to exit once only the home view remains. So back from a widget-opened event returns to that widget's - * view, then home; back from a widget-opened day leaves the app at once (#530), - * until the user navigates on from it; and back through pill switches walks the - * views in reverse. + * to exit once only the home view remains. Back from a widget-opened day or + * event leaves the app at once (#530), until the user navigates on from it; and + * back through pill switches walks the views in reverse. * * [requests] carries what notifications, widgets and other apps asked to * open. A tapped reminder's occurrence opens the detail overlay exactly like an @@ -205,6 +204,8 @@ fun CalendarHost( // Like every overlay below, it draws its last key through the slide-out // ([rememberHeld]) rather than a placeholder. var detailKey by rememberSaveable { mutableStateOf(null) } + // The detail a widget opened; back from it leaves the app (#530). + var widgetDetailKey by rememberSaveable { mutableStateOf(null) } val onOpenDetail: (EventInstance) -> Unit = { event -> detailKey = event.occurrenceKey() } // The drawer's "Hide in this view" picker (#517). It is its own window over @@ -407,8 +408,7 @@ fun CalendarHost( // A home-screen widget launch asks to open a date (→ day view), open an // event's detail, or start a create. Handled once and cleared, mirroring - // the detail request. An event open roots the stack in the widget's own view - // so backing out returns there (then home); a widget's day leaves on back. + // the detail request. A widget's day or event leaves the app on back (#530). val widgetNavRequest = requests.nav LaunchedEffect(widgetNavRequest) { if (widgetNavRequest != null) { @@ -430,8 +430,9 @@ fun CalendarHost( // detail over it (same key shape as a tapped event / reminder). dismissCoveringOverlays() viewStack = viewBaseStack(defaultView, req.source) - requests.leaveOnBack = false + requests.leaveOnBack = req.leavesOnBack detailKey = longArrayOf(req.eventId, req.beginMillis, req.endMillis) + widgetDetailKey = detailKey requests.nav = null } is WidgetNavRequest.OpenView -> { @@ -785,7 +786,14 @@ fun CalendarHost( eventId = key[0], beginMillis = key[1], endMillis = key[2], - onBack = { detailKey = null }, + onBack = { + if (leavesFromDetail(requests.leaveOnBack, detailKey, widgetDetailKey)) { + requests.leaveOnBack = false + requests.onLeave() + } else { + detailKey = null + } + }, onEdit = { editKey = key }, onDuplicate = { form -> // Reuse the prefilled-create overlay: a duplicate is a diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt index c0f99c33..a917027b 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt @@ -7,10 +7,9 @@ import java.io.Serializable * A navigation a home-screen widget asked the app to perform when launched. * Parsed from the launch intent in MainActivity and consumed once by * [CalendarHost]. An event open carries the [OpenEvent.source] view of the - * widget it came from (the agenda widget → [CalendarView.Agenda]) so backing out - * returns to that view, not the default home. (Reminder notifications are not - * widgets — they keep the separate detail-key channel and leave the base view - * untouched.) + * widget it came from (the agenda widget → [CalendarView.Agenda]) as the view + * under its detail. (Reminder notifications are not widgets — they keep the + * separate detail-key channel and leave the base view untouched.) */ sealed interface WidgetNavRequest : Serializable { /** @@ -20,7 +19,7 @@ sealed interface WidgetNavRequest : Serializable { */ data class OpenDate(val dateIso: String, val fromWidget: Boolean) : WidgetNavRequest - /** Open one occurrence's detail (an agenda-widget event tap), over [source]. */ + /** Open one occurrence's detail (a widget event tap), over [source]; back leaves the app (#530). */ data class OpenEvent( val eventId: Long, val beginMillis: Long, @@ -41,9 +40,13 @@ sealed interface WidgetNavRequest : Serializable { data class OpenView(val view: CalendarView?) : WidgetNavRequest } -/** Whether back leaves the app straight from what this request opens (a widget's day, #530). */ +/** Whether back leaves the app straight from what this request opens: a widget's day or event (#530). */ val WidgetNavRequest.leavesOnBack: Boolean - get() = this is WidgetNavRequest.OpenDate && fromWidget + get() = (this is WidgetNavRequest.OpenDate && fromWidget) || this is WidgetNavRequest.OpenEvent + +/** Whether back on the detail showing [shown] leaves the app: it's still the widget's event, [opened]. */ +fun leavesFromDetail(armed: Boolean, shown: LongArray?, opened: LongArray?): Boolean = + armed && shown != null && opened != null && shown.contentEquals(opened) /** * Whether a widget day's leave-on-back stays armed across a move from [before] to diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt index be3bc927..1d8512ef 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt @@ -7,10 +7,11 @@ import org.junit.jupiter.api.Test class WidgetNavRequestTest { @Test - fun `only a widget's day leaves on back`() { + fun `a widget's day or event leaves on back`() { assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = true).leavesOnBack).isTrue() assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = false).leavesOnBack).isFalse() - assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Agenda).leavesOnBack).isFalse() + assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Agenda).leavesOnBack).isTrue() + assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Day).leavesOnBack).isTrue() assertThat(WidgetNavRequest.OpenView(CalendarView.Month).leavesOnBack).isFalse() assertThat(WidgetNavRequest.Create(null).leavesOnBack).isFalse() } @@ -23,4 +24,14 @@ class WidgetNavRequestTest { assertThat(keepsLeaveOnBack(true, day, day + CalendarView.Month)).isFalse() assertThat(keepsLeaveOnBack(false, day, day)).isFalse() } + + @Test + fun `only the widget's own detail leaves on back`() { + val opened = longArrayOf(1, 2, 3) + assertThat(leavesFromDetail(true, longArrayOf(1, 2, 3), opened)).isTrue() + assertThat(leavesFromDetail(true, longArrayOf(4, 5, 6), opened)).isFalse() + assertThat(leavesFromDetail(false, opened, opened)).isFalse() + assertThat(leavesFromDetail(true, null, opened)).isFalse() + assertThat(leavesFromDetail(true, opened, null)).isFalse() + } } From 616e4683280996a7e3baa57a0d7921536abc1483 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 16:01:10 +0100 Subject: [PATCH 8/8] fix(#530): review round 6 - one leave target, home reset as its own request, detail delete and up arrow stay in app --- .../jeanlucmakiola/calendula/MainActivity.kt | 23 +++++---- .../calendula/ui/CalendarHost.kt | 49 +++++++++++-------- .../calendula/ui/ExternalRequests.kt | 12 +++-- .../calendula/ui/WidgetNavRequest.kt | 44 ++++++++++++----- .../calendula/ui/detail/EventDetailScreen.kt | 3 +- .../calendula/ui/WidgetNavRequestTest.kt | 49 ++++++++++++------- 6 files changed, 113 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index 43d9550f..346d4755 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -40,6 +40,7 @@ import de.jeanlucmakiola.calendula.domain.EventForm import de.jeanlucmakiola.calendula.domain.buildInsertEventForm import de.jeanlucmakiola.calendula.domain.sharedTextOrNull import de.jeanlucmakiola.calendula.ui.ExternalRequests +import de.jeanlucmakiola.calendula.ui.LeaveTarget import de.jeanlucmakiola.calendula.ui.InsertRequest import de.jeanlucmakiola.calendula.ui.RootScreen import de.jeanlucmakiola.calendula.ui.common.LocalShowHourGrid @@ -94,8 +95,8 @@ class MainActivity : AppCompatActivity() { if (launchedForInsert) finish() }, onLeave = { - homeOnReturn = true - moveTaskToBack(true) + homeOnReturn = moveTaskToBack(true) + homeOnReturn }, ) @@ -107,7 +108,7 @@ class MainActivity : AppCompatActivity() { // app that asked instead of leaving the calendar on top of it. private var launchedForInsert = false - // Back left a widget's day for the home screen; the next start shows the home view. + // Back left a widget's day or event for the home screen; the next start shows the home view. private var homeOnReturn = false // A captured crash report awaiting the user's decision, surfaced as a dialog @@ -178,7 +179,9 @@ class MainActivity : AppCompatActivity() { } else { intent.navRequestOrNull() } - requests.leaveOnBack = savedInstanceState?.getBoolean(KEY_LEAVE_ON_BACK) == true + requests.leaveTarget = savedInstanceState?.let { + BundleCompat.getSerializable(it, KEY_LEAVE_TARGET, LeaveTarget::class.java) + } homeOnReturn = savedInstanceState?.getBoolean(KEY_HOME_ON_RETURN) == true requests.importUri = intent.importUriOrNull() val insertOpenBefore = savedInstanceState?.getBoolean(KEY_INSERT_FORM_OPEN) == true @@ -291,10 +294,10 @@ class MainActivity : AppCompatActivity() { override fun onStart() { super.onStart() // Reset only now, so the screen doesn't change while the task animates away. - // A request arriving with this start (onNewIntent comes after) replaces it. + // A request arriving with this start (onNewIntent comes after) opens over it. if (homeOnReturn) { homeOnReturn = false - if (requests.nav == null) requests.nav = WidgetNavRequest.OpenView(null) + requests.resetToHome = true } // Changes made while we were away may never have notified us (#364). calendarRepository.refresh() @@ -302,8 +305,8 @@ class MainActivity : AppCompatActivity() { override fun onUserLeaveHint() { super.onUserLeaveHint() - // Once the user has left, a later return is no longer the widget's day. - requests.leaveOnBack = false + // Once the user has left, a later return is no longer the widget's day or event. + requests.leaveTarget = null } override fun onResume() { @@ -318,7 +321,7 @@ class MainActivity : AppCompatActivity() { outState.putBoolean(KEY_INSERT_FORM_OPEN, insertFormOpen) outState.putBoolean(KEY_LAUNCHED_FOR_INSERT, launchedForInsert) outState.putSerializable(KEY_PENDING_NAV, requests.nav) - outState.putBoolean(KEY_LEAVE_ON_BACK, requests.leaveOnBack) + outState.putSerializable(KEY_LEAVE_TARGET, requests.leaveTarget) outState.putBoolean(KEY_HOME_ON_RETURN, homeOnReturn) } @@ -596,7 +599,7 @@ class MainActivity : AppCompatActivity() { private const val KEY_INSERT_FORM_OPEN = "insert_form_open" private const val KEY_LAUNCHED_FOR_INSERT = "launched_for_insert" private const val KEY_PENDING_NAV = "pending_nav" - private const val KEY_LEAVE_ON_BACK = "leave_on_back" + private const val KEY_LEAVE_TARGET = "leave_target" private const val KEY_HOME_ON_RETURN = "home_on_return" private const val EXTRA_EVENT_ID = "de.jeanlucmakiola.calendula.extra.EVENT_ID" diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt index cadfe298..66036c6b 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -184,7 +184,7 @@ fun CalendarHost( navigationVersion++ returnedFromDay = false val moved = move(viewStack) - requests.leaveOnBack = keepsLeaveOnBack(requests.leaveOnBack, viewStack, moved) + requests.leaveTarget = requests.leaveTarget.afterMove(viewStack, moved) viewStack = moved } val onSelectView: (CalendarView) -> Unit = { target -> switchTo { it.selectView(target) } } @@ -204,14 +204,28 @@ fun CalendarHost( // Like every overlay below, it draws its last key through the slide-out // ([rememberHeld]) rather than a placeholder. var detailKey by rememberSaveable { mutableStateOf(null) } - // The detail a widget opened; back from it leaves the app (#530). - var widgetDetailKey by rememberSaveable { mutableStateOf(null) } + LaunchedEffect(detailKey) { requests.leaveTarget = requests.leaveTarget.afterDetail(detailKey) } val onOpenDetail: (EventInstance) -> Unit = { event -> detailKey = event.occurrenceKey() } // The drawer's "Hide in this view" picker (#517). It is its own window over // everything here, so each external request below closes it first. var showViewCalendars by rememberSaveable { mutableStateOf(false) } + // Back left a widget's day or event (#530): the app comes back on the home + // view. Ahead of the other requests, so one arriving with the return opens over it. + val resetToHome = requests.resetToHome + LaunchedEffect(resetToHome) { + if (resetToHome) { + navigationVersion++ + returnedFromDay = false + detailKey = null + focus.navigateTo(currentDate()) + viewStack = listOf(defaultView) + requests.leaveTarget = null + requests.resetToHome = false + } + } + // A tapped reminder notification asks for a specific occurrence. val requestedDetailKey = requests.detailKey LaunchedEffect(requestedDetailKey) { @@ -422,7 +436,7 @@ fun CalendarHost( dismissCoveringOverlays() focus.navigateTo(LocalDate.parse(req.dateIso)) viewStack = listOf(defaultView).drillToDay() - requests.leaveOnBack = req.leavesOnBack + requests.leaveTarget = req.leaveTarget requests.nav = null } is WidgetNavRequest.OpenEvent -> { @@ -430,9 +444,8 @@ fun CalendarHost( // detail over it (same key shape as a tapped event / reminder). dismissCoveringOverlays() viewStack = viewBaseStack(defaultView, req.source) - requests.leaveOnBack = req.leavesOnBack detailKey = longArrayOf(req.eventId, req.beginMillis, req.endMillis) - widgetDetailKey = detailKey + requests.leaveTarget = req.leaveTarget requests.nav = null } is WidgetNavRequest.OpenView -> { @@ -443,7 +456,7 @@ fun CalendarHost( dismissCoveringOverlays() focus.navigateTo(currentDate()) viewStack = viewBaseStack(defaultView, req.view ?: defaultView) - requests.leaveOnBack = false + requests.leaveTarget = null requests.nav = null } is WidgetNavRequest.Create -> { @@ -540,13 +553,12 @@ fun CalendarHost( val viewTransition = remember { SeekableTransitionState(view) } LaunchedEffect(view) { viewTransition.animateTo(view) } val reduceMotion = rememberReduceMotion() - val leaveOnBack = !anyOverlayVisible && requests.leaveOnBack + val leaveOnBack = requests.leaveTarget.leaves(detailKey, anyOverlayVisible) val viewBackEnabled = !anyOverlayVisible && viewStack.size > 1 && !leaveOnBack - // The app goes to the background and comes back on the home view. - BackHandler(enabled = leaveOnBack) { - requests.leaveOnBack = false - requests.onLeave() - } + // The app goes to the background and comes back on the home view. Overlays + // opened over a widget's detail register later, so their own back still wins. + // A refused move (screen pinning) falls back to the in-app back from then on. + BackHandler(enabled = leaveOnBack) { if (!requests.onLeave()) requests.leaveTarget = null } if (reduceMotion) { BackHandler(enabled = viewBackEnabled) { navigationVersion++ @@ -786,14 +798,7 @@ fun CalendarHost( eventId = key[0], beginMillis = key[1], endMillis = key[2], - onBack = { - if (leavesFromDetail(requests.leaveOnBack, detailKey, widgetDetailKey)) { - requests.leaveOnBack = false - requests.onLeave() - } else { - detailKey = null - } - }, + onBack = { detailKey = null }, onEdit = { editKey = key }, onDuplicate = { form -> // Reuse the prefilled-create overlay: a duplicate is a @@ -803,6 +808,8 @@ fun CalendarHost( detailKey = null }, visible = detailKey != null, + // A widget's own detail leaves the app on back, through the handler above. + backEnabled = !leaveOnBack, ) } } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt index badc9e5c..dbd195ca 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/ExternalRequests.kt @@ -16,12 +16,13 @@ import de.jeanlucmakiola.calendula.ui.edit.ImportSource * * [onExternalFormClosed] runs when a form opened for [insert] closes, so the * activity can hand control back to the app that asked. [onLeave] sends the app - * to the background when back leaves a widget's day ([leaveOnBack]). + * to the background when back leaves a widget's day or event ([leaveTarget]), + * false when the system refused. */ @Stable class ExternalRequests( val onExternalFormClosed: () -> Unit = {}, - val onLeave: () -> Unit = {}, + val onLeave: () -> Boolean = { false }, ) { /** A tapped reminder notification's occurrence (eventId, begin, end), for the detail screen. */ var detailKey by mutableStateOf(null) @@ -41,8 +42,11 @@ class ExternalRequests( /** An external "edit this event" (ACTION_EDIT), in the detail key's shape. */ var editKey by mutableStateOf(null) - /** Back leaves the app at once: a widget's day is showing (#530). Cleared once the user leaves the app. */ - var leaveOnBack by mutableStateOf(false) + /** What back leaves the app from (#530). Cleared once the user leaves the app. */ + var leaveTarget by mutableStateOf(null) + + /** Set when the app comes back after [onLeave]: show the home view. */ + var resetToHome by mutableStateOf(false) /** Whether these came from re-reading the launch intent on a recreation. */ var replayed = false diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt index a917027b..9c0f54a7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt @@ -40,17 +40,37 @@ sealed interface WidgetNavRequest : Serializable { data class OpenView(val view: CalendarView?) : WidgetNavRequest } -/** Whether back leaves the app straight from what this request opens: a widget's day or event (#530). */ -val WidgetNavRequest.leavesOnBack: Boolean - get() = (this is WidgetNavRequest.OpenDate && fromWidget) || this is WidgetNavRequest.OpenEvent +/** What back leaves the app from (#530): a widget's day, or a widget's event detail. */ +sealed interface LeaveTarget : Serializable { + /** The view a widget opened, while no overlay covers it. */ + data object View : LeaveTarget -/** Whether back on the detail showing [shown] leaves the app: it's still the widget's event, [opened]. */ -fun leavesFromDetail(armed: Boolean, shown: LongArray?, opened: LongArray?): Boolean = - armed && shown != null && opened != null && shown.contentEquals(opened) + /** The detail of the occurrence a widget opened. */ + data class Detail(val eventId: Long, val beginMillis: Long, val endMillis: Long) : LeaveTarget { + fun matches(key: LongArray?): Boolean = + key != null && key.size == 3 && key[0] == eventId && key[1] == beginMillis && key[2] == endMillis + } +} -/** - * Whether a widget day's leave-on-back stays armed across a move from [before] to - * [after]: only while the user hasn't navigated to another view. - */ -fun keepsLeaveOnBack(armed: Boolean, before: List, after: List): Boolean = - armed && before == after +/** What back leaves the app from after this request: a widget's day or event, else nothing. */ +val WidgetNavRequest.leaveTarget: LeaveTarget? + get() = when (this) { + is WidgetNavRequest.OpenDate -> LeaveTarget.View.takeIf { fromWidget } + is WidgetNavRequest.OpenEvent -> LeaveTarget.Detail(eventId, beginMillis, endMillis) + else -> null + } + +/** The target after a move from [before] to [after]: kept only while the user stays on the same views. */ +fun LeaveTarget?.afterMove(before: List, after: List): LeaveTarget? = + takeIf { before == after } + +/** The target once the detail shows [shown]: a widget's detail is dropped once it closes or changes. */ +fun LeaveTarget?.afterDetail(shown: LongArray?): LeaveTarget? = + if (this is LeaveTarget.Detail && !matches(shown)) null else this + +/** Whether back leaves the app now, given the open detail and whether any overlay is up. */ +fun LeaveTarget?.leaves(detail: LongArray?, overlayVisible: Boolean): Boolean = when (this) { + LeaveTarget.View -> !overlayVisible + is LeaveTarget.Detail -> matches(detail) + null -> false +} diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt index fe1381ca..403b3f1a 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/detail/EventDetailScreen.kt @@ -142,6 +142,7 @@ fun EventDetailScreen( onEdit: () -> Unit, onDuplicate: (EventForm) -> Unit, visible: Boolean = true, + backEnabled: Boolean = true, viewModel: EventDetailViewModel = hiltViewModel(), ) { LaunchedEffect(eventId, beginMillis, endMillis) { @@ -198,7 +199,7 @@ fun EventDetailScreen( } Scaffold( - modifier = Modifier.predictiveBack(onBack = onBack, visible = visible), + modifier = Modifier.predictiveBack(onBack = onBack, enabled = backEnabled, visible = visible), snackbarHost = { SnackbarHost(snackbarHostState) }, topBar = { TopAppBar( diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt index 1d8512ef..c705cf14 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequestTest.kt @@ -6,32 +6,43 @@ import org.junit.jupiter.api.Test class WidgetNavRequestTest { + private val detail = LeaveTarget.Detail(1, 2, 3) + @Test - fun `a widget's day or event leaves on back`() { - assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = true).leavesOnBack).isTrue() - assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = false).leavesOnBack).isFalse() - assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Agenda).leavesOnBack).isTrue() - assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Day).leavesOnBack).isTrue() - assertThat(WidgetNavRequest.OpenView(CalendarView.Month).leavesOnBack).isFalse() - assertThat(WidgetNavRequest.Create(null).leavesOnBack).isFalse() + fun `a widget's day or event is what back leaves from`() { + assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = true).leaveTarget).isEqualTo(LeaveTarget.View) + assertThat(WidgetNavRequest.OpenDate("2026-11-25", fromWidget = false).leaveTarget).isNull() + assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Agenda).leaveTarget).isEqualTo(detail) + assertThat(WidgetNavRequest.OpenEvent(1, 2, 3, CalendarView.Day).leaveTarget).isEqualTo(detail) + assertThat(WidgetNavRequest.OpenView(CalendarView.Month).leaveTarget).isNull() + assertThat(WidgetNavRequest.Create(null).leaveTarget).isNull() } @Test - fun `leave on back stays armed only while the view stays put`() { + fun `the target stays only while the views stay put`() { val day = listOf(CalendarView.Week, CalendarView.Day) - assertThat(keepsLeaveOnBack(true, day, day.toList())).isTrue() - assertThat(keepsLeaveOnBack(true, day, listOf(CalendarView.Week))).isFalse() - assertThat(keepsLeaveOnBack(true, day, day + CalendarView.Month)).isFalse() - assertThat(keepsLeaveOnBack(false, day, day)).isFalse() + assertThat(LeaveTarget.View.afterMove(day, day.toList())).isEqualTo(LeaveTarget.View) + assertThat(LeaveTarget.View.afterMove(day, listOf(CalendarView.Week))).isNull() + assertThat(detail.afterMove(day, day + CalendarView.Month)).isNull() + assertThat(null.afterMove(day, day)).isNull() } @Test - fun `only the widget's own detail leaves on back`() { - val opened = longArrayOf(1, 2, 3) - assertThat(leavesFromDetail(true, longArrayOf(1, 2, 3), opened)).isTrue() - assertThat(leavesFromDetail(true, longArrayOf(4, 5, 6), opened)).isFalse() - assertThat(leavesFromDetail(false, opened, opened)).isFalse() - assertThat(leavesFromDetail(true, null, opened)).isFalse() - assertThat(leavesFromDetail(true, opened, null)).isFalse() + fun `a widget's detail is dropped once it closes or another opens`() { + assertThat(detail.afterDetail(longArrayOf(1, 2, 3))).isEqualTo(detail) + assertThat(detail.afterDetail(null)).isNull() + assertThat(detail.afterDetail(longArrayOf(4, 5, 6))).isNull() + // A widget's day keeps its target under an in-app detail. + assertThat(LeaveTarget.View.afterDetail(longArrayOf(4, 5, 6))).isEqualTo(LeaveTarget.View) + } + + @Test + fun `back leaves only from the widget's own day or detail`() { + assertThat(LeaveTarget.View.leaves(detail = null, overlayVisible = false)).isTrue() + assertThat(LeaveTarget.View.leaves(detail = longArrayOf(4, 5, 6), overlayVisible = true)).isFalse() + assertThat(detail.leaves(detail = longArrayOf(1, 2, 3), overlayVisible = true)).isTrue() + assertThat(detail.leaves(detail = longArrayOf(4, 5, 6), overlayVisible = true)).isFalse() + assertThat(detail.leaves(detail = null, overlayVisible = false)).isFalse() + assertThat(null.leaves(detail = null, overlayVisible = false)).isFalse() } }