diff options
| author | Albin <albin@mullvad.net> | 2023-05-03 13:31:49 +0200 |
|---|---|---|
| committer | Albin <albin@mullvad.net> | 2023-05-03 13:31:49 +0200 |
| commit | 63090b2f13eb213e9e19447910b4ec4525e29764 (patch) | |
| tree | d3bc8e53d4f94f9af232044108729b421efa7c00 /android/app/src | |
| parent | b8091d32497d52171ffc3888094d52856d96b194 (diff) | |
| parent | 356125ab344ca823202644256b11e5a878beaca6 (diff) | |
| download | mullvadvpn-63090b2f13eb213e9e19447910b4ec4525e29764.tar.xz mullvadvpn-63090b2f13eb213e9e19447910b4ec4525e29764.zip | |
Merge branch 'fix-app-resume-regression-droid-101'
Diffstat (limited to 'android/app/src')
| -rw-r--r-- | android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt | 70 |
1 files changed, 37 insertions, 33 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt index d3c0d07315..ceb67ebc6c 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt @@ -18,9 +18,8 @@ import androidx.compose.ui.platform.ViewCompositionStrategy import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentManager -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.filter @@ -81,6 +80,9 @@ open class MainActivity : FragmentActivity() { private lateinit var serviceConnectionManager: ServiceConnectionManager private lateinit var changelogViewModel: ChangelogViewModel + private var deviceStateJob: Job? = null + private var currentDeviceState: DeviceState? = null + override fun onCreate(savedInstanceState: Bundle?) { loadKoinModules(uiModule) @@ -125,7 +127,7 @@ open class MainActivity : FragmentActivity() { fun initializeStateHandlerAndServiceConnection( apiEndpointConfiguration: ApiEndpointConfiguration? ) { - launchDeviceStateHandler() + deviceStateJob = launchDeviceStateHandler() checkForNotificationPermission() serviceConnectionManager.bind( vpnPermissionRequestHandler = ::requestVpnPermission, @@ -152,6 +154,8 @@ open class MainActivity : FragmentActivity() { // NOTE: `super.onStop()` must be called before unbinding due to the fragment state handling // otherwise the fragments will believe there was an unexpected disconnect. serviceConnectionManager.unbind() + + deviceStateJob?.cancel() } override fun onDestroy() { @@ -193,39 +197,39 @@ open class MainActivity : FragmentActivity() { } } - private fun launchDeviceStateHandler() { - var currentState: DeviceState? = null + private fun launchDeviceStateHandler(): Job { - lifecycleScope.launch { - deviceRepository.deviceState - .flowWithLifecycle(lifecycle, Lifecycle.State.RESUMED) - .debounce { - // Debounce DeviceState.Unknown to delay view transitions during reconnect. - it.addDebounceForUnknownState(UNKNOWN_STATE_DEBOUNCE_DELAY_MILLISECONDS) - } - .collect { newState -> - if (newState != currentState) { - when (newState) { - is DeviceState.Initial, - is DeviceState.Unknown -> openLaunchView() - is DeviceState.LoggedOut -> openLoginView() - is DeviceState.Revoked -> openRevokedView() - is DeviceState.LoggedIn -> { - openLoggedInView( - accountToken = newState.accountAndDevice.account_token, - shouldDelayLogin = currentState is DeviceState.LoggedOut - ) + return lifecycleScope.launch { + launch { + deviceRepository.deviceState + .debounce { + // Debounce DeviceState.Unknown to delay view transitions during reconnect. + it.addDebounceForUnknownState(UNKNOWN_STATE_DEBOUNCE_DELAY_MILLISECONDS) + } + .collect { newState -> + if (newState != currentDeviceState) + when (newState) { + is DeviceState.Initial, + is DeviceState.Unknown -> openLaunchView() + is DeviceState.LoggedOut -> openLoginView() + is DeviceState.Revoked -> openRevokedView() + is DeviceState.LoggedIn -> { + openLoggedInView( + accountToken = newState.accountAndDevice.account_token, + shouldDelayLogin = + currentDeviceState is DeviceState.LoggedOut + ) + } } - } - currentState = newState + currentDeviceState = newState } - } - } - lifecycleScope.launch { - deviceRepository.deviceState - .flowWithLifecycle(lifecycle, Lifecycle.State.RESUMED) - .filter { it is DeviceState.LoggedIn || it is DeviceState.LoggedOut } - .collect { loadChangelogComponent() } + } + + lifecycleScope.launch { + deviceRepository.deviceState + .filter { it is DeviceState.LoggedIn || it is DeviceState.LoggedOut } + .collect { loadChangelogComponent() } + } } } |
