fix(#530): review round 4 - home view reset on return, leave hint instead of stop, no clear-top

This commit is contained in:
makiolaj committed 2026-11-25 15:30:38 +01:00
1 parent cd7474f314
commit 8bdc886226
5 files changed
+30 -83

No files matched your search

@@ -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)
}
}
}
@@ -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) {
@@ -41,7 +41,7 @@ class ExternalRequests(
/** An external "edit this event" (ACTION_EDIT), in the detail key's shape. */
var editKey by mutableStateOf<LongArray?>(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. */
@@ -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<CalendarView>, after: List<CalendarView>): 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 = "|"
@@ -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()
}
}