Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

Fix settings sync overlay cancellation semantics

David Nashash45fb5ea76a46
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsBackupActions.kt b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsBackupActions.kt
index da14d129..01c16345 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsBackupActions.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsBackupActions.kt
@@ -22,7 +22,14 @@ internal class SettingsBackupActions(
) {
fun exportConfig(scope: CoroutineScope, uriString: String, onSuccess: (() -> Unit)? = null) {
scope.launch {
- uiState.update { it.copy(isSyncing = true) }
+ uiState.update {
+ it.copy(
+ isSyncing = true,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false
+ )
+ }
val result = exportBackup(ExportBackupCommand(uriString))
if (result is ExportBackupResult.Success) {
onSuccess?.invoke()
@@ -30,6 +37,9 @@ internal class SettingsBackupActions(
uiState.update { state ->
state.copy(
isSyncing = false,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = if (result is ExportBackupResult.Error) {
"Export failed: ${result.message}"
} else {
@@ -42,16 +52,29 @@ internal class SettingsBackupActions(
fun inspectBackup(scope: CoroutineScope, uriString: String) {
scope.launch {
- uiState.update { it.copy(isSyncing = true) }
+ uiState.update {
+ it.copy(
+ isSyncing = true,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false
+ )
+ }
val result = importBackup.inspect(InspectBackupCommand(uriString))
uiState.update { state ->
when (result) {
is InspectBackupResult.Error -> state.copy(
isSyncing = false,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = "Import failed: ${result.message}"
)
is InspectBackupResult.Success -> state.copy(
isSyncing = false,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
pendingBackupUri = result.uriString,
backupPreview = result.preview,
backupImportPlan = result.defaultPlan
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsProviderActions.kt b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsProviderActions.kt
index 311dbd3b..f9ab2076 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsProviderActions.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsProviderActions.kt
@@ -16,7 +16,9 @@ import com.streamvault.domain.usecase.SyncProviderCommand
import com.streamvault.domain.usecase.SyncProviderResult
import com.streamvault.data.sync.SyncManager
import com.streamvault.data.preferences.PreferencesRepository
+import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
@@ -245,15 +247,21 @@ internal class SettingsProviderActions(
scope: CoroutineScope,
providerId: Long,
syncMode: SettingsProviderSyncMode = SettingsProviderSyncMode.SYNC_NOW,
- progressPrefix: String? = null
- ) {
- scope.launch {
+ progressPrefix: String? = null,
+ startedAt: Long = 0L,
+ sectionLabel: String? = null,
+ isCancelable: Boolean = false
+ ): Job {
+ return scope.launch {
val providerName = providerRepository.getProvider(providerId)?.name
uiState.update {
it.copy(
isSyncing = true,
syncingProviderName = providerName,
- syncProgress = progressPrefix ?: "Preparing sync..."
+ syncProgress = progressPrefix ?: "Preparing sync...",
+ syncStartedAt = startedAt,
+ syncSectionLabel = sectionLabel,
+ syncCanCancel = isCancelable
)
}
try {
@@ -261,12 +269,27 @@ internal class SettingsProviderActions(
SettingsProviderSyncMode.SYNC_NOW -> runSyncNow(providerId, providerName)
SettingsProviderSyncMode.REBUILD_INDEX -> runRebuildIndex(providerId, providerName)
}
+ } catch (e: CancellationException) {
+ uiState.update {
+ it.copy(
+ isSyncing = false,
+ syncProgress = null,
+ syncingProviderName = null,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false
+ )
+ }
+ throw e
} catch (e: Exception) {
uiState.update {
it.copy(
isSyncing = false,
syncProgress = null,
syncingProviderName = null,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = "Sync failed: ${e.message}"
)
}
@@ -350,6 +373,9 @@ internal class SettingsProviderActions(
isSyncing = false,
syncProgress = null,
syncingProviderName = null,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = when {
result is SyncProviderResult.Error -> "Sync failed: ${result.message}"
(result as? SyncProviderResult.Success)?.isPartial == true -> "Sync completed with warnings: $warningsMessage"
@@ -410,6 +436,9 @@ internal class SettingsProviderActions(
isSyncing = false,
syncProgress = null,
syncingProviderName = null,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = when {
result is Result.Error -> "Rebuild index failed: ${result.message}"
else -> "Index rebuild queued"
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsScreenDialogs.kt b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsScreenDialogs.kt
index 2960bf09..e59241ad 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsScreenDialogs.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsScreenDialogs.kt
@@ -59,7 +59,7 @@ internal fun SettingsScreenDialogs(
progress = uiState.syncProgress,
sectionLabel = uiState.syncSectionLabel,
startedAt = uiState.syncStartedAt,
- onCancel = onCancelSync
+ onCancel = onCancelSync.takeIf { uiState.syncCanCancel }
)
if (dialogState.showLiveTvModeDialog) {
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncActions.kt b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncActions.kt
index 0225de4e..b1fb54c8 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncActions.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncActions.kt
@@ -9,7 +9,6 @@ import com.streamvault.domain.model.ProviderType
import com.streamvault.domain.model.Result
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
-import kotlinx.coroutines.cancel
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
@@ -20,11 +19,11 @@ internal class SettingsSyncActions(
private val syncManager: SyncManager,
private val tvInputChannelSyncManager: TvInputChannelSyncManager,
private val uiState: MutableStateFlow<SettingsUiState>,
- private val refreshProvider: (CoroutineScope, Long, SettingsProviderSyncMode) -> Unit
+ private val refreshProvider: (CoroutineScope, Long, SettingsProviderSyncMode, String?, Long, String?, Boolean) -> Job
) {
private var syncJob: Job? = null
- fun cancelSync(scope: CoroutineScope) {
+ fun cancelSync() {
syncJob?.cancel()
syncJob = null
uiState.update {
@@ -33,19 +32,41 @@ internal class SettingsSyncActions(
syncProgress = null,
syncingProviderName = null,
syncStartedAt = 0L,
- syncSectionLabel = null
+ syncSectionLabel = null,
+ syncCanCancel = false,
+ userMessage = appContext.getString(R.string.settings_sync_cancelled)
)
}
}
fun syncProviderSection(scope: CoroutineScope, providerId: Long, selection: ProviderSyncSelection) {
+ if (selection == ProviderSyncSelection.REBUILD_INDEX) {
+ val sectionLabel = selection.label(appContext)
+ val job = refreshProvider(
+ scope,
+ providerId,
+ SettingsProviderSyncMode.REBUILD_INDEX,
+ appContext.getString(R.string.settings_syncing_section, sectionLabel),
+ System.currentTimeMillis(),
+ sectionLabel,
+ true
+ )
+ syncJob = job
+ job.invokeOnCompletion {
+ if (syncJob === job) {
+ syncJob = null
+ }
+ }
+ return
+ }
+
syncJob = scope.launch {
when (selection) {
ProviderSyncSelection.SYNC_NOW -> runSectionSync(
providerId = providerId,
selections = syncNowSelections(providerId)
)
- ProviderSyncSelection.REBUILD_INDEX -> refreshProvider(scope, providerId, SettingsProviderSyncMode.REBUILD_INDEX)
+ ProviderSyncSelection.REBUILD_INDEX -> Unit
else -> runSectionSync(providerId, listOf(selection))
}
}
@@ -83,7 +104,8 @@ internal class SettingsSyncActions(
syncProgress = appContext.getString(R.string.settings_syncing_preparing),
syncingProviderName = providerName,
syncStartedAt = System.currentTimeMillis(),
- syncSectionLabel = sectionLabel
+ syncSectionLabel = sectionLabel,
+ syncCanCancel = true
)
}
val section = when (action) {
@@ -108,6 +130,7 @@ internal class SettingsSyncActions(
syncingProviderName = null,
syncStartedAt = 0L,
syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = "Retry failed: ${result.message}"
)
} else {
@@ -125,6 +148,7 @@ internal class SettingsSyncActions(
syncingProviderName = null,
syncStartedAt = 0L,
syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = if (updatedWarnings.isEmpty()) {
"Section retry succeeded. All current warnings cleared."
} else {
@@ -138,6 +162,12 @@ internal class SettingsSyncActions(
)
}
}
+ }.also { job ->
+ job.invokeOnCompletion {
+ if (syncJob === job) {
+ syncJob = null
+ }
+ }
}
}
@@ -153,7 +183,8 @@ internal class SettingsSyncActions(
syncProgress = appContext.getString(R.string.settings_syncing_preparing),
syncingProviderName = providerName,
syncStartedAt = System.currentTimeMillis(),
- syncSectionLabel = null
+ syncSectionLabel = null,
+ syncCanCancel = true
)
}
try {
@@ -211,6 +242,7 @@ internal class SettingsSyncActions(
syncingProviderName = null,
syncStartedAt = 0L,
syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = when {
failures.isEmpty() -> appContext.getString(
R.string.settings_sync_sections_success,
@@ -236,6 +268,7 @@ internal class SettingsSyncActions(
syncingProviderName = null,
syncStartedAt = 0L,
syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = appContext.getString(R.string.settings_sync_cancelled)
)
}
@@ -247,6 +280,7 @@ internal class SettingsSyncActions(
syncingProviderName = null,
syncStartedAt = 0L,
syncSectionLabel = null,
+ syncCanCancel = false,
userMessage = "Sync failed: ${e.message}"
)
}
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncOverlay.kt b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncOverlay.kt
index 069e4566..8aba10c9 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncOverlay.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsSyncOverlay.kt
@@ -57,11 +57,16 @@ internal fun SyncingOverlay(
)
) {
val cancelFocusRequester = remember { FocusRequester() }
- LaunchedEffect(Unit) { cancelFocusRequester.requestFocus() }
+ LaunchedEffect(onCancel) {
+ if (onCancel != null) {
+ cancelFocusRequester.requestFocus()
+ }
+ }
// Elapsed time counter
val elapsedSnapshot = remember { mutableStateOf(0L) }
LaunchedEffect(startedAt) {
+ elapsedSnapshot.value = 0L
if (startedAt > 0L) {
while (true) {
kotlinx.coroutines.delay(1000L)
@@ -134,15 +139,17 @@ internal fun SyncingOverlay(
}
// Elapsed time
- Row(
- horizontalArrangement = Arrangement.spacedBy(24.dp),
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text(
- text = stringResource(R.string.settings_sync_elapsed, elapsedText),
- style = MaterialTheme.typography.labelSmall,
- color = OnSurfaceDim
- )
+ if (startedAt > 0L) {
+ Row(
+ horizontalArrangement = Arrangement.spacedBy(24.dp),
+ verticalAlignment = Alignment.CenterVertically
+ ) {
+ Text(
+ text = stringResource(R.string.settings_sync_elapsed, elapsedText),
+ style = MaterialTheme.typography.labelSmall,
+ color = OnSurfaceDim
+ )
+ }
}
// Cancel button — the only interactive element
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsUiStateModel.kt b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsUiStateModel.kt
index 856aa246..bf8a86be 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsUiStateModel.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsUiStateModel.kt
@@ -57,6 +57,7 @@ data class SettingsUiState(
val syncingProviderName: String? = null,
val syncStartedAt: Long = 0L,
val syncSectionLabel: String? = null,
+ val syncCanCancel: Boolean = false,
val userMessage: String? = null,
val syncWarningsByProvider: Map<Long, List<String>> = emptyMap(),
val xtreamLiveOnboardingPhaseByProvider: Map<Long, String> = emptyMap(),
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsViewModel.kt b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsViewModel.kt
index 3bb9aedb..d8663ece 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsViewModel.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/settings/SettingsViewModel.kt
@@ -178,7 +178,17 @@ class SettingsViewModel @Inject constructor(
syncManager = syncManager,
tvInputChannelSyncManager = tvInputChannelSyncManager,
uiState = _uiState,
- refreshProvider = { scope, providerId, syncMode -> providerActions.refreshProvider(scope, providerId, syncMode) }
+ refreshProvider = { scope, providerId, syncMode, progressPrefix, startedAt, sectionLabel, isCancelable ->
+ providerActions.refreshProvider(
+ scope = scope,
+ providerId = providerId,
+ syncMode = syncMode,
+ progressPrefix = progressPrefix,
+ startedAt = startedAt,
+ sectionLabel = sectionLabel,
+ isCancelable = isCancelable
+ )
+ }
)
private val epgActions = SettingsEpgActions(
epgSourceRepository = epgSourceRepository,
@@ -988,14 +998,37 @@ class SettingsViewModel @Inject constructor(
fun clearHistory() {
viewModelScope.launch {
- _uiState.update { it.copy(isSyncing = true) }
+ _uiState.update {
+ it.copy(
+ isSyncing = true,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false
+ )
+ }
when (val result = playbackHistoryRepository.clearAllHistory()) {
is Result.Success -> {
preferencesRepository.clearAllRecentData()
- _uiState.update { it.copy(isSyncing = false, userMessage = appContext.getString(R.string.settings_history_cleared)) }
+ _uiState.update {
+ it.copy(
+ isSyncing = false,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
+ userMessage = appContext.getString(R.string.settings_history_cleared)
+ )
+ }
}
is Result.Error -> {
- _uiState.update { it.copy(isSyncing = false, userMessage = "Failed to clear history: ${result.message}") }
+ _uiState.update {
+ it.copy(
+ isSyncing = false,
+ syncStartedAt = 0L,
+ syncSectionLabel = null,
+ syncCanCancel = false,
+ userMessage = "Failed to clear history: ${result.message}"
+ )
+ }
}
Result.Loading -> Unit
}
@@ -1053,7 +1086,7 @@ class SettingsViewModel @Inject constructor(
}
fun cancelSync() {
- syncActions.cancelSync(viewModelScope)
+ syncActions.cancelSync()
}
fun deleteProvider(providerId: Long, onSuccess: () -> Unit = {}) {