Skip to content

Commit

Permalink
Simplify code using LaunchCollectEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Feb 28, 2024
1 parent ebd1691 commit a473e46
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
Expand All @@ -22,6 +21,7 @@ import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.NegativeButton
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.textfield.DnsTextField
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.viewmodel.DnsDialogSideEffect
Expand Down Expand Up @@ -100,11 +100,9 @@ fun DnsDialog(
val viewModel =
koinViewModel<DnsDialogViewModel>(parameters = { parametersOf(initialValue, index) })

LaunchedEffect(Unit) {
viewModel.uiSideEffect.collect {
when (it) {
DnsDialogSideEffect.Complete -> resultNavigator.navigateBack(result = true)
}
LaunchedEffectCollect(viewModel.uiSideEffect) {
when (it) {
DnsDialogSideEffect.Complete -> resultNavigator.navigateBack(result = true)
}
}
val state by viewModel.uiState.collectAsState(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
Expand All @@ -21,6 +20,7 @@ import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.textfield.MtuTextField
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.constant.MTU_MAX_VALUE
import net.mullvad.mullvadvpn.constant.MTU_MIN_VALUE
import net.mullvad.mullvadvpn.lib.theme.AppTheme
Expand All @@ -42,11 +42,9 @@ private fun PreviewMtuDialog() {
fun MtuDialog(mtuInitial: Int?, navigator: DestinationsNavigator) {
val viewModel = koinViewModel<MtuDialogViewModel>()

LaunchedEffect(Unit) {
viewModel.uiSideEffect.collect {
when (it) {
MtuDialogSideEffect.Complete -> navigator.navigateUp()
}
LaunchedEffectCollect(viewModel.uiSideEffect) {
when (it) {
MtuDialogSideEffect.Complete -> navigator.navigateUp()
}
}
MtuDialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorMedium
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.lib.payment.model.ProductId
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.color.AlphaDescription
Expand Down Expand Up @@ -125,12 +126,10 @@ fun Payment(productId: ProductId, resultBackNavigator: ResultBackNavigator<Boole
val vm = koinViewModel<PaymentViewModel>()
val uiState = vm.uiState.collectAsState().value

LaunchedEffect(Unit) {
vm.uiSideEffect.collect {
when (it) {
is PaymentUiSideEffect.PaymentCancelled ->
resultBackNavigator.navigateBack(result = false)
}
LaunchedEffectCollect(vm.uiSideEffect) {
when (it) {
is PaymentUiSideEffect.PaymentCancelled ->
resultBackNavigator.navigateBack(result = false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -55,6 +54,7 @@ import net.mullvad.mullvadvpn.compose.destinations.RedeemVoucherDestination
import net.mullvad.mullvadvpn.compose.destinations.VerificationPendingDialogDestination
import net.mullvad.mullvadvpn.compose.state.PaymentState
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromBottomTransition
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.compose.util.SecureScreenWhileInView
import net.mullvad.mullvadvpn.lib.common.util.openAccountPageInBrowser
import net.mullvad.mullvadvpn.lib.payment.model.PaymentProduct
Expand Down Expand Up @@ -171,22 +171,20 @@ fun AccountScreen(
val clipboardManager = LocalClipboardManager.current
val snackbarHostState = remember { SnackbarHostState() }
val copyTextString = stringResource(id = R.string.copied_mullvad_account_number)
LaunchedEffect(Unit) {
uiSideEffect.collect { uiSideEffect ->
when (uiSideEffect) {
AccountViewModel.UiSideEffect.NavigateToLogin -> navigateToLogin()
is AccountViewModel.UiSideEffect.OpenAccountManagementPageInBrowser ->
context.openAccountPageInBrowser(uiSideEffect.token)
is AccountViewModel.UiSideEffect.CopyAccountNumber ->
launch {
clipboardManager.setText(AnnotatedString(uiSideEffect.accountNumber))
LaunchedEffectCollect(uiSideEffect) { sideEffect ->
when (sideEffect) {
AccountViewModel.UiSideEffect.NavigateToLogin -> navigateToLogin()
is AccountViewModel.UiSideEffect.OpenAccountManagementPageInBrowser ->
context.openAccountPageInBrowser(sideEffect.token)
is AccountViewModel.UiSideEffect.CopyAccountNumber ->
launch {
clipboardManager.setText(AnnotatedString(sideEffect.accountNumber))

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
snackbarHostState.currentSnackbarData?.dismiss()
snackbarHostState.showSnackbar(message = copyTextString)
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
snackbarHostState.currentSnackbarData?.dismiss()
snackbarHostState.showSnackbar(message = copyTextString)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import net.mullvad.mullvadvpn.model.LatLong
import net.mullvad.mullvadvpn.model.Latitude
import net.mullvad.mullvadvpn.model.Longitude
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.util.CollectSideEffectWithLifecycle
import net.mullvad.mullvadvpn.util.appendHideNavOnPlayBuild
import net.mullvad.mullvadvpn.viewmodel.ConnectViewModel
import org.koin.androidx.compose.koinViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
Expand All @@ -32,6 +31,7 @@ import net.mullvad.mullvadvpn.compose.component.ScaffoldWithTopBar
import net.mullvad.mullvadvpn.compose.destinations.LoginDestination
import net.mullvad.mullvadvpn.compose.destinations.SettingsDestination
import net.mullvad.mullvadvpn.compose.state.DeviceRevokedUiState
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.viewmodel.DeviceRevokedSideEffect
Expand All @@ -51,16 +51,13 @@ fun DeviceRevoked(navigator: DestinationsNavigator) {

val state by viewModel.uiState.collectAsState()

LaunchedEffect(Unit) {
viewModel.uiSideEffect.collect { sideEffect ->
when (sideEffect) {
DeviceRevokedSideEffect.NavigateToLogin -> {
navigator.navigate(LoginDestination()) {
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
LaunchedEffectCollect(viewModel.uiSideEffect) { sideEffect ->
when (sideEffect) {
DeviceRevokedSideEffect.NavigateToLogin ->
navigator.navigate(LoginDestination()) {
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -39,6 +38,7 @@ import net.mullvad.mullvadvpn.compose.cell.ExpandableComposeCell
import net.mullvad.mullvadvpn.compose.cell.SelectableCell
import net.mullvad.mullvadvpn.compose.state.RelayFilterState
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromRightTransition
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.model.Ownership
Expand Down Expand Up @@ -72,11 +72,9 @@ fun FilterScreen(navigator: DestinationsNavigator) {
val viewModel = koinViewModel<FilterViewModel>()
val uiState by viewModel.uiState.collectAsState()

LaunchedEffect(Unit) {
viewModel.uiSideEffect.collect {
when (it) {
FilterScreenSideEffect.CloseScreen -> navigator.navigateUp()
}
LaunchedEffectCollect(viewModel.uiSideEffect) {
when (it) {
FilterScreenSideEffect.CloseScreen -> navigator.navigateUp()
}
}
FilterScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ import net.mullvad.mullvadvpn.compose.util.accountTokenVisualTransformation
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.lib.theme.color.AlphaTopBar
import net.mullvad.mullvadvpn.util.CollectSideEffectWithLifecycle
import net.mullvad.mullvadvpn.viewmodel.LoginUiSideEffect
import net.mullvad.mullvadvpn.viewmodel.LoginViewModel
import org.koin.androidx.compose.koinViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package net.mullvad.mullvadvpn.compose.screen
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.semantics
Expand All @@ -14,14 +13,14 @@ import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.navigation.popBackStack
import com.ramcosta.composedestinations.rememberNavHostEngine
import com.ramcosta.composedestinations.utils.destination
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import net.mullvad.mullvadvpn.compose.NavGraphs
import net.mullvad.mullvadvpn.compose.destinations.ChangelogDestination
import net.mullvad.mullvadvpn.compose.destinations.ConnectDestination
import net.mullvad.mullvadvpn.compose.destinations.NoDaemonScreenDestination
import net.mullvad.mullvadvpn.compose.destinations.OutOfTimeDestination
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.viewmodel.ChangelogViewModel
import net.mullvad.mullvadvpn.viewmodel.DaemonScreenEvent
import net.mullvad.mullvadvpn.viewmodel.NoDaemonViewModel
Expand Down Expand Up @@ -50,28 +49,23 @@ fun MullvadApp() {
)

// Globally handle daemon dropped connection with NoDaemonScreen
LaunchedEffect(Unit) {
serviceVm.uiSideEffect.collect {
when (it) {
DaemonScreenEvent.Show ->
navController.navigate(NoDaemonScreenDestination) { launchSingleTop = true }
DaemonScreenEvent.Remove ->
navController.popBackStack(NoDaemonScreenDestination, true)
}
LaunchedEffectCollect(serviceVm.uiSideEffect) {
when (it) {
DaemonScreenEvent.Show ->
navController.navigate(NoDaemonScreenDestination) { launchSingleTop = true }
DaemonScreenEvent.Remove -> navController.popBackStack(NoDaemonScreenDestination, true)
}
}

// Globally show the changelog
val changeLogsViewModel = koinViewModel<ChangelogViewModel>()
LaunchedEffect(Unit) {
changeLogsViewModel.uiSideEffect.collect {
LaunchedEffectCollect(changeLogsViewModel.uiSideEffect) {

// Wait until we are in an acceptable destination
navController.currentBackStackEntryFlow
.map { it.destination() }
.first { it in changeLogDestinations }
// Wait until we are in an acceptable destination
navController.currentBackStackEntryFlow
.map { it.destination() }
.first { it in changeLogDestinations }

navController.navigate(ChangelogDestination(it).route)
}
navController.navigate(ChangelogDestination(it).route)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -45,6 +44,7 @@ import net.mullvad.mullvadvpn.compose.extensions.createOpenAccountPageHook
import net.mullvad.mullvadvpn.compose.state.OutOfTimeUiState
import net.mullvad.mullvadvpn.compose.test.OUT_OF_TIME_SCREEN_TITLE_TEST_TAG
import net.mullvad.mullvadvpn.compose.transitions.HomeTransition
import net.mullvad.mullvadvpn.compose.util.LaunchedEffectCollect
import net.mullvad.mullvadvpn.lib.payment.model.ProductId
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
Expand Down Expand Up @@ -134,18 +134,15 @@ fun OutOfTime(
}

val openAccountPage = LocalUriHandler.current.createOpenAccountPageHook()
LaunchedEffect(Unit) {
vm.uiSideEffect.collect { uiSideEffect ->
when (uiSideEffect) {
is OutOfTimeViewModel.UiSideEffect.OpenAccountView ->
openAccountPage(uiSideEffect.token)
OutOfTimeViewModel.UiSideEffect.OpenConnectScreen -> {
navigator.navigate(ConnectDestination) {
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
LaunchedEffectCollect(vm.uiSideEffect) { uiSideEffect ->
when (uiSideEffect) {
is OutOfTimeViewModel.UiSideEffect.OpenAccountView ->
openAccountPage(uiSideEffect.token)
OutOfTimeViewModel.UiSideEffect.OpenConnectScreen ->
navigator.navigate(ConnectDestination) {
launchSingleTop = true
popUpTo(NavGraphs.root) { inclusive = true }
}
}
}
}

Expand Down
Loading

0 comments on commit a473e46

Please sign in to comment.