diff options
| author | David Göransson <david.goransson@mullvad.net> | 2025-03-03 13:39:04 +0100 |
|---|---|---|
| committer | David Göransson <david.goransson@mullvad.net> | 2025-03-03 20:37:22 +0100 |
| commit | 9eb15e878f8a5aec1cb0963d33b1492308b6a4f7 (patch) | |
| tree | cff01ab58fafdd6da6402f2b9f0056584516109f /android/lib/common | |
| parent | 0199b5872f8268a7017aa202c309b50dafe2c704 (diff) | |
| download | mullvadvpn-9eb15e878f8a5aec1cb0963d33b1492308b6a4f7.tar.xz mullvadvpn-9eb15e878f8a5aec1cb0963d33b1492308b6a4f7.zip | |
Fix connectivity listener
Fixes an issue where another VPN app or user having unfortunate
timing of turning on airplane mode and connecting at almost the same
time would leave a lingering network cached in the scan. This fix will
start with the all networks state and give the networkEvents flow 1
second to start up and emit the actual network state.
Diffstat (limited to 'android/lib/common')
| -rw-r--r-- | android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt b/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt index bf94c80778..863fc32251 100644 --- a/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt +++ b/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt @@ -1,9 +1,26 @@ package net.mullvad.mullvadvpn.lib.common.util +import kotlin.time.Duration +import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.withIndex import kotlinx.coroutines.withTimeoutOrNull suspend fun <T> Flow<T>.firstOrNullWithTimeout(timeMillis: Long): T? { return withTimeoutOrNull(timeMillis) { firstOrNull() } } + +@OptIn(FlowPreview::class) +fun <T> Flow<T>.debounceFirst(timeout: Duration): Flow<T> = + withIndex() + .debounce { + if (it.index == 0) { + timeout + } else { + Duration.ZERO + } + } + .map { it.value } |
