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)
|
private val _offline = MutableStateFlow(false)
|
||||||
val offline: StateFlow<Boolean> = _offline
|
val offline: StateFlow<Boolean> = _offline
|
||||||
|
|
||||||
|
private val _offlineLoaded = MutableStateFlow(false)
|
||||||
|
val offlineLoaded: StateFlow<Boolean> = _offlineLoaded
|
||||||
|
|
||||||
private val _activeSource = MutableStateFlow<Long?>(null)
|
private val _activeSource = MutableStateFlow<Long?>(null)
|
||||||
val activeSource: StateFlow<Long?> = _activeSource
|
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
|
// 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.
|
// cold start with offline on never touches the network.
|
||||||
_offline.value = prefs.offlineMode().first()
|
_offline.value = prefs.offlineMode().first()
|
||||||
|
_offlineLoaded.value = true
|
||||||
var lastMode: NetworkMode? = null
|
var lastMode: NetworkMode? = null
|
||||||
combine(
|
combine(
|
||||||
db.sourcesDao().activeSubsonicConfig(),
|
db.sourcesDao().activeSubsonicConfig(),
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ class RootViewModel(
|
|||||||
.stateIn(viewModelScope, SharingStarted.Eagerly, null)
|
.stateIn(viewModelScope, SharingStarted.Eagerly, null)
|
||||||
|
|
||||||
val offline = sourceRepository.offline
|
val offline = sourceRepository.offline
|
||||||
|
|
||||||
|
val offlineLoaded = sourceRepository.offlineLoaded
|
||||||
}
|
}
|
||||||
|
|
||||||
private const val NAVIGATION_DURATION_MS = 260
|
private const val NAVIGATION_DURATION_MS = 260
|
||||||
@@ -209,17 +211,23 @@ fun SubtracksRoot(root: RootViewModel = koinViewModel()) {
|
|||||||
val syncManager = koinInject<SyncManager>()
|
val syncManager = koinInject<SyncManager>()
|
||||||
val serverAddress by root.serverAddress.collectAsStateWithLifecycle()
|
val serverAddress by root.serverAddress.collectAsStateWithLifecycle()
|
||||||
val offline by root.offline.collectAsStateWithLifecycle()
|
val offline by root.offline.collectAsStateWithLifecycle()
|
||||||
|
val offlineLoaded by root.offlineLoaded.collectAsStateWithLifecycle()
|
||||||
val networkRevision = rememberNetworkRevision()
|
val networkRevision = rememberNetworkRevision()
|
||||||
LaunchedEffect(serverAddress, networkRevision, offline) {
|
LaunchedEffect(serverAddress, networkRevision, offline, offlineLoaded) {
|
||||||
|
if (!offlineLoaded) return@LaunchedEffect
|
||||||
if (offline) {
|
if (offline) {
|
||||||
localNetworkGate.cancel()
|
localNetworkGate.cancel()
|
||||||
return@LaunchedEffect
|
return@LaunchedEffect
|
||||||
}
|
}
|
||||||
|
val statusBeforePrompt = syncManager.status.value
|
||||||
localNetworkGate.run(
|
localNetworkGate.run(
|
||||||
address = serverAddress,
|
address = serverAddress,
|
||||||
onGranted = {
|
onGranted = {
|
||||||
val status = syncManager.status.value
|
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 = {},
|
onNotNeeded = {},
|
||||||
automatic = true,
|
automatic = true,
|
||||||
|
|||||||
@@ -24,8 +24,10 @@ import androidx.core.content.ContextCompat
|
|||||||
import com.subtracks.R
|
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.CancellationException
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.ensureActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.koin.compose.koinInject
|
import org.koin.compose.koinInject
|
||||||
|
|
||||||
@@ -53,8 +55,16 @@ class LocalNetworkGate internal constructor(
|
|||||||
val current = generation
|
val current = generation
|
||||||
check =
|
check =
|
||||||
scope.launch {
|
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
|
if (current != generation) return@launch
|
||||||
|
ensureActive()
|
||||||
if (required && !(automatic && checker.wasDenied(address))) {
|
if (required && !(automatic && checker.wasDenied(address))) {
|
||||||
requestPermission(address, onGranted, onDenied)
|
requestPermission(address, onGranted, onDenied)
|
||||||
} else {
|
} else {
|
||||||
@@ -101,6 +111,7 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val gate =
|
val gate =
|
||||||
|
remember(scope, checker) {
|
||||||
LocalNetworkGate(
|
LocalNetworkGate(
|
||||||
scope = scope,
|
scope = scope,
|
||||||
checker = checker,
|
checker = checker,
|
||||||
@@ -111,6 +122,7 @@ fun rememberLocalNetworkGate(): LocalNetworkGate {
|
|||||||
if (!awaitingSystemPrompt) pending = null
|
if (!awaitingSystemPrompt) pending = null
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
val request = pending
|
val request = pending
|
||||||
if (request != null && !awaitingSystemPrompt) {
|
if (request != null && !awaitingSystemPrompt) {
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ 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 offlineLoaded by viewModel.offlineLoaded.collectAsStateWithLifecycle()
|
||||||
val localNetworkGate = rememberLocalNetworkGate()
|
val localNetworkGate = rememberLocalNetworkGate()
|
||||||
var gatedBusy by remember { mutableStateOf(false) }
|
var gatedBusy by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
@@ -73,20 +74,20 @@ fun AddSourceRoute(
|
|||||||
) {
|
) {
|
||||||
if (gatedBusy) return
|
if (gatedBusy) return
|
||||||
gatedBusy = true
|
gatedBusy = true
|
||||||
if (offline) {
|
val address = state.address
|
||||||
|
|
||||||
|
fun finish(action: () -> Unit) {
|
||||||
gatedBusy = false
|
gatedBusy = false
|
||||||
onGranted()
|
if (viewModel.state.value.address == address) action()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!offlineLoaded || offline) {
|
||||||
|
finish(onGranted)
|
||||||
} else {
|
} else {
|
||||||
localNetworkGate.run(
|
localNetworkGate.run(
|
||||||
address = state.address,
|
address = address,
|
||||||
onGranted = {
|
onGranted = { finish(onGranted) },
|
||||||
gatedBusy = false
|
onDenied = { finish(onDenied) },
|
||||||
onGranted()
|
|
||||||
},
|
|
||||||
onDenied = {
|
|
||||||
gatedBusy = false
|
|
||||||
onDenied()
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ class AddSourceViewModel(
|
|||||||
|
|
||||||
val offline: StateFlow<Boolean> = sourceRepository.offline
|
val offline: StateFlow<Boolean> = sourceRepository.offline
|
||||||
|
|
||||||
|
val offlineLoaded: StateFlow<Boolean> = sourceRepository.offlineLoaded
|
||||||
|
|
||||||
init {
|
init {
|
||||||
sourceId?.let { id ->
|
sourceId?.let { id ->
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
|||||||
Reference in New Issue
Block a user