From f73bee3a9a7a9cb0afbb72c7525fec2d1c07ae49 Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Wed, 11 Nov 2026 02:10:10 +0000 Subject: [PATCH] net: keep one gate instance and bind actions to the checked address Remember the gate across recompositions so cancel() reaches the running check, rethrow cancellation instead of reading it as permission-not- needed, and abandon a form action when the address changed under it. Only retry a sync that was running or failed after the prompt, and wait for the persisted offline state before checking the network. --- .../subtracks/data/repo/SourceRepository.kt | 4 +++ .../main/kotlin/com/subtracks/ui/AppRoot.kt | 12 +++++-- .../ui/components/LocalNetworkGate.kt | 34 +++++++++++++------ .../subtracks/ui/settings/AddSourceScreen.kt | 23 +++++++------ .../ui/settings/AddSourceViewModel.kt | 2 ++ 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/com/subtracks/data/repo/SourceRepository.kt b/app/src/main/kotlin/com/subtracks/data/repo/SourceRepository.kt index c34cb274..e1bc3eef 100644 --- a/app/src/main/kotlin/com/subtracks/data/repo/SourceRepository.kt +++ b/app/src/main/kotlin/com/subtracks/data/repo/SourceRepository.kt @@ -71,6 +71,9 @@ class SourceRepository( private val _offline = MutableStateFlow(false) val offline: StateFlow = _offline + private val _offlineLoaded = MutableStateFlow(false) + val offlineLoaded: StateFlow = _offlineLoaded + private val _activeSource = MutableStateFlow(null) val activeSource: StateFlow = _activeSource @@ -95,6 +98,7 @@ class SourceRepository( // Read the persisted flag before the source config can build a source or probe it, so a // cold start with offline on never touches the network. _offline.value = prefs.offlineMode().first() + _offlineLoaded.value = true var lastMode: NetworkMode? = null combine( db.sourcesDao().activeSubsonicConfig(), diff --git a/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt b/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt index a45f60f5..3830fcff 100644 --- a/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt +++ b/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt @@ -123,6 +123,8 @@ class RootViewModel( .stateIn(viewModelScope, SharingStarted.Eagerly, null) val offline = sourceRepository.offline + + val offlineLoaded = sourceRepository.offlineLoaded } private const val NAVIGATION_DURATION_MS = 260 @@ -209,17 +211,23 @@ fun SubtracksRoot(root: RootViewModel = koinViewModel()) { val syncManager = koinInject() val serverAddress by root.serverAddress.collectAsStateWithLifecycle() val offline by root.offline.collectAsStateWithLifecycle() + val offlineLoaded by root.offlineLoaded.collectAsStateWithLifecycle() val networkRevision = rememberNetworkRevision() - LaunchedEffect(serverAddress, networkRevision, offline) { + LaunchedEffect(serverAddress, networkRevision, offline, offlineLoaded) { + if (!offlineLoaded) return@LaunchedEffect if (offline) { localNetworkGate.cancel() return@LaunchedEffect } + val statusBeforePrompt = syncManager.status.value localNetworkGate.run( address = serverAddress, onGranted = { val status = syncManager.status.value - if (status is SyncStatus.Running || status is SyncStatus.Failed) syncManager.requestSync() + val raced = + status is SyncStatus.Running || + (status is SyncStatus.Failed && statusBeforePrompt !is SyncStatus.Failed) + if (raced) syncManager.requestSync() }, onNotNeeded = {}, automatic = true, 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 e448c9aa..7f6caf3b 100644 --- a/app/src/main/kotlin/com/subtracks/ui/components/LocalNetworkGate.kt +++ b/app/src/main/kotlin/com/subtracks/ui/components/LocalNetworkGate.kt @@ -24,8 +24,10 @@ import androidx.core.content.ContextCompat import com.subtracks.R import com.subtracks.data.net.LOCAL_NETWORK_PERMISSION import com.subtracks.data.net.LocalNetworkAccess +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job +import kotlinx.coroutines.ensureActive import kotlinx.coroutines.launch import org.koin.compose.koinInject @@ -53,8 +55,16 @@ class LocalNetworkGate internal constructor( val current = generation check = scope.launch { - val required = runCatching { checker.requiresPermission(address) }.getOrDefault(false) + val required = + try { + checker.requiresPermission(address) + } catch (cancellation: CancellationException) { + throw cancellation + } catch (_: Exception) { + false + } if (current != generation) return@launch + ensureActive() if (required && !(automatic && checker.wasDenied(address))) { requestPermission(address, onGranted, onDenied) } else { @@ -101,16 +111,18 @@ fun rememberLocalNetworkGate(): LocalNetworkGate { } val gate = - LocalNetworkGate( - scope = scope, - checker = checker, - requestPermission = { address, onGranted, onDenied -> - if (pending == null) pending = PendingRequest(address, onGranted, onDenied) - }, - clearPrompt = { - if (!awaitingSystemPrompt) pending = null - }, - ) + remember(scope, checker) { + 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) { 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 23c6d5ea..a577a144 100644 --- a/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt +++ b/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt @@ -64,6 +64,7 @@ fun AddSourceRoute( ) { val state by viewModel.state.collectAsStateWithLifecycle() val offline by viewModel.offline.collectAsStateWithLifecycle() + val offlineLoaded by viewModel.offlineLoaded.collectAsStateWithLifecycle() val localNetworkGate = rememberLocalNetworkGate() var gatedBusy by remember { mutableStateOf(false) } @@ -73,20 +74,20 @@ fun AddSourceRoute( ) { if (gatedBusy) return gatedBusy = true - if (offline) { + val address = state.address + + fun finish(action: () -> Unit) { gatedBusy = false - onGranted() + if (viewModel.state.value.address == address) action() + } + + if (!offlineLoaded || offline) { + finish(onGranted) } else { localNetworkGate.run( - address = state.address, - onGranted = { - gatedBusy = false - onGranted() - }, - onDenied = { - gatedBusy = false - onDenied() - }, + address = address, + onGranted = { finish(onGranted) }, + onDenied = { finish(onDenied) }, ) } } diff --git a/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceViewModel.kt b/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceViewModel.kt index b05ea112..3b421bf9 100644 --- a/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceViewModel.kt +++ b/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceViewModel.kt @@ -49,6 +49,8 @@ class AddSourceViewModel( val offline: StateFlow = sourceRepository.offline + val offlineLoaded: StateFlow = sourceRepository.offlineLoaded + init { sourceId?.let { id -> viewModelScope.launch {