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 09a2e8a4..b3c614a8 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 @@ -149,11 +149,12 @@ class CalendarRepositoryImpl @Inject constructor( */ private fun reach(accounts: Set): SyncTargets { if (accounts.isEmpty()) return SyncTargets.None + // Unreadable access counts as reachable: a grant can't fix a failing read. val addressable = try { syncRequester.addressableAccounts(accounts.mapTo(mutableSetOf()) { it.type }) } catch (e: Exception) { Log.w(TAG, "Account visibility unreadable", e) - emptySet() + null } if (addressable == null) return SyncTargets(reachable = accounts, needsAccess = emptySet()) val (reachable, hidden) = accounts.partition { it in addressable } @@ -199,13 +200,13 @@ class CalendarRepositoryImpl @Inject constructor( return@withContext if (targets.needsAccess.isEmpty()) { SyncRequestResult.NothingToSync } else { - SyncRequestResult.Failed + SyncRequestResult.NeedsAccess } } 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 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 d81acafe..94ed46ee 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, NothingToSync, Failed } +enum class SyncRequestResult { Requested, 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 9cd7e5c1..6576138f 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -5,6 +5,8 @@ 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 @@ -461,9 +463,10 @@ fun CalendarHost( 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. - val accessPicker = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { - sync.refreshAccess() - } + // 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()) {} + LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { sync.refreshAccess() } val accessPickerDescription = stringResource(R.string.sync_access_picker_description) val launchAccessPicker: (Set) -> Unit = { accounts -> try { @@ -569,11 +572,6 @@ fun CalendarHost( val stayingPut = remember { StayingPut() } val chromeHost = remember { ChromeHost() } val drawerState = rememberDrawerState(DrawerValue.Closed) - // Account access can be granted or revoked outside the app; re-read it - // whenever the drawer that offers the grant opens. - 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() } @@ -628,15 +626,14 @@ fun CalendarHost( DrawerSync( canSync = syncTargets.reachable.isNotEmpty(), inFlight = syncInFlight, - needsAccess = syncTargets.needsAccess.map { it.name }, + needsAccess = syncTargets.needsAccess.map { it.name }.distinct(), onSync = { sync.requestSync() switchScope.launch { drawerState.close() } }, - onGrantAccess = { - launchAccessPicker(syncTargets.needsAccess) - 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/SyncViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModel.kt index bf38193e..4207019e 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 @@ -45,7 +45,7 @@ class SyncViewModel @Inject constructor( .flatMapLatest { repository.syncTargets.catch { emit(SyncTargets.None) } } .stateWhileSubscribed(scope = viewModelScope, initialValue = SyncTargets.None) - /** Re-read account access: after the picker returns, and whenever the drawer opens. */ + /** Re-read account access: whenever the app resumes, which includes the picker returning. */ fun refreshAccess() { accessChecks.update { it + 1 } } @@ -91,6 +91,7 @@ class SyncViewModel @Inject constructor( 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.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 c5504106..62ea96d1 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 + 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 a9ea68d9..5f5b956f 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.Requested) assertThat(sync.requests).containsExactly(Request(dav, true)) } @@ -250,24 +250,24 @@ class SyncNudgeTest { } @Test - fun `only hidden accounts make manual sync fail without asking anyone`(@TempDir dir: Path) = runTest { + fun `only hidden accounts make manual sync ask for access, asking no one`(@TempDir dir: Path) = runTest { val sync = RecordingSyncRequester(hiddenTypes = setOf(dav.type, google.type)) sync.knownAccounts = setOf(dav, google) val repo = testRepository(fake(), dir, Dispatchers.Unconfined, syncRequester = sync) - assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.Failed) + assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.NeedsAccess) repo.createEvent(form(1L)) assertThat(sync.requests).isEmpty() } @Test - fun `unreadable account visibility asks no one`(@TempDir dir: Path) = runTest { + fun `unreadable account visibility counts as reachable`(@TempDir dir: Path) = runTest { val sync = RecordingSyncRequester(hiddenTypes = setOf("other")) sync.visibilityError = SecurityException("no") val repo = testRepository(fake(), dir, Dispatchers.Unconfined, syncRequester = sync) repo.createEvent(form(1L)) - assertThat(sync.requests).isEmpty() + assertThat(sync.requests).containsExactly(auto(dav)) } } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2f2f28e1..5a839865 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -23,8 +23,8 @@ the package list (recurring writes, save conflicts, reminder delivery). all (an account-less request would sync every account on the device); the 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 when - the drawer opens, so a revoke simply puts the account back behind the + 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 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