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 c3e88c49..98872219 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 @@ -184,7 +184,11 @@ class CalendarRepositoryImpl @Inject constructor( private fun targetsOf(calendars: List, dismissed: Set): 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 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 e22146f8..db44e688 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 @@ -81,22 +81,27 @@ fun List.syncAccounts(): Set = /** * 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, val needsAccess: Set) { +data class SyncTargets( + val reachable: Set, + val needsAccess: Set, + val dismissed: Set = 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, 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, 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 1e0d935c..22b75422 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/CalendarHost.kt @@ -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 }, ) } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt index 2a6f3f47..131fd970 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt @@ -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> = - 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()) 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 index bdd97be7..db705450 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncAccessPicker.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/SyncAccessPicker.kt @@ -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) -> 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) -> 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)) 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 47322557..f3ff3f0e 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 @@ -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() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7b603c08..5c40047e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -400,7 +400,7 @@ Pick each account Calendula may ask to sync 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. Don\'t ask - Not asked to sync + Not asked for sync access Calendula won\'t offer to request syncs for these accounts. Ask again to bring them back to Allow sync requests in the menu. Ask again Choose an account Calendula may ask to sync your calendars 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 77208479..1fb3be7a 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 @@ -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() } }