refactor(#240): one access refresh in the repository, loading state in the access sheet

This commit is contained in:
makiolaj committed 2026-11-14 01:09:00 +01:00
1 parent 6161efab40
commit 145ef6c6bf
6 files changed
+49 -39

No files matched your search

@@ -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<SyncTargets> get() = flowOf(SyncTargets.None)
/** Accounts the user said not to be asked about granting access for. */
val syncAccessDismissed: Flow<Set<SyncAccount>> 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) {}
@@ -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<SyncTargets>
get() = calendars().combine(prefs.syncAccessDismissed, ::targetsOf).flowOn(io)
private val accessChecks = MutableStateFlow(0)
override val syncAccessDismissed: Flow<Set<SyncAccount>> = prefs.syncAccessDismissed
override val syncTargets: Flow<SyncTargets>
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 {
@@ -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<List<SyncAccount>> =
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) }
@@ -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<SyncAccount>?,
@@ -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,
@@ -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<SyncTargets?> = 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<SyncTargets?> = 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<Boolean> = _inFlight.asStateFlow()
@@ -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)))
}
}