From bac4ccf26efa67c15c428ede461555c3eed17ea0 Mon Sep 17 00:00:00 2001 From: kalarina Date: Sun, 25 Oct 2026 12:18:17 +0200 Subject: [PATCH] Show an error instead of crashing when blocklist changes cannot be saved --- .../cc/skysparkle/sparkledns/Blocklist.kt | 8 ++++- .../skysparkle/sparkledns/BlocklistScreen.kt | 33 ++++++++++++++++--- .../cc/skysparkle/sparkledns/MonitorScreen.kt | 18 ++++++---- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/cc/skysparkle/sparkledns/Blocklist.kt b/app/src/main/java/cc/skysparkle/sparkledns/Blocklist.kt index 834217d..cfabe1e 100644 --- a/app/src/main/java/cc/skysparkle/sparkledns/Blocklist.kt +++ b/app/src/main/java/cc/skysparkle/sparkledns/Blocklist.kt @@ -1,6 +1,8 @@ package cc.skysparkle.sparkledns import android.content.Context +import android.util.Log +import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob @@ -28,7 +30,9 @@ data class RemoteList(val url: String, val count: Int, val updated: Long) object Blocklist { const val ALL_LISTS = "*" - private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + private val scope = CoroutineScope( + SupervisorJob() + Dispatchers.IO + CoroutineExceptionHandler { _, e -> Log.e("SparkleDNS", "Blocklist task failed", e) } + ) private val lock = Any() private val _enabled = MutableStateFlow(true) @@ -143,6 +147,8 @@ object Blocklist { } } commit(store, fresh) + } catch (e: IOException) { + _error.value = e.message ?: e.javaClass.simpleName } finally { _busy.value = null } diff --git a/app/src/main/java/cc/skysparkle/sparkledns/BlocklistScreen.kt b/app/src/main/java/cc/skysparkle/sparkledns/BlocklistScreen.kt index 37ea5e6..5a01141 100644 --- a/app/src/main/java/cc/skysparkle/sparkledns/BlocklistScreen.kt +++ b/app/src/main/java/cc/skysparkle/sparkledns/BlocklistScreen.kt @@ -1,6 +1,8 @@ package cc.skysparkle.sparkledns +import android.content.Context import android.net.Uri +import android.widget.Toast import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.animateContentSize import androidx.compose.foundation.horizontalScroll @@ -47,9 +49,11 @@ import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java.io.IOException import java.text.DateFormat import java.util.Date @@ -83,9 +87,11 @@ fun BlocklistScreen(modifier: Modifier = Modifier) { val input = domainInput if (input.isBlank()) return scope.launch { - val ok = withContext(Dispatchers.IO) { Blocklist.addCustom(context, input) } - domainError = !ok - if (ok) domainInput = "" + val result = withContext(Dispatchers.IO) { tryEdit { Blocklist.addCustom(context, input) } } + result.onSuccess { ok -> + domainError = !ok + if (ok) domainInput = "" + }.onFailure { showEditError(context, it) } } } @@ -189,7 +195,7 @@ fun BlocklistScreen(modifier: Modifier = Modifier) { verticalAlignment = Alignment.CenterVertically, ) { Text(domain, modifier = Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis) - IconButton(onClick = { scope.launch(Dispatchers.IO) { Blocklist.removeCustom(context, domain) } }) { + IconButton(onClick = { scope.editBlocklist(context) { Blocklist.removeCustom(context, domain) } }) { Icon(painterResource(R.drawable.ic_delete), stringResource(R.string.delete), Modifier.size(20.dp)) } } @@ -225,7 +231,7 @@ fun BlocklistScreen(modifier: Modifier = Modifier) { updating = busy == list.url || busy == Blocklist.ALL_LISTS, actionsEnabled = busy == null, onRefresh = { Blocklist.download(context, list.url) }, - onDelete = { scope.launch(Dispatchers.IO) { Blocklist.removeList(context, list.url) } }, + onDelete = { scope.editBlocklist(context) { Blocklist.removeList(context, list.url) } }, ) } if (lists.isNotEmpty()) InsetDivider(16) @@ -358,4 +364,21 @@ private fun ListRow( } } +// Blocklist edits write to storage, which can fail (a full disk, for one) +internal inline fun tryEdit(action: () -> T): Result = + try { + Result.success(action()) + } catch (e: IOException) { + Result.failure(e) + } + +internal fun CoroutineScope.editBlocklist(context: Context, action: () -> Unit) = launch { + withContext(Dispatchers.IO) { tryEdit(action) }.onFailure { showEditError(context, it) } +} + +internal fun showEditError(context: Context, error: Throwable) { + val reason = error.message ?: error.javaClass.simpleName + Toast.makeText(context, context.getString(R.string.test_fail, reason), Toast.LENGTH_LONG).show() +} + private const val HINT_BLOCKLIST = "blocklist" diff --git a/app/src/main/java/cc/skysparkle/sparkledns/MonitorScreen.kt b/app/src/main/java/cc/skysparkle/sparkledns/MonitorScreen.kt index 53148a8..fd8e7fc 100644 --- a/app/src/main/java/cc/skysparkle/sparkledns/MonitorScreen.kt +++ b/app/src/main/java/cc/skysparkle/sparkledns/MonitorScreen.kt @@ -90,14 +90,20 @@ fun MonitorScreen(modifier: Modifier = Modifier) { fun toggleBlock(domain: String, unblock: Boolean) { scope.launch { - val ok = withContext(Dispatchers.IO) { - if (unblock) { - Blocklist.removeCustom(context, domain) - true - } else { - Blocklist.addCustom(context, domain) + val result = withContext(Dispatchers.IO) { + tryEdit { + if (unblock) { + Blocklist.removeCustom(context, domain) + true + } else { + Blocklist.addCustom(context, domain) + } } } + val ok = result.getOrElse { + showEditError(context, it) + return@launch + } val message = when { !ok -> context.getString(R.string.bl_invalid) unblock -> context.getString(R.string.mon_unblocked, domain)