From 5d45bb64f03cbdfc35de17bfab34f333c81d5f29 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 30 Nov 2026 19:38:16 +0100 Subject: [PATCH] Setting for what other apps' date taps open (#524) --- app/src/main/AndroidManifest.xml | 4 +- .../jeanlucmakiola/calendula/MainActivity.kt | 41 ++++++------ .../data/backup/SettingsBackupSchema.kt | 1 + .../calendula/data/prefs/SettingsPrefs.kt | 8 +++ .../calendula/ui/CalendarHost.kt | 13 +++- .../calendula/ui/CalendarHostViewModel.kt | 6 ++ .../calendula/ui/WidgetNavRequest.kt | 40 +++++++++++- .../calendula/ui/agenda/AgendaScreen.kt | 35 ++++++++--- .../calendula/ui/common/ViewFocus.kt | 34 ++++++++++ .../calendula/ui/month/MonthScreen.kt | 3 + .../calendula/ui/settings/SettingsUiState.kt | 3 + .../ui/settings/SettingsViewModel.kt | 3 + .../calendula/ui/settings/ViewsSettings.kt | 27 ++++++++ .../calendula/ui/week/WeekScreen.kt | 3 + app/src/main/res/values/strings.xml | 3 + .../calendula/ui/ExternalDateTapTest.kt | 62 +++++++++++++++++++ 16 files changed, 248 insertions(+), 38 deletions(-) create mode 100644 app/src/test/java/de/jeanlucmakiola/calendula/ui/ExternalDateTapTest.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 27cd8113..ac6b4732 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -116,8 +116,8 @@ + some surfaces use the time/epoch mime type. We open the day view, + or the default view per setting, on that date (viewTimeRequest). --> diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt index 346d4755..32a1ed3f 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/MainActivity.kt @@ -49,7 +49,9 @@ import de.jeanlucmakiola.calendula.ui.common.LocalTimelineZoom 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.CALENDAR_PROVIDER_HOST import de.jeanlucmakiola.calendula.ui.WidgetNavRequest +import de.jeanlucmakiola.calendula.ui.viewTimeRequest 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 @@ -73,9 +75,7 @@ import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalTime import kotlinx.datetime.TimeZone import kotlinx.datetime.toInstant -import kotlinx.datetime.toLocalDateTime import kotlin.time.Clock -import kotlin.time.Instant import javax.inject.Inject @AndroidEntryPoint @@ -473,26 +473,22 @@ class MainActivity : AppCompatActivity() { }?.takeIf { it > 0 } /** - * The date a launcher/clock date tap points at, parsed from the AOSP calendar - * "view time" intent: ACTION_VIEW on `content://com.android.calendar/time/ - * `. Null for any other intent. The matching manifest filter is - * what lets users pick Calendula from the system calendar chooser (issue #9). + * A launcher/clock date tap: the AOSP calendar "view time" intent. The + * matching manifest filter is what lets users pick Calendula from the system + * calendar chooser (issue #9). Our own daily summary speaks it too (#459). */ - private fun Intent.calendarTimeDateOrNull(): LocalDate? { - if (action != Intent.ACTION_VIEW) return null - val uri = data ?: return null - if (uri.host != CALENDAR_PROVIDER_HOST) return null - val segments = uri.pathSegments - if (segments.firstOrNull() != "time") return null - val millis = segments.getOrNull(1)?.toLongOrNull() ?: return null - return Instant.fromEpochMilliseconds(millis) - .toLocalDateTime(TimeZone.currentSystemDefault()).date - } + private fun Intent.viewTimeRequestOrNull(): WidgetNavRequest.OpenDate? = viewTimeRequest( + action = action, + host = data?.host, + pathSegments = data?.pathSegments.orEmpty(), + zone = TimeZone.currentSystemDefault(), + ownDayTap = getBooleanExtra(EXTRA_OWN_DAY_TAP, false), + ) private fun Intent.navRequestOrNull(): WidgetNavRequest? { - // 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) } + // An external date tap (launcher/clock) opens the day view or the default + // view on that date (#524), and backs out to the default view. + viewTimeRequestOrNull()?.let { return it } // 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)) { @@ -590,9 +586,6 @@ class MainActivity : AppCompatActivity() { } companion object { - // The calendar provider's authority/host. A date tap arrives as - // ACTION_VIEW on content://com.android.calendar/time/. - private const val CALENDAR_PROVIDER_HOST = "com.android.calendar" private const val SHARED_TEXT_TYPE = "text/plain" private val CALENDAR_TYPES = setOf("text/calendar", "text/x-vcalendar", "application/ics") private val CALENDAR_EXTENSIONS = listOf(".ics", ".vcs") @@ -608,6 +601,9 @@ class MainActivity : AppCompatActivity() { private const val EXTRA_DATE_ISO = "de.jeanlucmakiola.calendula.extra.DATE_ISO" private const val EXTRA_CREATE = "de.jeanlucmakiola.calendula.extra.CREATE" + // Marks the daily summary's own "view time" tap, which keeps the day view (#524). + private const val EXTRA_OWN_DAY_TAP = "de.jeanlucmakiola.calendula.extra.OWN_DAY_TAP" + // A widget header tap asking to open a top-level view (no date drill-in). // Its value is the target [CalendarView] name, or "" for the default view. private const val EXTRA_OPEN_VIEW = "de.jeanlucmakiola.calendula.extra.OPEN_VIEW" @@ -677,6 +673,7 @@ class MainActivity : AppCompatActivity() { .toEpochMilliseconds() return Intent(Intent.ACTION_VIEW, "content://$CALENDAR_PROVIDER_HOST/time/$noon".toUri()) .setClass(context, MainActivity::class.java) + .putExtra(EXTRA_OWN_DAY_TAP, true) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/SettingsBackupSchema.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/SettingsBackupSchema.kt index f4d7bf3d..31e9198c 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/SettingsBackupSchema.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/SettingsBackupSchema.kt @@ -42,6 +42,7 @@ internal object SettingsBackupSchema { SettingsKeys.agendaShowRangeBar, SettingsKeys.agendaShowToday, SettingsKeys.defaultView, + SettingsKeys.externalDateView, SettingsKeys.quickSwitchConfig, SettingsKeys.drawerViewOrder, SettingsKeys.defaultFormFields, diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt index 719405ea..712c7a46 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt @@ -97,6 +97,9 @@ enum class TimeFormatPref { AUTO, TWELVE_HOUR, TWENTY_FOUR_HOUR } */ enum class PastEventDisplay { SHOW, DIM, HIDE } +/** What a date another app opens in Calendula lands on (#524): the day view, or the default view on that date. */ +enum class ExternalDateView { DAY, DEFAULT } + /** Parse a stored [PastEventDisplay.name]; unknown/null falls back to [default]. */ fun parsePastEventDisplay( stored: String?, @@ -183,6 +186,7 @@ class SettingsPrefs @Inject constructor( val agendaShowRangeBar: Flow = prefs.flow(SettingsKeys.agendaShowRangeBar) val agendaShowToday: Flow = prefs.flow(SettingsKeys.agendaShowToday) val defaultView: Flow = prefs.flow(SettingsKeys.defaultView) + val externalDateView: Flow = prefs.flow(SettingsKeys.externalDateView) val quickSwitchConfig: Flow = prefs.flow(SettingsKeys.quickSwitchConfig) val drawerViewOrder: Flow> = prefs.flow(SettingsKeys.drawerViewOrder) val defaultFormFields: Flow> = prefs.flow(SettingsKeys.defaultFormFields) @@ -290,6 +294,7 @@ class SettingsPrefs @Inject constructor( suspend fun setAgendaShowRangeBar(enabled: Boolean) = prefs.set(SettingsKeys.agendaShowRangeBar, enabled) suspend fun setAgendaShowToday(enabled: Boolean) = prefs.set(SettingsKeys.agendaShowToday, enabled) suspend fun setDefaultView(view: CalendarView) = prefs.set(SettingsKeys.defaultView, view) + suspend fun setExternalDateView(view: ExternalDateView) = prefs.set(SettingsKeys.externalDateView, view) /** * Atomic read-modify-write of the quick-switch config: [transform] sees the @@ -792,6 +797,9 @@ object SettingsKeys { */ val defaultView: Pref = enumPref("default_view", CalendarView.Week) + /** What a launcher or clock date tap opens (#524); our own notifications and widgets keep the day view. */ + val externalDateView: Pref = enumPref("external_date_view", ExternalDateView.DAY) + /** * Quick-switch button customisation (#24): the ordered full view list plus * which views are enabled in the cycle. Stored comma-joined by enum name, a 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 812ac5fa..00cc7e41 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -44,6 +44,7 @@ import androidx.compose.ui.layout.positionInWindow import androidx.compose.ui.platform.LocalDensity import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import de.jeanlucmakiola.calendula.data.prefs.ExternalDateView import de.jeanlucmakiola.calendula.data.time.currentDate import de.jeanlucmakiola.calendula.domain.EventForm import de.jeanlucmakiola.calendula.domain.EventInstance @@ -431,11 +432,17 @@ fun CalendarHost( } when (val req = widgetNavRequest) { is WidgetNavRequest.OpenDate -> { - // Drop any overlay that would cover the day, so the open doesn't + // Another app's date tap opens the day view or the default view (#524). + val stack = req.stack( + defaultView, + if (req.external) viewModel.externalDateView() else ExternalDateView.DAY, + ) + // Drop any overlay that would cover the date, so the open doesn't // land under Settings/form. dismissCoveringOverlays() - focus.navigateTo(LocalDate.parse(req.dateIso)) - viewStack = listOf(defaultView).drillToDay() + val date = LocalDate.parse(req.dateIso) + if (stack.last() == CalendarView.Day) focus.navigateTo(date) else focus.showDate(date) + viewStack = stack requests.leaveTarget = req.leaveTarget requests.nav = null } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHostViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHostViewModel.kt index cba2890c..e3eb227e 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHostViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHostViewModel.kt @@ -3,6 +3,7 @@ package de.jeanlucmakiola.calendula.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel +import de.jeanlucmakiola.calendula.data.prefs.ExternalDateView import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.calendula.data.time.TodaySource import de.jeanlucmakiola.calendula.data.time.currentDate @@ -77,4 +78,9 @@ class CalendarHostViewModel @Inject constructor( /** Whether tapping an event opens the edit form rather than the detail screen (#178). */ suspend fun tapOpensEditor(): Boolean = tapOpensEditorPref.first() + + private val externalDateViewPref = prefs.externalDateView + + /** What another app's date tap opens (#524). */ + suspend fun externalDateView(): ExternalDateView = externalDateViewPref.first() } 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 9c0f54a7..0581ce7c 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/WidgetNavRequest.kt @@ -1,7 +1,13 @@ package de.jeanlucmakiola.calendula.ui +import android.content.Intent +import de.jeanlucmakiola.calendula.data.prefs.ExternalDateView import de.jeanlucmakiola.calendula.ui.common.CalendarView +import de.jeanlucmakiola.calendula.ui.common.drillToDay +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toLocalDateTime import java.io.Serializable +import kotlin.time.Instant /** * A navigation a home-screen widget asked the app to perform when launched. @@ -16,8 +22,13 @@ 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 * launcher/clock date tap (issue #9) or the daily summary backs out to home. + * [external] marks another app's date tap, which follows [ExternalDateView] (#524). */ - data class OpenDate(val dateIso: String, val fromWidget: Boolean) : WidgetNavRequest + data class OpenDate( + val dateIso: String, + val fromWidget: Boolean, + val external: Boolean = false, + ) : WidgetNavRequest /** Open one occurrence's detail (a widget event tap), over [source]; back leaves the app (#530). */ data class OpenEvent( @@ -40,6 +51,33 @@ sealed interface WidgetNavRequest : Serializable { data class OpenView(val view: CalendarView?) : WidgetNavRequest } +/** The view stack [this] lands on over [defaultView]: the day view, unless another app's date tap is set to the default view (#524). */ +fun WidgetNavRequest.OpenDate.stack(defaultView: CalendarView, externalView: ExternalDateView): List = + if (external && externalView == ExternalDateView.DEFAULT) listOf(defaultView) else listOf(defaultView).drillToDay() + +/** + * The date request of the system "view time" intent (#9): ACTION_VIEW on + * `content://com.android.calendar/time/`, dated in [zone]. [ownDayTap] + * marks our own daily summary's tap (#459), which stays on the day view. Null for + * any other intent. + */ +fun viewTimeRequest( + action: String?, + host: String?, + pathSegments: List, + zone: TimeZone, + ownDayTap: Boolean, +): WidgetNavRequest.OpenDate? { + if (action != Intent.ACTION_VIEW || host != CALENDAR_PROVIDER_HOST) return null + if (pathSegments.firstOrNull() != "time") return null + val millis = pathSegments.getOrNull(1)?.toLongOrNull() ?: return null + val date = Instant.fromEpochMilliseconds(millis).toLocalDateTime(zone).date + return WidgetNavRequest.OpenDate(date.toString(), fromWidget = false, external = !ownDayTap) +} + +/** The calendar provider's authority, which the system's calendar intents address. */ +const val CALENDAR_PROVIDER_HOST = "com.android.calendar" + /** 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. */ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/agenda/AgendaScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/agenda/AgendaScreen.kt index 7a55c64e..7989c7c9 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/agenda/AgendaScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/agenda/AgendaScreen.kt @@ -6,6 +6,7 @@ import de.jeanlucmakiola.calendula.ui.common.ScreenActions import de.jeanlucmakiola.calendula.ui.common.ScreenBody import de.jeanlucmakiola.calendula.ui.common.ScreenChrome import de.jeanlucmakiola.calendula.ui.common.LocalViewFocus +import de.jeanlucmakiola.calendula.ui.common.TakeDateRequest import kotlinx.coroutines.flow.drop import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue @@ -93,12 +94,31 @@ fun AgendaScreen( // the top. The top day still goes back to the shared focus. val focus = LocalViewFocus.current val opened = rememberSaveable { mutableStateOf(false) } - val restoreTop = remember(viewModel) { - if (restorePosition || opened.value) { - viewModel.lastTopDate + val scope = rememberCoroutineScope() + val listState = rememberLazyListState() + // Past days load above the anchor (#367), so the anchor's row counts as the + // start of the list. + val anchorIndex = remember { mutableIntStateOf(0) } + // Another app's date (#524) is where it opens; one asked for while it is up moves it there. + val requestedDate = TakeDateRequest(viewModel) { date -> + focus?.date = date + if (viewModel.anchor.value == date) { + scope.launch { listState.animateScrollToItem(anchorIndex.intValue) } } else { - viewModel.goToToday() - null + viewModel.goToDate(date) + } + } + val restoreTop = remember(viewModel) { + when { + requestedDate != null -> { + viewModel.goToDate(requestedDate) + null + } + restorePosition || opened.value -> viewModel.lastTopDate + else -> { + viewModel.goToToday() + null + } } } SideEffect { opened.value = true } @@ -108,7 +128,6 @@ fun AgendaScreen( val showToday by viewModel.showToday.collectAsStateWithLifecycle() val weekStart by viewModel.weekStart.collectAsStateWithLifecycle() - val scope = rememberCoroutineScope() var showRangePicker by remember { mutableStateOf(false) } val isOnToday = when (val s = state) { @@ -120,10 +139,6 @@ fun AgendaScreen( // The agenda window always starts at the anchor, so moving through it is a // list scroll, not an anchor change — "today" has to bring the list back to // the top as well, and stay offered while it is scrolled away (#305). - val listState = rememberLazyListState() - // Past days load above the anchor (#367), so the anchor's row counts as the - // start of the list. - val anchorIndex = remember { mutableIntStateOf(0) } val scrolledAway by remember { derivedStateOf { listState.firstVisibleItemIndex != anchorIndex.intValue || diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ViewFocus.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ViewFocus.kt index 27bf4a07..d4615f40 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ViewFocus.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ViewFocus.kt @@ -1,10 +1,12 @@ package de.jeanlucmakiola.calendula.ui.common import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.Stable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.saveable.Saver import androidx.compose.runtime.setValue import androidx.compose.runtime.staticCompositionLocalOf @@ -29,8 +31,20 @@ class ViewFocus(initial: LocalDate) { date = target navigationVersion++ } + + /** A date another app asked to see (#524), until the screen on show takes it. */ + var dateRequest by mutableStateOf(null) + + /** [navigateTo], also moving a screen that is already on show, the agenda included. */ + fun showDate(target: LocalDate) { + navigateTo(target) + dateRequest = DateRequest(target) + } } +/** One [ViewFocus.showDate]; compared by identity, so asking for the same date twice is two requests. */ +class DateRequest(val date: LocalDate) + val ViewFocusSaver: Saver = Saver( save = { it.date.toString() }, restore = { ViewFocus(LocalDate.parse(it)) }, @@ -47,3 +61,23 @@ fun EnterOnFocus(key: T, enter: (LocalDate) -> Unit) { val focus = LocalViewFocus.current remember(key) { focus?.date?.also(enter) } } + +/** + * Takes a pending [ViewFocus.dateRequest] while this screen is the one on show. + * Returns the date asked for as the screen came on ([key]), for it to open on; + * one arriving while it is already up goes to [onShown] instead. + */ +@Composable +fun TakeDateRequest(key: T, onShown: (LocalDate) -> Unit = {}): LocalDate? { + val focus = LocalViewFocus.current ?: return null + val current = LocalChromeSlot.current?.current ?: true + val request = focus.dateRequest?.takeIf { current } + val atEntry = remember(key) { request } + val shown by rememberUpdatedState(onShown) + LaunchedEffect(request) { + if (request == null) return@LaunchedEffect + focus.dateRequest = null + if (request !== atEntry) shown(request.date) + } + return atEntry?.date +} diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/month/MonthScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/month/MonthScreen.kt index 7a78eb26..6b39e27a 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/month/MonthScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/month/MonthScreen.kt @@ -25,6 +25,7 @@ import de.jeanlucmakiola.calendula.ui.common.ScreenChrome import de.jeanlucmakiola.calendula.ui.common.LocalViewMorph import de.jeanlucmakiola.calendula.ui.common.EnterOnFocus import de.jeanlucmakiola.calendula.ui.common.LocalViewFocus +import de.jeanlucmakiola.calendula.ui.common.TakeDateRequest import de.jeanlucmakiola.calendula.ui.common.ViewMorphKey import de.jeanlucmakiola.calendula.ui.common.morphDay import de.jeanlucmakiola.calendula.ui.common.morphEvent @@ -480,6 +481,8 @@ fun MonthScreen( jumpToMonth(target.yearMonth) } } + // Another app's date asked for while the month is already up (#524); on entry the focus did it. + TakeDateRequest(viewModel, onShown = jumpToDate) PublishChrome( ScreenChrome( diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt index a296674f..6c617aa6 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt @@ -5,6 +5,7 @@ import de.jeanlucmakiola.calendula.domain.reminders.DailyDigestSettings import de.jeanlucmakiola.calendula.domain.reminders.FixedTimeReminder import android.net.Uri import de.jeanlucmakiola.floret.reminders.ReminderOverride +import de.jeanlucmakiola.calendula.data.prefs.ExternalDateView import de.jeanlucmakiola.calendula.data.prefs.PastEventDisplay import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.floret.prefs.ThemeMode @@ -62,6 +63,8 @@ data class SettingsUiState( val agendaShowRangeBar: Boolean = true, /** The calendar view the app opens on, and the home of the view back stack (M1). */ val defaultView: CalendarView = CalendarView.Week, + /** What a date tap from another app opens (#524). */ + val externalDateView: ExternalDateView = ExternalDateView.DAY, /** Which views the top-bar quick-switch button cycles through, and their order (#24). */ val quickSwitchConfig: QuickSwitchConfig = QuickSwitchConfig.Default, /** How the Month view lays itself out: pages, continuous scroll, or split (#38, #53). */ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt index 49915cc5..cf9aa8b1 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt @@ -30,6 +30,7 @@ import de.jeanlucmakiola.calendula.domain.reminders.FixedTimeReminder import de.jeanlucmakiola.floret.reminders.ReminderOverride import de.jeanlucmakiola.floret.reminders.reminderOverrideFor import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefs +import de.jeanlucmakiola.calendula.data.prefs.ExternalDateView import de.jeanlucmakiola.calendula.data.prefs.PastEventDisplay import de.jeanlucmakiola.calendula.data.prefs.SettingsKeys import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs @@ -150,6 +151,7 @@ class SettingsViewModel @Inject constructor( agendaShowToday = this[SettingsKeys.agendaShowToday], agendaShowRangeBar = this[SettingsKeys.agendaShowRangeBar], defaultView = this[SettingsKeys.defaultView], + externalDateView = this[SettingsKeys.externalDateView], quickSwitchConfig = this[SettingsKeys.quickSwitchConfig], monthViewStyle = this[SettingsKeys.monthViewStyle], timelineScale = this[SettingsKeys.timelineScale], @@ -373,6 +375,7 @@ class SettingsViewModel @Inject constructor( fun setDimCompletedEvents(enabled: Boolean) = save { setDimCompletedEvents(enabled) } fun setDefaultView(view: CalendarView) = save { setDefaultView(view) } + fun setExternalDateView(view: ExternalDateView) = save { setExternalDateView(view) } fun setFormFieldDefault(field: EventFormField, enabled: Boolean) = save { setFormFieldDefault(field, enabled) } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/ViewsSettings.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/ViewsSettings.kt index 772b21aa..d36c5c3f 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/ViewsSettings.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/ViewsSettings.kt @@ -25,6 +25,7 @@ import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import de.jeanlucmakiola.calendula.R +import de.jeanlucmakiola.calendula.data.prefs.ExternalDateView import de.jeanlucmakiola.calendula.data.prefs.PastEventDisplay import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.calendula.data.prefs.TimeFormatPref @@ -76,6 +77,7 @@ internal fun ViewsScreen( ) { var showMonthStyle by remember { mutableStateOf(false) } var showDefaultView by remember { mutableStateOf(false) } + var showExternalDateView by remember { mutableStateOf(false) } var showWeekStart by remember { mutableStateOf(false) } var showTimeFormat by remember { mutableStateOf(false) } var showTapOpens by remember { mutableStateOf(false) } @@ -103,6 +105,12 @@ internal fun ViewsScreen( position = Position.Top, onClick = { showDefaultView = true }, ) + GroupedRow( + title = stringResource(R.string.settings_external_date_view), + summary = externalDateViewLabel(state.externalDateView), + position = Position.Middle, + onClick = { showExternalDateView = true }, + ) GroupedRow( title = stringResource(R.string.settings_week_start), summary = weekStartLabel(state.weekStart).capitalizeStart(currentLocale()), @@ -328,6 +336,17 @@ internal fun ViewsScreen( onDismiss = { showDefaultView = false }, ) } + if (showExternalDateView) { + OptionPicker( + title = stringResource(R.string.settings_external_date_view), + predictiveBack = true, + options = ExternalDateView.entries, + selected = state.externalDateView, + label = { externalDateViewLabel(it) }, + onSelect = viewModel::setExternalDateView, + onDismiss = { showExternalDateView = false }, + ) + } if (showWeekStart) { WeekStartPicker( selected = state.weekStart, @@ -519,6 +538,14 @@ internal fun weekStartLabel(pref: WeekStartPref): String = when (pref) { .getDisplayName(JavaTextStyle.FULL, currentLocale()) } +@Composable +private fun externalDateViewLabel(view: ExternalDateView): String = stringResource( + when (view) { + ExternalDateView.DAY -> R.string.settings_external_date_day + ExternalDateView.DEFAULT -> R.string.settings_external_date_default + }, +) + @Composable private fun tapOpensLabel(editor: Boolean): String = stringResource( if (editor) R.string.settings_tap_opens_editor else R.string.settings_tap_opens_details, diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt index 0f00f711..4c559dfc 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/week/WeekScreen.kt @@ -184,6 +184,7 @@ import de.jeanlucmakiola.calendula.ui.common.rememberOutOfViewEdges import de.jeanlucmakiola.calendula.ui.common.rememberCurrentMinute import de.jeanlucmakiola.calendula.ui.common.EnterOnFocus import de.jeanlucmakiola.calendula.ui.common.LocalViewFocus +import de.jeanlucmakiola.calendula.ui.common.TakeDateRequest import de.jeanlucmakiola.calendula.ui.common.rememberCalendarFadeSpec import de.jeanlucmakiola.calendula.ui.common.ViewMorphKey import de.jeanlucmakiola.calendula.ui.common.ViewMorphPage @@ -354,6 +355,8 @@ private fun PagedTimelineScreen( val navigationVersion = focus?.navigationVersion ?: 0 var handledNavigation by remember { mutableIntStateOf(navigationVersion) } EnterOnFocus(viewModel) { viewModel.jumpTo(it) } + // The focus moves the timeline already; this only clears another app's date request (#524). + TakeDateRequest(Unit) // Keyed on the view model: collected unkeyed, a switch would read the view // before's anchor and grid for a frame and open its pager. val loadedState by key(viewModel) { viewModel.state.collectAsStateWithLifecycle() } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index eb53ffa0..03b8e6e2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -591,6 +591,9 @@ Currently %1$s Default view + Dates opened from other apps + Day view + Default view Dynamic colour Requires Android 12 or newer Harmonise calendar colours diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/ExternalDateTapTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/ExternalDateTapTest.kt new file mode 100644 index 00000000..aa3dccb1 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/ExternalDateTapTest.kt @@ -0,0 +1,62 @@ +package de.jeanlucmakiola.calendula.ui + +import android.content.Intent +import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.calendula.data.prefs.ExternalDateView +import de.jeanlucmakiola.calendula.ui.common.CalendarView +import kotlinx.datetime.LocalDateTime +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toInstant +import org.junit.jupiter.api.Test + +class ExternalDateTapTest { + + private val zone = TimeZone.of("Europe/Berlin") + private val lateEvening = LocalDateTime(2026, 11, 30, 23, 30).toInstant(zone).toEpochMilliseconds() + + private fun request( + action: String? = Intent.ACTION_VIEW, + host: String? = CALENDAR_PROVIDER_HOST, + segments: List = listOf("time", lateEvening.toString()), + ownDayTap: Boolean = false, + ) = viewTimeRequest(action, host, segments, zone, ownDayTap) + + @Test + fun `a launcher date tap opens its local date and follows the setting`() { + assertThat(request()).isEqualTo(WidgetNavRequest.OpenDate("2026-11-30", fromWidget = false, external = true)) + } + + @Test + fun `the daily summary's own tap stays on the day view`() { + assertThat(request(ownDayTap = true)?.external).isFalse() + } + + @Test + fun `other intents are no date tap`() { + assertThat(request(action = Intent.ACTION_EDIT)).isNull() + assertThat(request(host = "com.example")).isNull() + assertThat(request(segments = listOf("events", "12"))).isNull() + assertThat(request(segments = listOf("time"))).isNull() + assertThat(request(segments = listOf("time", "noon"))).isNull() + } + + @Test + fun `an external tap set to the default view lands on it`() { + val tap = request()!! + for (view in CalendarView.entries) { + assertThat(tap.stack(view, ExternalDateView.DEFAULT)).containsExactly(view) + } + assertThat(tap.stack(CalendarView.Month, ExternalDateView.DAY)) + .containsExactly(CalendarView.Month, CalendarView.Day).inOrder() + } + + @Test + fun `our own date opens always drill into the day`() { + val own = request(ownDayTap = true)!! + val widget = WidgetNavRequest.OpenDate("2026-11-30", fromWidget = true) + for (open in listOf(own, widget)) { + assertThat(open.stack(CalendarView.Agenda, ExternalDateView.DEFAULT)) + .containsExactly(CalendarView.Agenda, CalendarView.Day).inOrder() + } + } +}