From 145ef6c6bfef153b2611a8dc7fd19391700ab501 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sat, 14 Nov 2026 01:09:00 +0100 Subject: [PATCH] refactor(#240): one access refresh in the repository, loading state in the access sheet --- .../data/calendar/CalendarRepository.kt | 8 +++---- .../data/calendar/CalendarRepositoryImpl.kt | 14 +++++++++--- .../ui/calendars/CalendarsViewModel.kt | 18 +++------------ .../calendula/ui/common/SyncAccessPicker.kt | 11 ++++++++++ .../calendula/ui/common/SyncViewModel.kt | 22 +++++-------------- .../calendula/ui/common/SyncViewModelTest.kt | 15 +++++++++++++ 6 files changed, 49 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepository.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepository.kt index e4a5ebec..28309da2 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepository.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarRepository.kt @@ -144,13 +144,13 @@ interface CalendarRepository { /** * The accounts behind visible calendars with a sync adapter, split by - * whether [requestSync] can reach them yet. Re-reads account access on - * every collection, so a grant or revoke shows on the next one. + * whether [requestSync] can reach them yet. Account access isn't observable: + * it is re-read on each collection and on every [refreshSyncAccess]. */ val syncTargets: Flow get() = flowOf(SyncTargets.None) - /** Accounts the user said not to be asked about granting access for. */ - val syncAccessDismissed: Flow> get() = flowOf(emptySet()) + /** Re-read account access for every [syncTargets] collector; it can change outside the app. */ + fun refreshSyncAccess() {} /** Stop or resume asking for [account]'s sync-request access. */ suspend fun setSyncAccessDismissed(account: SyncAccount, dismissed: Boolean) {} 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 846b1f96..947a4432 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 @@ -18,6 +18,8 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.drop @@ -195,10 +197,16 @@ class CalendarRepositoryImpl @Inject constructor( } } - override val syncTargets: Flow - get() = calendars().combine(prefs.syncAccessDismissed, ::targetsOf).flowOn(io) + private val accessChecks = MutableStateFlow(0) - override val syncAccessDismissed: Flow> = prefs.syncAccessDismissed + override val syncTargets: Flow + get() = combine(calendars(), prefs.syncAccessDismissed, accessChecks) { calendars, dismissed, _ -> + targetsOf(calendars, dismissed) + }.flowOn(io) + + override fun refreshSyncAccess() { + accessChecks.update { it + 1 } + } override suspend fun setSyncAccessDismissed(account: SyncAccount, dismissed: Boolean) { try { 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 2f4ab300..7fa95c38 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 @@ -9,10 +9,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import de.jeanlucmakiola.calendula.data.backup.BackupScheduler import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository -import kotlinx.coroutines.flow.update -import kotlinx.coroutines.flow.flowOf -import kotlinx.coroutines.flow.flatMapLatest -import kotlinx.coroutines.ExperimentalCoroutinesApi import de.jeanlucmakiola.calendula.data.calendar.SyncAccount import de.jeanlucmakiola.calendula.data.ics.IcsExporter import de.jeanlucmakiola.calendula.data.prefs.BackupStatus @@ -61,26 +57,18 @@ class CalendarsViewModel @Inject constructor( initialValue = emptyList(), ) - private val accessChecks = MutableStateFlow(0) - /** * Accounts the drawer was told not to ask sync-request access for (#240), * limited to those still behind a visible calendar and still unreachable. */ - @OptIn(ExperimentalCoroutinesApi::class) val syncAccessDismissed: StateFlow> = - combine(repository.syncAccessDismissed, accessChecks) { stored, _ -> stored } - .flatMapLatest { stored -> - if (stored.isEmpty()) flowOf(emptyList()) - else repository.syncTargets.map { it.dismissed.toList() } - } + repository.syncTargets + .map { it.dismissed.toList() } .catch { emit(emptyList()) } .stateWhileSubscribed(scope = viewModelScope, initialValue = emptyList()) /** Re-read account access, which can change outside the app; called on resume. */ - fun refreshAccess() { - accessChecks.update { it + 1 } - } + fun refreshAccess() = repository.refreshSyncAccess() fun askSyncAccessAgain(account: SyncAccount) { viewModelScope.launch { repository.setSyncAccessDismissed(account, dismissed = false) } 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 eb3f82b4..f2fd2639 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,7 +4,12 @@ import android.content.ActivityNotFoundException import android.util.Log import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding import androidx.compose.material3.DrawerState +import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi +import androidx.compose.material3.LoadingIndicator import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.material3.DrawerValue @@ -14,6 +19,8 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.Lifecycle @@ -67,6 +74,7 @@ fun RefreshSyncAccessOnOpen(drawerState: DrawerState, sync: SyncViewModel) { * opens the system picker for just that account, "Don't ask" stops offering it. * Closes itself once no account is left. */ +@OptIn(ExperimentalMaterial3ExpressiveApi::class) @Composable fun SyncAccessSheet( accounts: List?, @@ -77,6 +85,9 @@ fun SyncAccessSheet( LaunchedEffect(accounts?.isEmpty()) { if (accounts?.isEmpty() == true) onDismiss() } FullScreenPicker(title = stringResource(R.string.drawer_sync_access), onDismiss = onDismiss, predictiveBack = true) { GroupedHint(stringResource(R.string.sync_access_hint), verticalPadding = 8.dp) + if (accounts == null) { + Box(Modifier.fillMaxWidth().padding(24.dp), contentAlignment = Alignment.Center) { LoadingIndicator() } + } accounts.orEmpty().forEachIndexed { index, account -> SyncAccountRow( account = account, 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 00b2f3b1..8b16f10e 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 @@ -11,15 +11,12 @@ import de.jeanlucmakiola.calendula.data.calendar.SyncRequestResult import de.jeanlucmakiola.calendula.data.calendar.SyncTargets import de.jeanlucmakiola.floret.android.stateWhileSubscribed import kotlinx.coroutines.CoroutineStart -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.async import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.firstOrNull -import kotlinx.coroutines.flow.flatMapLatest -import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeoutOrNull import javax.inject.Inject @@ -35,22 +32,13 @@ class SyncViewModel @Inject constructor( private val repository: CalendarRepository, ) : ViewModel() { - private val accessChecks = MutableStateFlow(0) - - /** - * Who "Sync now" can reach, and who first needs the user's grant; null until - * first read. Account access isn't observable, so it is re-read on every - * [refreshAccess]. - */ - @OptIn(ExperimentalCoroutinesApi::class) - val targets: StateFlow = accessChecks - .flatMapLatest { repository.syncTargets.catch { emit(SyncTargets.None) } } + /** Who "Sync now" can reach, and who first needs the user's grant; null until first read. */ + val targets: StateFlow = repository.syncTargets + .catch { emit(SyncTargets.None) } .stateWhileSubscribed(scope = viewModelScope, initialValue = null) - /** Re-read account access: whenever the app resumes, which includes the picker returning. */ - fun refreshAccess() { - accessChecks.update { it + 1 } - } + /** Re-read account access: on resume (which includes the picker returning) and drawer open. */ + fun refreshAccess() = repository.refreshSyncAccess() private val _inFlight = MutableStateFlow(false) val inFlight: StateFlow = _inFlight.asStateFlow() diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModelTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModelTest.kt index d755a9fc..803c9586 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModelTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/common/SyncViewModelTest.kt @@ -114,4 +114,19 @@ class SyncViewModelTest { assertThat(vm.targets.value).isEqualTo(SyncTargets(setOf(davAccount), emptySet())) } + + @Test + fun `dismissing a hidden account takes it out of the grant list`(@TempDir dir: Path) = runTest(dispatcher) { + val sync = RecordingSyncRequester(hiddenTypes = setOf(davAccount.type)) + sync.knownAccounts = setOf(davAccount) + val (vm, _) = viewModel(dir, sync) + backgroundScope.launch { vm.targets.collect {} } + runCurrent() + assertThat(vm.targets.value?.needsAccess).containsExactly(davAccount) + + vm.dismissAccess(davAccount) + runCurrent() + + assertThat(vm.targets.value).isEqualTo(SyncTargets(emptySet(), emptySet(), dismissed = setOf(davAccount))) + } }