fix(#325): review round 3 - sheet failure state, unlinkable exceptions treated as the backup does, one-row colour lookup

This commit is contained in:
makiolaj committed 2026-11-13 22:28:12 +01:00
1 parent 026970501e
commit 3f4bf65b1a
4 files changed
+42 -21

No files matched your search

@@ -817,11 +817,19 @@ class AndroidCalendarDataSource @Inject constructor(
val exceptions = mutableListOf<CopyException>()
// Some adapters stamp the calendar's colour onto every row; that is no
// colour of the event's own and would pin it in the target.
val calendarColor = calendars().firstOrNull { it.id == calendarId }?.color
val calendarColor = resolver.query(
ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, calendarId),
arrayOf(CalendarContract.Calendars.CALENDAR_COLOR),
null,
null,
null,
)?.use { c -> if (c.moveToFirst() && !c.isNull(0)) c.getInt(0) else null }
forEachExportRow(selection, allDayReminderTimeMinutes) { event ->
val color = getIntOrNull(EventExportProjection.IDX_EVENT_COLOR)?.takeIf { it != calendarColor }
val originalId = getLongOrNull(EventExportProjection.IDX_ORIGINAL_ID)
val originalSyncId = getString(EventExportProjection.IDX_ORIGINAL_SYNC_ID)?.takeIf { it.isNotEmpty() }
val originalSyncId = getString(EventExportProjection.IDX_ORIGINAL_SYNC_ID)
// The same test the backup query makes: an empty ORIGINAL_SYNC_ID
// still marks an exception, one that can't be linked.
if (originalId == null && originalSyncId == null) {
series += CopySeries(
eventId = getLong(EventExportProjection.IDX_ID),
@@ -835,7 +843,7 @@ class AndroidCalendarDataSource @Inject constructor(
?: return@forEachExportRow
exceptions += CopyException(
originalId = originalId,
originalSyncId = originalSyncId,
originalSyncId = originalSyncId.takeIf { !it.isNullOrEmpty() },
originalInstance = originalInstance.toKotlinInstantFromEpochMillis(),
event = event(null),
color = color,
@@ -267,10 +267,10 @@ class CalendarRepositoryImpl @Inject constructor(
}
onProgress(0, events.size)
// Checked between events, so cancelling stops the copy; a plain import
// keeps running to the end of its file. A finished run stays finished.
// keeps running to the end of its file.
val context = currentCoroutineContext()
return importEvents(targetCalendarId, events) { done ->
if (done < events.size) context.ensureActive()
context.ensureActive()
onProgress(done, events.size)
}
}
@@ -25,6 +25,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import de.jeanlucmakiola.calendula.R
import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.FailureReason
import de.jeanlucmakiola.calendula.ui.common.CalendarFailure
import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip
import de.jeanlucmakiola.calendula.ui.common.CalendarPickerGroups
import de.jeanlucmakiola.calendula.ui.common.orUnnamed
@@ -40,7 +42,7 @@ import de.jeanlucmakiola.floret.components.Position
*/
@Composable
fun CopyEventsSheet(
calendars: List<CalendarSource>?,
calendars: CopyCalendars,
selection: CopySelection,
run: CopyRunState,
onSelectSource: (Long) -> Unit,
@@ -49,9 +51,10 @@ fun CopyEventsSheet(
onCancel: () -> Unit,
onDismiss: () -> Unit,
) {
val sources = calendars?.let(::copySources).orEmpty()
val list = (calendars as? CopyCalendars.Ready)?.calendars.orEmpty()
val sources = copySources(list)
val source = sources.firstOrNull { it.id == selection.sourceId }
val targets = calendars?.let { copyTargets(it, source?.id) }.orEmpty()
val targets = copyTargets(list, source?.id)
val target = targets.firstOrNull { it.id == selection.targetId }
val running = run is CopyRunState.Running
var picking by rememberSaveable { mutableStateOf<CopyEnd?>(null) }
@@ -62,11 +65,18 @@ fun CopyEventsSheet(
predictiveBack = true,
) {
GroupedHint(stringResource(R.string.calendars_copy_hint), style = MaterialTheme.typography.bodySmall)
if (calendars == null) {
Box(Modifier.fillMaxWidth().padding(32.dp), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
when (calendars) {
CopyCalendars.Loading -> {
Box(Modifier.fillMaxWidth().padding(32.dp), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
return@FullScreenPicker
}
return@FullScreenPicker
CopyCalendars.Failure -> {
CalendarFailure(reason = FailureReason.ProviderUnavailable, onRetry = onDismiss)
return@FullScreenPicker
}
is CopyCalendars.Ready -> Unit
}
if (sources.isEmpty() && !running) {
GroupedHint(stringResource(R.string.calendars_copy_none))
@@ -9,8 +9,6 @@ import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository
import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.ics.IcsImportSummary
import de.jeanlucmakiola.floret.android.stateWhileSubscribed
import de.jeanlucmakiola.floret.di.IoDispatcher
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -18,7 +16,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlin.coroutines.cancellation.CancellationException
@@ -34,15 +32,13 @@ import javax.inject.Inject
class CopyEventsViewModel @Inject constructor(
private val repository: CalendarRepository,
private val savedState: SavedStateHandle,
@IoDispatcher io: CoroutineDispatcher,
) : ViewModel() {
/** Null until the first read. */
val calendars: StateFlow<List<CalendarSource>?> =
val calendars: StateFlow<CopyCalendars> =
repository.calendars()
.catch { emit(emptyList()) }
.flowOn(io)
.stateWhileSubscribed(scope = viewModelScope, initialValue = null)
.map<List<CalendarSource>, CopyCalendars> { CopyCalendars.Ready(it) }
.catch { emit(CopyCalendars.Failure) }
.stateWhileSubscribed(scope = viewModelScope, initialValue = CopyCalendars.Loading)
val selection: StateFlow<CopySelection> = combine(
savedState.getStateFlow<Long?>(KEY_SOURCE, null),
@@ -120,6 +116,13 @@ data class CopySelection(val sourceId: Long? = null, val targetId: Long? = null)
CopySelection(sourceId = id, targetId = targetId?.takeIf { it != id })
}
/** The calendar list the copy sheet picks from. */
sealed interface CopyCalendars {
data object Loading : CopyCalendars
data object Failure : CopyCalendars
data class Ready(val calendars: List<CalendarSource>) : CopyCalendars
}
sealed interface CopyRunState {
data object Idle : CopyRunState