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 (Build.VERSION.SDK_INT < LOCAL_NETWORK_PERMISSION_SDK) return false
|
||||||
if (granted()) return false
|
if (granted()) return false
|
||||||
val host = serverHost(address) ?: return false
|
val host = serverHost(address) ?: return false
|
||||||
if (isLocalHostname(host)) return true
|
|
||||||
val linkAddresses = currentLinkAddresses()
|
val linkAddresses = currentLinkAddresses()
|
||||||
if (linkAddresses.isEmpty()) return false
|
if (linkAddresses.isEmpty()) return false
|
||||||
|
if (isLocalHostname(host)) return true
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
val resolved = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList())
|
val resolved = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList())
|
||||||
resolved.any { candidate -> linkAddresses.any { it.contains(candidate) } }
|
resolved.any { candidate -> linkAddresses.any { it.contains(candidate) } }
|
||||||
|
|||||||
@@ -211,7 +211,10 @@ fun SubtracksRoot(root: RootViewModel = koinViewModel()) {
|
|||||||
val offline by root.offline.collectAsStateWithLifecycle()
|
val offline by root.offline.collectAsStateWithLifecycle()
|
||||||
val networkRevision = rememberNetworkRevision()
|
val networkRevision = rememberNetworkRevision()
|
||||||
LaunchedEffect(serverAddress, networkRevision, offline) {
|
LaunchedEffect(serverAddress, networkRevision, offline) {
|
||||||
if (offline) return@LaunchedEffect
|
if (offline) {
|
||||||
|
localNetworkGate.cancel()
|
||||||
|
return@LaunchedEffect
|
||||||
|
}
|
||||||
localNetworkGate.run(
|
localNetworkGate.run(
|
||||||
address = serverAddress,
|
address = serverAddress,
|
||||||
onGranted = {
|
onGranted = {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import com.subtracks.R
|
|||||||
import com.subtracks.data.net.LOCAL_NETWORK_PERMISSION
|
import com.subtracks.data.net.LOCAL_NETWORK_PERMISSION
|
||||||
import com.subtracks.data.net.LocalNetworkAccess
|
import com.subtracks.data.net.LocalNetworkAccess
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.koin.compose.koinInject
|
import org.koin.compose.koinInject
|
||||||
|
|
||||||
@@ -32,7 +33,11 @@ class LocalNetworkGate internal constructor(
|
|||||||
private val scope: CoroutineScope,
|
private val scope: CoroutineScope,
|
||||||
private val checker: LocalNetworkAccess,
|
private val checker: LocalNetworkAccess,
|
||||||
private val requestPermission: (address: String, onGranted: () -> Unit, onDenied: () -> Unit) -> Unit,
|
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(
|
fun run(
|
||||||
address: String?,
|
address: String?,
|
||||||
onGranted: () -> Unit,
|
onGranted: () -> Unit,
|
||||||
@@ -40,18 +45,30 @@ class LocalNetworkGate internal constructor(
|
|||||||
onNotNeeded: () -> Unit = onGranted,
|
onNotNeeded: () -> Unit = onGranted,
|
||||||
automatic: Boolean = false,
|
automatic: Boolean = false,
|
||||||
) {
|
) {
|
||||||
|
cancel()
|
||||||
if (address == null) {
|
if (address == null) {
|
||||||
onNotNeeded()
|
onNotNeeded()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
val current = generation
|
||||||
|
check =
|
||||||
scope.launch {
|
scope.launch {
|
||||||
if (!checker.requiresPermission(address) || (automatic && checker.wasDenied(address))) {
|
val required = runCatching { checker.requiresPermission(address) }.getOrDefault(false)
|
||||||
onNotNeeded()
|
if (current != generation) return@launch
|
||||||
} else {
|
if (required && !(automatic && checker.wasDenied(address))) {
|
||||||
requestPermission(address, onGranted, onDenied)
|
requestPermission(address, onGranted, onDenied)
|
||||||
|
} else {
|
||||||
|
onNotNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun cancel() {
|
||||||
|
generation++
|
||||||
|
check?.cancel()
|
||||||
|
check = null
|
||||||
|
clearPrompt()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class PendingRequest(
|
private class PendingRequest(
|
||||||
@@ -66,34 +83,39 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
|||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
var pending by remember { mutableStateOf<PendingRequest?>(null) }
|
var pending by remember { mutableStateOf<PendingRequest?>(null) }
|
||||||
var awaitingSystemPrompt by remember { mutableStateOf(false) }
|
var awaitingSystemPrompt by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
fun deny(request: PendingRequest) {
|
||||||
|
pending = null
|
||||||
|
checker.markDenied(request.address)
|
||||||
|
request.onDenied()
|
||||||
|
}
|
||||||
|
|
||||||
val launcher =
|
val launcher =
|
||||||
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||||
val request = pending
|
val request = pending
|
||||||
pending = null
|
pending = null
|
||||||
awaitingSystemPrompt = false
|
awaitingSystemPrompt = false
|
||||||
if (request != null) {
|
if (request != null) {
|
||||||
if (granted) {
|
if (granted) request.onGranted() else deny(request)
|
||||||
request.onGranted()
|
|
||||||
} else {
|
|
||||||
checker.markDenied(request.address)
|
|
||||||
request.onDenied()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val gate =
|
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)
|
if (pending == null) pending = PendingRequest(address, onGranted, onDenied)
|
||||||
}
|
},
|
||||||
|
clearPrompt = {
|
||||||
|
if (!awaitingSystemPrompt) pending = null
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
val request = pending
|
val request = pending
|
||||||
if (request != null && !awaitingSystemPrompt) {
|
if (request != null && !awaitingSystemPrompt) {
|
||||||
AlertDialog(
|
AlertDialog(
|
||||||
onDismissRequest = {
|
onDismissRequest = { deny(request) },
|
||||||
pending = null
|
|
||||||
checker.markDenied(request.address)
|
|
||||||
request.onDenied()
|
|
||||||
},
|
|
||||||
title = { Text(stringResource(R.string.local_network_permission_title)) },
|
title = { Text(stringResource(R.string.local_network_permission_title)) },
|
||||||
text = { Text(stringResource(R.string.local_network_permission_body)) },
|
text = { Text(stringResource(R.string.local_network_permission_body)) },
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
@@ -108,11 +130,7 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
|||||||
},
|
},
|
||||||
dismissButton = {
|
dismissButton = {
|
||||||
TextButton(
|
TextButton(
|
||||||
onClick = {
|
onClick = { deny(request) },
|
||||||
pending = null
|
|
||||||
checker.markDenied(request.address)
|
|
||||||
request.onDenied()
|
|
||||||
},
|
|
||||||
) {
|
) {
|
||||||
Text(stringResource(R.string.actions_cancel))
|
Text(stringResource(R.string.actions_cancel))
|
||||||
}
|
}
|
||||||
@@ -129,28 +147,29 @@ internal fun rememberNetworkRevision(): Int {
|
|||||||
DisposableEffect(context) {
|
DisposableEffect(context) {
|
||||||
val manager = context.getSystemService(ConnectivityManager::class.java)
|
val manager = context.getSystemService(ConnectivityManager::class.java)
|
||||||
val mainExecutor = ContextCompat.getMainExecutor(context)
|
val mainExecutor = ContextCompat.getMainExecutor(context)
|
||||||
|
val notifyChanged = { mainExecutor.execute { revision++ } }
|
||||||
val callback =
|
val callback =
|
||||||
object : ConnectivityManager.NetworkCallback() {
|
object : ConnectivityManager.NetworkCallback() {
|
||||||
override fun onAvailable(network: Network) {
|
override fun onAvailable(network: Network) {
|
||||||
mainExecutor.execute { revision++ }
|
notifyChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onLost(network: Network) {
|
override fun onLost(network: Network) {
|
||||||
mainExecutor.execute { revision++ }
|
notifyChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onLinkPropertiesChanged(
|
override fun onLinkPropertiesChanged(
|
||||||
network: Network,
|
network: Network,
|
||||||
linkProperties: LinkProperties,
|
linkProperties: LinkProperties,
|
||||||
) {
|
) {
|
||||||
mainExecutor.execute { revision++ }
|
notifyChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCapabilitiesChanged(
|
override fun onCapabilitiesChanged(
|
||||||
network: Network,
|
network: Network,
|
||||||
networkCapabilities: NetworkCapabilities,
|
networkCapabilities: NetworkCapabilities,
|
||||||
) {
|
) {
|
||||||
mainExecutor.execute { revision++ }
|
notifyChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
runCatching { manager?.registerNetworkCallback(NetworkRequest.Builder().build(), callback) }
|
runCatching { manager?.registerNetworkCallback(NetworkRequest.Builder().build(), callback) }
|
||||||
|
|||||||
@@ -65,23 +65,33 @@ fun AddSourceRoute(
|
|||||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||||
val offline by viewModel.offline.collectAsStateWithLifecycle()
|
val offline by viewModel.offline.collectAsStateWithLifecycle()
|
||||||
val localNetworkGate = rememberLocalNetworkGate()
|
val localNetworkGate = rememberLocalNetworkGate()
|
||||||
|
var gatedBusy by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
fun runGated(
|
fun runGated(
|
||||||
onGranted: () -> Unit,
|
onGranted: () -> Unit,
|
||||||
onDenied: () -> Unit = onGranted,
|
onDenied: () -> Unit = onGranted,
|
||||||
) {
|
) {
|
||||||
|
if (gatedBusy) return
|
||||||
|
gatedBusy = true
|
||||||
if (offline) {
|
if (offline) {
|
||||||
|
gatedBusy = false
|
||||||
onGranted()
|
onGranted()
|
||||||
} else {
|
} else {
|
||||||
localNetworkGate.run(
|
localNetworkGate.run(
|
||||||
address = state.address,
|
address = state.address,
|
||||||
onGranted = onGranted,
|
onGranted = {
|
||||||
onDenied = onDenied,
|
gatedBusy = false
|
||||||
|
onGranted()
|
||||||
|
},
|
||||||
|
onDenied = {
|
||||||
|
gatedBusy = false
|
||||||
|
onDenied()
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AddSourceScreen(
|
AddSourceScreen(
|
||||||
state = state,
|
state = if (gatedBusy) state.copy(busy = true) else state,
|
||||||
onNameChange = viewModel::setName,
|
onNameChange = viewModel::setName,
|
||||||
onAddressChange = viewModel::setAddress,
|
onAddressChange = viewModel::setAddress,
|
||||||
onUsernameChange = viewModel::setUsername,
|
onUsernameChange = viewModel::setUsername,
|
||||||
|
|||||||
Reference in New Issue
Block a user