mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
Show an error instead of crashing when blocklist changes cannot be saved
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package cc.skysparkle.sparkledns
|
package cc.skysparkle.sparkledns
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
@@ -28,7 +30,9 @@ data class RemoteList(val url: String, val count: Int, val updated: Long)
|
|||||||
object Blocklist {
|
object Blocklist {
|
||||||
const val ALL_LISTS = "*"
|
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 lock = Any()
|
||||||
|
|
||||||
private val _enabled = MutableStateFlow(true)
|
private val _enabled = MutableStateFlow(true)
|
||||||
@@ -143,6 +147,8 @@ object Blocklist {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
commit(store, fresh)
|
commit(store, fresh)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
_error.value = e.message ?: e.javaClass.simpleName
|
||||||
} finally {
|
} finally {
|
||||||
_busy.value = null
|
_busy.value = null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package cc.skysparkle.sparkledns
|
package cc.skysparkle.sparkledns
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.animation.animateContentSize
|
import androidx.compose.animation.animateContentSize
|
||||||
import androidx.compose.foundation.horizontalScroll
|
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.input.KeyboardType
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.io.IOException
|
||||||
import java.text.DateFormat
|
import java.text.DateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
@@ -83,9 +87,11 @@ fun BlocklistScreen(modifier: Modifier = Modifier) {
|
|||||||
val input = domainInput
|
val input = domainInput
|
||||||
if (input.isBlank()) return
|
if (input.isBlank()) return
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val ok = withContext(Dispatchers.IO) { Blocklist.addCustom(context, input) }
|
val result = withContext(Dispatchers.IO) { tryEdit { Blocklist.addCustom(context, input) } }
|
||||||
|
result.onSuccess { ok ->
|
||||||
domainError = !ok
|
domainError = !ok
|
||||||
if (ok) domainInput = ""
|
if (ok) domainInput = ""
|
||||||
|
}.onFailure { showEditError(context, it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +195,7 @@ fun BlocklistScreen(modifier: Modifier = Modifier) {
|
|||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
) {
|
) {
|
||||||
Text(domain, modifier = Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis)
|
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))
|
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,
|
updating = busy == list.url || busy == Blocklist.ALL_LISTS,
|
||||||
actionsEnabled = busy == null,
|
actionsEnabled = busy == null,
|
||||||
onRefresh = { Blocklist.download(context, list.url) },
|
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)
|
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 <T> tryEdit(action: () -> T): Result<T> =
|
||||||
|
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"
|
private const val HINT_BLOCKLIST = "blocklist"
|
||||||
|
|||||||
@@ -90,7 +90,8 @@ fun MonitorScreen(modifier: Modifier = Modifier) {
|
|||||||
|
|
||||||
fun toggleBlock(domain: String, unblock: Boolean) {
|
fun toggleBlock(domain: String, unblock: Boolean) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val ok = withContext(Dispatchers.IO) {
|
val result = withContext(Dispatchers.IO) {
|
||||||
|
tryEdit {
|
||||||
if (unblock) {
|
if (unblock) {
|
||||||
Blocklist.removeCustom(context, domain)
|
Blocklist.removeCustom(context, domain)
|
||||||
true
|
true
|
||||||
@@ -98,6 +99,11 @@ fun MonitorScreen(modifier: Modifier = Modifier) {
|
|||||||
Blocklist.addCustom(context, domain)
|
Blocklist.addCustom(context, domain)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
val ok = result.getOrElse {
|
||||||
|
showEditError(context, it)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
val message = when {
|
val message = when {
|
||||||
!ok -> context.getString(R.string.bl_invalid)
|
!ok -> context.getString(R.string.bl_invalid)
|
||||||
unblock -> context.getString(R.string.mon_unblocked, domain)
|
unblock -> context.getString(R.string.mon_unblocked, domain)
|
||||||
|
|||||||
Reference in New Issue
Block a user