From 2d9c946e9641756df40eccba7a4387a38cb603c2 Mon Sep 17 00:00:00 2001 From: Jamil Date: Tue, 27 May 2025 13:41:36 -0700 Subject: [PATCH] refactor(android): use early returns in connect logic (#9280) Our connect logic is more complex than it needs to be. This aims to simplify that a bit to aid in future maintenance. Related: https://github.com/firezone/firezone/pull/9279#discussion_r2108127619 --- .../features/splash/ui/SplashViewModel.kt | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/kotlin/android/app/src/main/java/dev/firezone/android/features/splash/ui/SplashViewModel.kt b/kotlin/android/app/src/main/java/dev/firezone/android/features/splash/ui/SplashViewModel.kt index 76ddf788d..0a750a9b0 100644 --- a/kotlin/android/app/src/main/java/dev/firezone/android/features/splash/ui/SplashViewModel.kt +++ b/kotlin/android/app/src/main/java/dev/firezone/android/features/splash/ui/SplashViewModel.kt @@ -41,36 +41,41 @@ internal class SplashViewModel // Stay a while and enjoy the logo delay(REQUEST_DELAY) - // If this is an 'initial launch' call, but we've already handled the - // initial launch logic for this ViewModel instance, then do nothing. + // We've already posted the initial action, so we can skip the rest of the checks if (isInitialLaunch && hasPerformedInitialLaunchCheck) { return@launch } + if (isInitialLaunch) { + hasPerformedInitialLaunchCheck = true + } + + // If we don't have VPN permission, we can't continue. if (!hasVpnPermissions(context) && applicationMode != ApplicationMode.TESTING) { actionMutableLiveData.postValue(ViewAction.NavigateToVpnPermission) - } else { - val token = applicationRestrictions.getString("token") ?: repo.getTokenSync() - val connectOnStart = repo.getConfigSync().connectOnStart - - // Determine if the tunnel should connect: - // 1. If it's an initial launch AND connectOnStart is true. - // OR - // 2. If it's NOT an initial launch (meaning it's a resume), always try to connect if a token exists. - if (!token.isNullOrBlank() && (isInitialLaunch && connectOnStart || !isInitialLaunch)) { - // token will be re-read by the TunnelService - if (!TunnelService.isRunning(context)) TunnelService.start(context) - - actionMutableLiveData.postValue(ViewAction.NavigateToSession) - } else { - actionMutableLiveData.postValue(ViewAction.NavigateToSignIn) - } + return@launch } - } - // Set the flag to true after the initial launch check is performed - if (isInitialLaunch) { - hasPerformedInitialLaunchCheck = true + val token = applicationRestrictions.getString("token") ?: repo.getTokenSync() + val connectOnStart = repo.getConfigSync().connectOnStart + + // If we don't have a token, we can't connect. + if (token.isNullOrBlank()) { + actionMutableLiveData.postValue(ViewAction.NavigateToSignIn) + return@launch + } + + // If it's the initial launch but connect on start isn't enabled, we navigate to sign in. + if (isInitialLaunch && !connectOnStart) { + actionMutableLiveData.postValue(ViewAction.NavigateToSignIn) + return@launch + } + + // If we reach here, we have a token and should attempt to connect. + if (!TunnelService.isRunning(context)) { + TunnelService.start(context) + } + actionMutableLiveData.postValue(ViewAction.NavigateToSession) } }