mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-16 01:00:17 +01:00
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.
This commit is contained in:
@@ -71,6 +71,9 @@ class SourceRepository(
|
||||
private val _offline = MutableStateFlow(false)
|
||||
val offline: StateFlow<Boolean> = _offline
|
||||
|
||||
private val _offlineLoaded = MutableStateFlow(false)
|
||||
val offlineLoaded: StateFlow<Boolean> = _offlineLoaded
|
||||
|
||||
private val _activeSource = MutableStateFlow<Long?>(null)
|
||||
val activeSource: StateFlow<Long?> = _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(),
|
||||
|
||||
@@ -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<SyncManager>()
|
||||
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,
|
||||
|
||||
@@ -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,6 +111,7 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
||||
}
|
||||
|
||||
val gate =
|
||||
remember(scope, checker) {
|
||||
LocalNetworkGate(
|
||||
scope = scope,
|
||||
checker = checker,
|
||||
@@ -111,6 +122,7 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
||||
if (!awaitingSystemPrompt) pending = null
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
val request = pending
|
||||
if (request != null && !awaitingSystemPrompt) {
|
||||
|
||||
@@ -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) },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ class AddSourceViewModel(
|
||||
|
||||
val offline: StateFlow<Boolean> = sourceRepository.offline
|
||||
|
||||
val offlineLoaded: StateFlow<Boolean> = sourceRepository.offlineLoaded
|
||||
|
||||
init {
|
||||
sourceId?.let { id ->
|
||||
viewModelScope.launch {
|
||||
|
||||
Reference in New Issue
Block a user