From 524c4cbb90a5586f8716399db5b3aaafaa08b1ce Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 16 Nov 2026 20:24:30 +0100 Subject: [PATCH] fix(#240): a failed read of the sync targets is retried instead of hiding Sync now --- .../data/calendar/CalendarRepositoryImpl.kt | 10 +++++++++- .../calendula/ui/common/SyncViewModel.kt | 13 +++++++++++-- .../data/calendar/FakeCalendarDataSource.kt | 4 ++++ .../calendula/ui/common/SyncViewModelTest.kt | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) 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 648a85b4..2179d79c 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 @@ -201,7 +201,15 @@ class CalendarRepositoryImpl @Inject constructor( override val syncTargets: Flow get() = combine(calendars(), prefs.syncAccessDismissed, accessChecks) { calendars, dismissed, _ -> - targetsOf(calendars, dismissed) + // One failed read leaves the rows off until the next change, not for good. + try { + targetsOf(calendars, dismissed) + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + Log.w(TAG, "Sync targets unreadable", e) + SyncTargets.None + } }.flowOn(io) override fun refreshSyncAccess() { 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 59994b46..bcb2f9e8 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 @@ -16,7 +16,8 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.retryWhen import kotlinx.coroutines.launch import javax.inject.Inject @@ -33,7 +34,13 @@ class SyncViewModel @Inject constructor( /** 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) } + // A failed read hides the rows for now, then reads again rather than for good. + .retryWhen { e, attempt -> + if (e is CancellationException) return@retryWhen false + emit(SyncTargets.None) + delay((RETRY_BASE_MILLIS shl attempt.coerceAtMost(5).toInt()).coerceAtMost(RETRY_MAX_MILLIS)) + true + } .stateWhileSubscribed(scope = viewModelScope, initialValue = null) /** Re-read account access: on resume (which includes the picker returning) and drawer open. */ @@ -81,6 +88,8 @@ class SyncViewModel @Inject constructor( } private companion object { + const val RETRY_BASE_MILLIS = 1_000L + const val RETRY_MAX_MILLIS = 30_000L const val TAG = "SyncViewModel" } } diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/FakeCalendarDataSource.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/FakeCalendarDataSource.kt index 493b2708..c926950e 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/FakeCalendarDataSource.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/FakeCalendarDataSource.kt @@ -79,8 +79,12 @@ internal class FakeCalendarDataSource : CalendarDataSource { var calendarQueries = 0 private set + /** Set to make [calendars] throw until cleared. */ + var calendarsError: Exception? = null + override fun calendars(): List { calendarQueries++ + calendarsError?.let { throw it } return calendarsResult } override fun instances(beginMillis: Long, endMillis: Long): List = 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 d36aa258..1d525b00 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 @@ -190,4 +190,20 @@ class SyncViewModelTest { assertThat(vm.targets.value).isEqualTo(SyncTargets(emptySet(), emptySet(), dismissed = setOf(davAccount))) } + + @Test + fun `a failed read of the targets is retried rather than hiding Sync now for good`(@TempDir dir: Path) = + runTest(dispatcher) { + val (vm, source) = viewModel(dir) + source.calendarsError = IllegalStateException("provider gone") + backgroundScope.launch { vm.targets.collect {} } + runCurrent() + assertThat(vm.targets.value?.reachable.orEmpty()).isEmpty() + + source.calendarsError = null + advanceTimeBy(31_000) + runCurrent() + + assertThat(vm.targets.value?.reachable).containsExactly(davAccount) + } }