mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-16 01:00:17 +01:00
net: cancel stale gate checks and serialize form actions
Generation-scope the on-link check so a network, source or offline change invalidates the result and drops an unanswered rationale. Mark the form busy while the gate resolves so a second tap cannot run a duplicate save, and require an eligible interface before treating .local as local. Fold the repeated denial and network-change handlers into helpers.
This commit is contained in:
@@ -26,9 +26,9 @@ class LocalNetworkAccess(
|
||||
if (Build.VERSION.SDK_INT < LOCAL_NETWORK_PERMISSION_SDK) return false
|
||||
if (granted()) return false
|
||||
val host = serverHost(address) ?: return false
|
||||
if (isLocalHostname(host)) return true
|
||||
val linkAddresses = currentLinkAddresses()
|
||||
if (linkAddresses.isEmpty()) return false
|
||||
if (isLocalHostname(host)) return true
|
||||
return withContext(Dispatchers.IO) {
|
||||
val resolved = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList())
|
||||
resolved.any { candidate -> linkAddresses.any { it.contains(candidate) } }
|
||||
|
||||
@@ -211,7 +211,10 @@ fun SubtracksRoot(root: RootViewModel = koinViewModel()) {
|
||||
val offline by root.offline.collectAsStateWithLifecycle()
|
||||
val networkRevision = rememberNetworkRevision()
|
||||
LaunchedEffect(serverAddress, networkRevision, offline) {
|
||||
if (offline) return@LaunchedEffect
|
||||
if (offline) {
|
||||
localNetworkGate.cancel()
|
||||
return@LaunchedEffect
|
||||
}
|
||||
localNetworkGate.run(
|
||||
address = serverAddress,
|
||||
onGranted = {
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.subtracks.R
|
||||
import com.subtracks.data.net.LOCAL_NETWORK_PERMISSION
|
||||
import com.subtracks.data.net.LocalNetworkAccess
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.compose.koinInject
|
||||
|
||||
@@ -32,7 +33,11 @@ class LocalNetworkGate internal constructor(
|
||||
private val scope: CoroutineScope,
|
||||
private val checker: LocalNetworkAccess,
|
||||
private val requestPermission: (address: String, onGranted: () -> Unit, onDenied: () -> Unit) -> Unit,
|
||||
private val clearPrompt: () -> Unit,
|
||||
) {
|
||||
private var generation = 0
|
||||
private var check: Job? = null
|
||||
|
||||
fun run(
|
||||
address: String?,
|
||||
onGranted: () -> Unit,
|
||||
@@ -40,18 +45,30 @@ class LocalNetworkGate internal constructor(
|
||||
onNotNeeded: () -> Unit = onGranted,
|
||||
automatic: Boolean = false,
|
||||
) {
|
||||
cancel()
|
||||
if (address == null) {
|
||||
onNotNeeded()
|
||||
return
|
||||
}
|
||||
val current = generation
|
||||
check =
|
||||
scope.launch {
|
||||
if (!checker.requiresPermission(address) || (automatic && checker.wasDenied(address))) {
|
||||
onNotNeeded()
|
||||
} else {
|
||||
val required = runCatching { checker.requiresPermission(address) }.getOrDefault(false)
|
||||
if (current != generation) return@launch
|
||||
if (required && !(automatic && checker.wasDenied(address))) {
|
||||
requestPermission(address, onGranted, onDenied)
|
||||
} else {
|
||||
onNotNeeded()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
generation++
|
||||
check?.cancel()
|
||||
check = null
|
||||
clearPrompt()
|
||||
}
|
||||
}
|
||||
|
||||
private class PendingRequest(
|
||||
@@ -66,34 +83,39 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
||||
val scope = rememberCoroutineScope()
|
||||
var pending by remember { mutableStateOf<PendingRequest?>(null) }
|
||||
var awaitingSystemPrompt by remember { mutableStateOf(false) }
|
||||
|
||||
fun deny(request: PendingRequest) {
|
||||
pending = null
|
||||
checker.markDenied(request.address)
|
||||
request.onDenied()
|
||||
}
|
||||
|
||||
val launcher =
|
||||
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||
val request = pending
|
||||
pending = null
|
||||
awaitingSystemPrompt = false
|
||||
if (request != null) {
|
||||
if (granted) {
|
||||
request.onGranted()
|
||||
} else {
|
||||
checker.markDenied(request.address)
|
||||
request.onDenied()
|
||||
}
|
||||
if (granted) request.onGranted() else deny(request)
|
||||
}
|
||||
}
|
||||
|
||||
val gate =
|
||||
LocalNetworkGate(scope, checker) { address, onGranted, onDenied ->
|
||||
LocalNetworkGate(
|
||||
scope = scope,
|
||||
checker = checker,
|
||||
requestPermission = { address, onGranted, onDenied ->
|
||||
if (pending == null) pending = PendingRequest(address, onGranted, onDenied)
|
||||
}
|
||||
},
|
||||
clearPrompt = {
|
||||
if (!awaitingSystemPrompt) pending = null
|
||||
},
|
||||
)
|
||||
|
||||
val request = pending
|
||||
if (request != null && !awaitingSystemPrompt) {
|
||||
AlertDialog(
|
||||
onDismissRequest = {
|
||||
pending = null
|
||||
checker.markDenied(request.address)
|
||||
request.onDenied()
|
||||
},
|
||||
onDismissRequest = { deny(request) },
|
||||
title = { Text(stringResource(R.string.local_network_permission_title)) },
|
||||
text = { Text(stringResource(R.string.local_network_permission_body)) },
|
||||
confirmButton = {
|
||||
@@ -108,11 +130,7 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
pending = null
|
||||
checker.markDenied(request.address)
|
||||
request.onDenied()
|
||||
},
|
||||
onClick = { deny(request) },
|
||||
) {
|
||||
Text(stringResource(R.string.actions_cancel))
|
||||
}
|
||||
@@ -129,28 +147,29 @@ internal fun rememberNetworkRevision(): Int {
|
||||
DisposableEffect(context) {
|
||||
val manager = context.getSystemService(ConnectivityManager::class.java)
|
||||
val mainExecutor = ContextCompat.getMainExecutor(context)
|
||||
val notifyChanged = { mainExecutor.execute { revision++ } }
|
||||
val callback =
|
||||
object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
mainExecutor.execute { revision++ }
|
||||
notifyChanged()
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
mainExecutor.execute { revision++ }
|
||||
notifyChanged()
|
||||
}
|
||||
|
||||
override fun onLinkPropertiesChanged(
|
||||
network: Network,
|
||||
linkProperties: LinkProperties,
|
||||
) {
|
||||
mainExecutor.execute { revision++ }
|
||||
notifyChanged()
|
||||
}
|
||||
|
||||
override fun onCapabilitiesChanged(
|
||||
network: Network,
|
||||
networkCapabilities: NetworkCapabilities,
|
||||
) {
|
||||
mainExecutor.execute { revision++ }
|
||||
notifyChanged()
|
||||
}
|
||||
}
|
||||
runCatching { manager?.registerNetworkCallback(NetworkRequest.Builder().build(), callback) }
|
||||
|
||||
@@ -65,23 +65,33 @@ fun AddSourceRoute(
|
||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||
val offline by viewModel.offline.collectAsStateWithLifecycle()
|
||||
val localNetworkGate = rememberLocalNetworkGate()
|
||||
var gatedBusy by remember { mutableStateOf(false) }
|
||||
|
||||
fun runGated(
|
||||
onGranted: () -> Unit,
|
||||
onDenied: () -> Unit = onGranted,
|
||||
) {
|
||||
if (gatedBusy) return
|
||||
gatedBusy = true
|
||||
if (offline) {
|
||||
gatedBusy = false
|
||||
onGranted()
|
||||
} else {
|
||||
localNetworkGate.run(
|
||||
address = state.address,
|
||||
onGranted = onGranted,
|
||||
onDenied = onDenied,
|
||||
onGranted = {
|
||||
gatedBusy = false
|
||||
onGranted()
|
||||
},
|
||||
onDenied = {
|
||||
gatedBusy = false
|
||||
onDenied()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
AddSourceScreen(
|
||||
state = state,
|
||||
state = if (gatedBusy) state.copy(busy = true) else state,
|
||||
onNameChange = viewModel::setName,
|
||||
onAddressChange = viewModel::setAddress,
|
||||
onUsernameChange = viewModel::setUsername,
|
||||
|
||||
Reference in New Issue
Block a user