From e88ad7f5339723d5b671417da76d61a4a48c68c1 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 13 Nov 2026 22:10:04 +0100 Subject: [PATCH] fix(#240): guard double picker launch, flag skipped accounts in the receipt, translatable grant summary --- .../data/calendar/CalendarRepositoryImpl.kt | 2 +- .../calendula/ui/CalendarHost.kt | 30 +++++++++++-------- .../calendula/ui/common/CalendarDrawer.kt | 12 ++++---- app/src/main/res/values/strings.xml | 2 +- .../calendula/data/calendar/SyncNudgeTest.kt | 2 +- docs/ARCHITECTURE.md | 2 +- 6 files changed, 27 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt index b3c614a8..7c11eac9 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepositoryImpl.kt @@ -206,7 +206,7 @@ class CalendarRepositoryImpl @Inject constructor( val sent = targets.reachable.count { requestSync(it, userInitiated = true) } when { sent == 0 -> SyncRequestResult.Failed - sent < targets.reachable.size -> SyncRequestResult.PartlyFailed + sent < targets.reachable.size || targets.needsAccess.isNotEmpty() -> SyncRequestResult.PartlyFailed else -> SyncRequestResult.Requested } } 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 6576138f..cd6bbfa3 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -461,18 +461,21 @@ fun CalendarHost( val syncResult by sync.result.collectAsStateWithLifecycle() val selectionFailureMessage = eventActionFailureMessage(selectionFailure) val syncMessage = syncResult?.let { stringResource(it.messageRes()) } - // The system account picker grants access to one account per pick; whatever - // the outcome, access is re-read rather than trusted from the result. - // Access can also be granted or revoked in system settings: re-read on every - // resume, which covers the picker returning too. - val accessPicker = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {} + // Access is re-read on resume (covering the picker's return), never trusted from the result. + var accessPickerOpen by remember { mutableStateOf(false) } + val accessPicker = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { + accessPickerOpen = false + } LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { sync.refreshAccess() } val accessPickerDescription = stringResource(R.string.sync_access_picker_description) val launchAccessPicker: (Set) -> Unit = { accounts -> - try { - accessPicker.launch(accountAccessIntent(accounts, accessPickerDescription)) - } catch (e: ActivityNotFoundException) { - Log.w("CalendarHost", "No account picker", e) + if (!accessPickerOpen) { + try { + accessPicker.launch(accountAccessIntent(accounts, accessPickerDescription)) + accessPickerOpen = true + } catch (e: ActivityNotFoundException) { + Log.w("CalendarHost", "No account picker", e) + } } } val onEditEvent: (EventInstance) -> Unit = { event -> editKey = event.occurrenceKey() } @@ -572,6 +575,9 @@ fun CalendarHost( val stayingPut = remember { StayingPut() } val chromeHost = remember { ChromeHost() } val drawerState = rememberDrawerState(DrawerValue.Closed) + LaunchedEffect(drawerState.targetValue) { + if (drawerState.targetValue == DrawerValue.Open) sync.refreshAccess() + } val density = LocalDensity.current val topBarBottom = TopAppBarDefaults.windowInsets.getTop(density) + with(density) { TopAppBarDefaults.TopAppBarExpandedHeight.toPx() } @@ -622,17 +628,15 @@ fun CalendarHost( onOpenSettings() switchScope.launch { drawerState.close() } }, - sync = remember(syncTargets, syncInFlight) { + sync = remember(syncTargets, syncInFlight, accessPickerDescription) { DrawerSync( canSync = syncTargets.reachable.isNotEmpty(), inFlight = syncInFlight, - needsAccess = syncTargets.needsAccess.map { it.name }.distinct(), + needsAccess = syncTargets.needsAccess.isNotEmpty(), onSync = { sync.requestSync() switchScope.launch { drawerState.close() } }, - // The drawer stays open: one pick grants one account, - // and the row is right there for the next. onGrantAccess = { launchAccessPicker(syncTargets.needsAccess) }, ) }, diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarDrawer.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarDrawer.kt index 2d5b567e..d115d61b 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarDrawer.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/CalendarDrawer.kt @@ -150,27 +150,27 @@ fun CalendarDrawer( /** * The drawer's sync rows: "Sync now" when [canSync] ([inFlight] shows progress - * and ignores taps), and a grant row naming the [needsAccess] accounts. + * and ignores taps), and a grant row while some account [needsAccess]. */ data class DrawerSync( val canSync: Boolean, val inFlight: Boolean, - val needsAccess: List, + val needsAccess: Boolean, val onSync: () -> Unit, val onGrantAccess: () -> Unit, ) { - val hasRows: Boolean get() = canSync || needsAccess.isNotEmpty() + val hasRows: Boolean get() = canSync || needsAccess } @Composable private fun SyncRows(sync: DrawerSync) { if (sync.canSync) { - SyncNowRow(sync, if (sync.needsAccess.isEmpty()) Position.Bottom else Position.Middle) + SyncNowRow(sync, if (sync.needsAccess) Position.Middle else Position.Bottom) } - if (sync.needsAccess.isNotEmpty()) { + if (sync.needsAccess) { GroupedRow( title = stringResource(R.string.drawer_sync_access), - summary = stringResource(R.string.drawer_sync_access_summary, sync.needsAccess.joinToString()), + summary = stringResource(R.string.drawer_sync_access_summary), position = Position.Bottom, minHeight = 56.dp, leading = { DrawerLeadingIcon(Icons.Filled.ManageAccounts) }, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 62ea96d1..5a27b757 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -397,7 +397,7 @@ Jump to date Sync now Allow sync requests - Calendula can\'t ask %1$s to sync yet + Pick each account Calendula may ask to sync Choose an account Calendula may ask to sync your calendars Sync requested Couldn\'t request a sync diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt index 5f5b956f..02cbb189 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/SyncNudgeTest.kt @@ -233,7 +233,7 @@ class SyncNudgeTest { assertThat(repo.syncTargets.first()).isEqualTo(SyncTargets(setOf(dav), setOf(google))) sync.requests.clear() - assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.Requested) + assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.PartlyFailed) assertThat(sync.requests).containsExactly(Request(dav, true)) } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 5a839865..759a1a4a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -24,7 +24,7 @@ the package list (recurring writes, save conflicts, reminder delivery). drawer offers "Allow sync requests" instead, which opens the system account picker (`AccountManager.newChooseAccountIntent`) — the picked account becomes visible to the app. Access is re-read on every request and on - every resume, so a revoke simply puts the account back behind the + every resume and drawer open, so a revoke simply puts the account back behind the grant row. Whether and when the adapter syncs stays its call. 2. **Observer-driven UI.** A `ContentObserver` on the provider triggers re-queries; every screen recomposes from fresh provider state. After a