fix(#240): access re-read on resume, needs-access receipt, drawer stays open for the next grant

This commit is contained in:
makiolaj committed 2026-11-13 22:04:44 +01:00
1 parent 88de8f42b0
commit 67052352ac
7 files changed
+25 -25

No files matched your search

@@ -149,11 +149,12 @@ class CalendarRepositoryImpl @Inject constructor(
*/
private fun reach(accounts: Set<SyncAccount>): 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
}
}
@@ -104,4 +104,4 @@ fun accountAccessIntent(accounts: Collection<SyncAccount>, 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 }
@@ -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<SyncAccount>) -> 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) },
)
},
)
@@ -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
}
+1
View File
@@ -401,6 +401,7 @@
<string name="sync_access_picker_description">Choose an account Calendula may ask to sync your calendars</string>
<string name="sync_requested">Sync requested</string>
<string name="sync_request_failed">Couldn\'t request a sync</string>
<string name="sync_request_needs_access">Allow sync requests in the menu first</string>
<string name="sync_request_partly_failed">Sync requested, but not for every account</string>
<string name="sync_nothing_to_sync">No synced calendars to sync</string>
@@ -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))
}
}
+2 -2
View File
@@ -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