fix(#240): list only dismissals that still apply, keep the access sheet across recreation

This commit is contained in:
makiolaj committed 2026-11-14 00:56:47 +01:00
1 parent a026f24bed
commit 34e8e8571c
8 files changed
+44 -30

No files matched your search

@@ -184,7 +184,11 @@ class CalendarRepositoryImpl @Inject constructor(
private fun targetsOf(calendars: List<CalendarSource>, dismissed: Set<SyncAccount>): SyncTargets =
reach(calendars.syncAccounts().withAdapter()).let {
if (dismissed.isEmpty()) it else it.copy(needsAccess = it.needsAccess - dismissed)
if (dismissed.isEmpty()) {
it
} else {
it.copy(needsAccess = it.needsAccess - dismissed, dismissed = it.needsAccess intersect dismissed)
}
}
override val syncTargets: Flow<SyncTargets>
@@ -81,22 +81,27 @@ fun List<CalendarSource>.syncAccounts(): Set<SyncAccount> =
/**
* Who a sync request can go to: [reachable] by name, [needsAccess] only once the
* user lets the app see the account (Android 13+, see [accountAccessIntent]).
* [dismissed] are hidden accounts the user said not to ask about.
*/
data class SyncTargets(val reachable: Set<SyncAccount>, val needsAccess: Set<SyncAccount>) {
data class SyncTargets(
val reachable: Set<SyncAccount>,
val needsAccess: Set<SyncAccount>,
val dismissed: Set<SyncAccount> = emptySet(),
) {
companion object {
val None = SyncTargets(emptySet(), emptySet())
}
}
/**
* The system account picker, limited to [accounts]: the one the user picks
* becomes visible to the app, so sync requests can name it. One account per pick.
* The system account picker, limited to [account]: once picked it becomes
* visible to the app, so sync requests can name it.
*/
fun accountAccessIntent(accounts: Collection<SyncAccount>, description: String): Intent =
fun accountAccessIntent(account: SyncAccount, description: String): Intent =
AccountManager.newChooseAccountIntent(
null,
accounts.map { Account(it.name, it.type) },
accounts.map { it.type }.distinct().toTypedArray(),
listOf(Account(account.name, account.type)),
arrayOf(account.type),
description,
null,
null,
@@ -456,7 +456,7 @@ fun CalendarHost(
val selectionFailureMessage = eventActionFailureMessage(selectionFailure)
val syncMessage = syncResult?.let { stringResource(it.messageRes()) }
val launchAccessPicker = rememberSyncAccessPicker(sync)
var showSyncAccess by remember { mutableStateOf(false) }
var showSyncAccess by rememberSaveable { mutableStateOf(false) }
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
@@ -605,7 +605,7 @@ fun CalendarHost(
onOpenSettings()
switchScope.launch { drawerState.close() }
},
sync = remember(syncTargets, syncInFlight, launchAccessPicker) {
sync = remember(syncTargets, syncInFlight) {
DrawerSync(
canSync = syncTargets.reachable.isNotEmpty(),
inFlight = syncInFlight,
@@ -719,8 +719,8 @@ fun CalendarHost(
if (showSyncAccess) {
SyncAccessSheet(
accounts = syncTargets.needsAccess.toList(),
onGrant = { launchAccessPicker(setOf(it)) },
onDismissAccount = { sync.setAccessDismissed(it, dismissed = true) },
onGrant = launchAccessPicker,
onDismissAccount = sync::dismissAccess,
onDismiss = { showSyncAccess = false },
)
}
@@ -57,10 +57,13 @@ class CalendarsViewModel @Inject constructor(
initialValue = emptyList(),
)
/** Accounts the drawer was told not to ask sync-request access for (#240). */
/**
* Accounts the drawer was told not to ask sync-request access for (#240),
* limited to those still behind a visible calendar and still unreachable.
*/
val syncAccessDismissed: StateFlow<List<SyncAccount>> =
repository.syncAccessDismissed
.map { set -> set.sortedWith(compareBy({ it.name.lowercase() }, { it.type })) }
repository.syncTargets
.map { targets -> targets.dismissed.sortedWith(compareBy({ it.name.lowercase() }, { it.type })) }
.catch { emit(emptyList()) }
.stateWhileSubscribed(scope = viewModelScope, initialValue = emptyList())
@@ -4,10 +4,7 @@ import android.content.ActivityNotFoundException
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ManageAccounts
import androidx.compose.material3.DrawerState
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.DrawerValue
@@ -16,8 +13,8 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
@@ -31,12 +28,12 @@ import de.jeanlucmakiola.floret.components.GroupedRow
import de.jeanlucmakiola.floret.components.positionOf
/**
* 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
* Launches the system account picker for an account 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<SyncAccount>) -> Unit {
fun rememberSyncAccessPicker(sync: SyncViewModel): (SyncAccount) -> Unit {
var open by remember { mutableStateOf(false) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
open = false
@@ -44,10 +41,10 @@ fun rememberSyncAccessPicker(sync: SyncViewModel): (Set<SyncAccount>) -> Unit {
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { sync.refreshAccess() }
val description = stringResource(R.string.sync_access_picker_description)
return remember(launcher, description) {
{ accounts ->
{ account ->
if (!open) {
try {
launcher.launch(accountAccessIntent(accounts, description))
launcher.launch(accountAccessIntent(account, description))
open = true
} catch (e: ActivityNotFoundException) {
Log.w("SyncAccessPicker", "No account picker", e)
@@ -77,7 +74,11 @@ fun SyncAccessSheet(
onDismissAccount: (SyncAccount) -> Unit,
onDismiss: () -> Unit,
) {
LaunchedEffect(accounts.isEmpty()) { if (accounts.isEmpty()) onDismiss() }
// Closes once the last account is granted or dismissed, not while the first read is pending.
var hadAccounts by rememberSaveable { mutableStateOf(false) }
LaunchedEffect(accounts.isEmpty()) {
if (accounts.isNotEmpty()) hadAccounts = true else if (hadAccounts) onDismiss()
}
FullScreenPicker(title = stringResource(R.string.drawer_sync_access), onDismiss = onDismiss, predictiveBack = true) {
GroupedHint(stringResource(R.string.sync_access_hint), verticalPadding = 8.dp)
accounts.forEachIndexed { index, account ->
@@ -86,7 +87,7 @@ fun SyncAccessSheet(
summary = sourceAppName(account.type),
position = positionOf(index, accounts.size),
minHeight = 64.dp,
leading = { Icon(Icons.Filled.ManageAccounts, contentDescription = null) },
leading = { SourceLogo(account.type) },
trailing = {
TextButton(onClick = { onDismissAccount(account) }) {
Text(stringResource(R.string.sync_access_dont_ask))
@@ -81,8 +81,8 @@ class SyncViewModel @Inject constructor(
}
/** Stop asking for [account]'s access (#240); undone from the calendar manager. */
fun setAccessDismissed(account: SyncAccount, dismissed: Boolean) {
viewModelScope.launch { repository.setSyncAccessDismissed(account, dismissed) }
fun dismissAccess(account: SyncAccount) {
viewModelScope.launch { repository.setSyncAccessDismissed(account, dismissed = true) }
}
fun consumeResult() {
+1 -1
View File
@@ -400,7 +400,7 @@
<string name="drawer_sync_access_summary">Pick each account Calendula may ask to sync</string>
<string name="sync_access_hint">Android only lets Calendula ask an account to sync once you pick it here. Tap an account to allow it, or choose Don\'t ask to stop offering it.</string>
<string name="sync_access_dont_ask">Don\'t ask</string>
<string name="calendars_sync_access_header">Not asked to sync</string>
<string name="calendars_sync_access_header">Not asked for sync access</string>
<string name="calendars_sync_access_hint">Calendula won\'t offer to request syncs for these accounts. Ask again to bring them back to Allow sync requests in the menu.</string>
<string name="calendars_sync_access_ask_again">Ask again</string>
<string name="sync_access_picker_description">Choose an account Calendula may ask to sync your calendars</string>
@@ -280,7 +280,7 @@ class SyncNudgeTest {
repo.setSyncAccessDismissed(google, dismissed = true)
assertThat(repo.syncTargets.first()).isEqualTo(SyncTargets(setOf(dav), emptySet()))
assertThat(repo.syncTargets.first()).isEqualTo(SyncTargets(setOf(dav), emptySet(), dismissed = setOf(google)))
assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.Requested)
repo.setSyncAccessDismissed(google, dismissed = false)
@@ -296,7 +296,7 @@ class SyncNudgeTest {
repo.setSyncAccessDismissed(dav, dismissed = true)
repo.setSyncAccessDismissed(google, dismissed = true)
assertThat(repo.syncTargets.first()).isEqualTo(SyncTargets.None)
assertThat(repo.syncTargets.first()).isEqualTo(SyncTargets(emptySet(), emptySet(), dismissed = setOf(dav, google)))
assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.NothingToSync)
}
@@ -309,5 +309,6 @@ class SyncNudgeTest {
repo.createEvent(form(1L))
assertThat(sync.requests).containsExactly(auto(dav))
assertThat(repo.syncTargets.first().dismissed).isEmpty()
}
}