From 5ab2f43cdf4df7f533c618a586f715583468cec1 Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:07:10 +0000 Subject: [PATCH] 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. --- .../subtracks/data/net/LocalNetworkAccess.kt | 2 +- .../main/kotlin/com/subtracks/ui/AppRoot.kt | 5 +- .../ui/components/LocalNetworkGate.kt | 77 ++++++++++++------- .../subtracks/ui/settings/AddSourceScreen.kt | 16 +++- 4 files changed, 66 insertions(+), 34 deletions(-) diff --git a/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt b/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt index 04858840..f94a934b 100644 --- a/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt +++ b/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt @@ -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) } } diff --git a/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt b/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt index 00ac82de..a45f60f5 100644 --- a/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt +++ b/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt @@ -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 = { diff --git a/app/src/main/kotlin/com/subtracks/ui/components/LocalNetworkGate.kt b/app/src/main/kotlin/com/subtracks/ui/components/LocalNetworkGate.kt index b4c28acc..e448c9aa 100644 --- a/app/src/main/kotlin/com/subtracks/ui/components/LocalNetworkGate.kt +++ b/app/src/main/kotlin/com/subtracks/ui/components/LocalNetworkGate.kt @@ -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,17 +45,29 @@ class LocalNetworkGate internal constructor( onNotNeeded: () -> Unit = onGranted, automatic: Boolean = false, ) { + cancel() if (address == null) { onNotNeeded() return } - scope.launch { - if (!checker.requiresPermission(address) || (automatic && checker.wasDenied(address))) { - onNotNeeded() - } else { - requestPermission(address, onGranted, onDenied) + val current = generation + check = + scope.launch { + 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() } } @@ -66,34 +83,39 @@ fun rememberLocalNetworkGate(): LocalNetworkGate { val scope = rememberCoroutineScope() var pending by remember { mutableStateOf(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 -> - if (pending == null) pending = PendingRequest(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) } diff --git a/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt b/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt index b5328d85..23c6d5ea 100644 --- a/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt +++ b/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt @@ -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,