From 14a0725b219ad7cfb480c41c95fd58e64bb394ea Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 13 Nov 2026 22:18:09 +0100 Subject: [PATCH] refactor(#240): grant-flow state in its own helper, clearer receipt when accounts need access --- .../data/calendar/CalendarRepositoryImpl.kt | 10 ++-- .../calendula/data/calendar/SyncRequester.kt | 2 +- .../calendula/ui/CalendarHost.kt | 35 ++---------- .../calendula/ui/common/SyncAccessPicker.kt | 55 +++++++++++++++++++ .../calendula/ui/common/SyncViewModel.kt | 5 +- app/src/main/res/values/strings.xml | 1 + .../calendula/data/calendar/SyncNudgeTest.kt | 3 +- 7 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncAccessPicker.kt 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 7c11eac9..5179a433 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 @@ -161,9 +161,10 @@ class CalendarRepositoryImpl @Inject constructor( return SyncTargets(reachable.toSet(), hidden.toSet()) } - /** Ask each reachable one of [accounts] once; returns how many were asked. */ - private fun requestSyncs(accounts: Set, userInitiated: Boolean): Int = - reach(accounts).reachable.count { requestSync(it, userInitiated) } + /** Ask each reachable one of [accounts] once. */ + private fun requestSyncs(accounts: Set, userInitiated: Boolean) { + reach(accounts).reachable.forEach { requestSync(it, userInitiated) } + } private fun requestSync(account: SyncAccount, userInitiated: Boolean): Boolean = try { syncRequester.requestSync(account, userInitiated) @@ -206,7 +207,8 @@ class CalendarRepositoryImpl @Inject constructor( val sent = targets.reachable.count { requestSync(it, userInitiated = true) } when { sent == 0 -> SyncRequestResult.Failed - sent < targets.reachable.size || targets.needsAccess.isNotEmpty() -> SyncRequestResult.PartlyFailed + sent < targets.reachable.size -> SyncRequestResult.PartlyFailed + targets.needsAccess.isNotEmpty() -> SyncRequestResult.SomeNeedAccess else -> SyncRequestResult.Requested } } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt index 94ed46ee..e22146f8 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/SyncRequester.kt @@ -104,4 +104,4 @@ fun accountAccessIntent(accounts: Collection, description: String): ) /** Outcome of a user-triggered sync request (#240). */ -enum class SyncRequestResult { Requested, PartlyFailed, NeedsAccess, NothingToSync, Failed } +enum class SyncRequestResult { Requested, SomeNeedAccess, PartlyFailed, NeedsAccess, NothingToSync, Failed } 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 cd6bbfa3..e7eef1b1 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -1,12 +1,6 @@ package de.jeanlucmakiola.calendula.ui -import android.content.ActivityNotFoundException -import android.util.Log import androidx.activity.compose.BackHandler -import androidx.activity.compose.rememberLauncherForActivityResult -import androidx.activity.result.contract.ActivityResultContracts -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.compose.LifecycleEventEffect import androidx.activity.compose.PredictiveBackHandler import androidx.compose.animation.AnimatedContent import androidx.compose.animation.AnimatedVisibility @@ -50,7 +44,6 @@ 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.R import de.jeanlucmakiola.calendula.data.time.currentDate import de.jeanlucmakiola.calendula.domain.EventForm import de.jeanlucmakiola.calendula.domain.EventInstance @@ -60,10 +53,10 @@ import de.jeanlucmakiola.calendula.ui.calendars.CalendarsScreen import de.jeanlucmakiola.calendula.ui.common.BelowTopBarClip import de.jeanlucmakiola.calendula.ui.common.StayingPut import de.jeanlucmakiola.calendula.ui.common.CalendarDrawer -import de.jeanlucmakiola.calendula.data.calendar.SyncAccount -import de.jeanlucmakiola.calendula.data.calendar.accountAccessIntent import de.jeanlucmakiola.calendula.ui.common.DrawerSync import de.jeanlucmakiola.calendula.ui.common.SyncViewModel +import de.jeanlucmakiola.calendula.ui.common.RefreshSyncAccessOnOpen +import de.jeanlucmakiola.calendula.ui.common.rememberSyncAccessPicker import de.jeanlucmakiola.calendula.ui.common.messageRes import de.jeanlucmakiola.calendula.ui.common.CalendarFab import de.jeanlucmakiola.calendula.ui.common.CalendarTopBar @@ -461,23 +454,7 @@ fun CalendarHost( val syncResult by sync.result.collectAsStateWithLifecycle() val selectionFailureMessage = eventActionFailureMessage(selectionFailure) val syncMessage = syncResult?.let { stringResource(it.messageRes()) } - // 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 -> - if (!accessPickerOpen) { - try { - accessPicker.launch(accountAccessIntent(accounts, accessPickerDescription)) - accessPickerOpen = true - } catch (e: ActivityNotFoundException) { - Log.w("CalendarHost", "No account picker", e) - } - } - } + val launchAccessPicker = rememberSyncAccessPicker(sync) val onEditEvent: (EventInstance) -> Unit = { event -> editKey = event.occurrenceKey() } // A tap opens the edit form instead of the detail screen when the user chose // so (#178) — only for events whose calendar takes writes. Both are read at @@ -575,9 +552,7 @@ 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() - } + RefreshSyncAccessOnOpen(drawerState, sync) val density = LocalDensity.current val topBarBottom = TopAppBarDefaults.windowInsets.getTop(density) + with(density) { TopAppBarDefaults.TopAppBarExpandedHeight.toPx() } @@ -628,7 +603,7 @@ fun CalendarHost( onOpenSettings() switchScope.launch { drawerState.close() } }, - sync = remember(syncTargets, syncInFlight, accessPickerDescription) { + sync = remember(syncTargets, syncInFlight, launchAccessPicker) { DrawerSync( canSync = syncTargets.reachable.isNotEmpty(), inFlight = syncInFlight, diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncAccessPicker.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncAccessPicker.kt new file mode 100644 index 00000000..ef02bec6 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncAccessPicker.kt @@ -0,0 +1,55 @@ +package de.jeanlucmakiola.calendula.ui.common + +import android.content.ActivityNotFoundException +import android.util.Log +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.material3.DrawerState +import androidx.compose.material3.DrawerValue +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.res.stringResource +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.compose.LifecycleEventEffect +import de.jeanlucmakiola.calendula.R +import de.jeanlucmakiola.calendula.data.calendar.SyncAccount +import de.jeanlucmakiola.calendula.data.calendar.accountAccessIntent + +/** + * Launches the system account picker for accounts sync requests can't reach yet + * (#240), one launch at a time. Access is re-read on every resume, which covers + * the picker's return and grants or revokes made in system settings. + */ +@Composable +fun rememberSyncAccessPicker(sync: SyncViewModel): (Set) -> Unit { + var open by remember { mutableStateOf(false) } + val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { + open = false + } + LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { sync.refreshAccess() } + val description = stringResource(R.string.sync_access_picker_description) + return remember(launcher, description) { + { accounts -> + if (!open) { + try { + launcher.launch(accountAccessIntent(accounts, description)) + open = true + } catch (e: ActivityNotFoundException) { + Log.w("SyncAccessPicker", "No account picker", e) + } + } + } + } +} + +/** Re-reads account access whenever [drawerState] opens; the app may stay resumed in multi-window. */ +@Composable +fun RefreshSyncAccessOnOpen(drawerState: DrawerState, sync: SyncViewModel) { + LaunchedEffect(drawerState.targetValue) { + if (drawerState.targetValue == DrawerValue.Open) sync.refreshAccess() + } +} diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModel.kt index 4207019e..ca8bf8ea 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModel.kt @@ -69,7 +69,9 @@ class SyncViewModel @Inject constructor( } val outcome = repository.requestSync() _result.value = outcome - val sent = outcome == SyncRequestResult.Requested || outcome == SyncRequestResult.PartlyFailed + val sent = outcome == SyncRequestResult.Requested || + outcome == SyncRequestResult.SomeNeedAccess || + outcome == SyncRequestResult.PartlyFailed if (sent) settled.await() else settled.cancel() } finally { _inFlight.value = false @@ -92,6 +94,7 @@ fun SyncRequestResult.messageRes(): Int = when (this) { SyncRequestResult.Requested -> R.string.sync_requested SyncRequestResult.PartlyFailed -> R.string.sync_request_partly_failed SyncRequestResult.NeedsAccess -> R.string.sync_request_needs_access + SyncRequestResult.SomeNeedAccess -> R.string.sync_request_some_need_access SyncRequestResult.NothingToSync -> R.string.sync_nothing_to_sync SyncRequestResult.Failed -> R.string.sync_request_failed } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5a27b757..9d96eac3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -401,6 +401,7 @@ Choose an account Calendula may ask to sync your calendars Sync requested Couldn\'t request a sync + Sync requested. Allow sync requests in the menu for the other accounts Allow sync requests in the menu first Sync requested, but not for every account No synced calendars to 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 02cbb189..849f41eb 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.PartlyFailed) + assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.SomeNeedAccess) assertThat(sync.requests).containsExactly(Request(dav, true)) } @@ -247,6 +247,7 @@ class SyncNudgeTest { assertThat(repo.syncTargets.first()).isEqualTo(SyncTargets(setOf(dav, google), emptySet())) assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.Requested) + assertThat(sync.requests).containsExactly(Request(dav, true), Request(google, true)) } @Test