Skip to content

Commit

Permalink
Add dropUnlessResumed to prevent duplicate navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jun 14, 2024
1 parent e3947f5 commit 001ef93
Show file tree
Hide file tree
Showing 15 changed files with 2,172 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
Expand Down Expand Up @@ -86,7 +87,7 @@ fun CreateCustomList(
state = state,
createCustomList = vm::createCustomList,
onInputChanged = vm::clearError,
onDismiss = backNavigator::navigateBack
onDismiss = dropUnlessResumed { backNavigator.navigateBack() }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
Expand Down Expand Up @@ -69,7 +70,7 @@ fun DeleteCustomList(
state = state.value,
name = name,
onDelete = viewModel::deleteCustomList,
onBack = navigator::navigateBack
onBack = dropUnlessResumed { navigator.navigateBack() }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
Expand All @@ -18,18 +19,18 @@ import net.mullvad.mullvadvpn.compose.button.PrimaryButton
@Composable
fun DiscardChangesDialog(resultBackNavigator: ResultBackNavigator<Boolean>) {
AlertDialog(
onDismissRequest = resultBackNavigator::navigateBack,
onDismissRequest = dropUnlessResumed { resultBackNavigator.navigateBack() },
title = { Text(text = stringResource(id = R.string.discard_changes)) },
dismissButton = {
PrimaryButton(
modifier = Modifier.focusRequester(FocusRequester()),
onClick = resultBackNavigator::navigateBack,
onClick = dropUnlessResumed {resultBackNavigator.navigateBack()},
text = stringResource(id = R.string.cancel)
)
},
confirmButton = {
PrimaryButton(
onClick = { resultBackNavigator.navigateBack(result = true) },
onClick = dropUnlessResumed { resultBackNavigator.navigateBack(result = true) },
text = stringResource(id = R.string.discard)
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
Expand Down Expand Up @@ -75,7 +76,7 @@ fun DnsDialog(
viewModel::onDnsInputChange,
onSaveDnsClick = viewModel::onSaveDnsClick,
onRemoveDnsClick = viewModel::onRemoveDnsClick,
onDismiss = { resultNavigator.navigateBack(result = DnsDialogResult.Cancel) }
onDismiss = dropUnlessResumed { resultNavigator.navigateBack(result = DnsDialogResult.Cancel) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
Expand Down Expand Up @@ -63,7 +64,7 @@ fun EditCustomListName(
state = state,
updateName = vm::updateCustomListName,
onInputChanged = vm::clearError,
onDismiss = backNavigator::navigateBack
onDismiss = dropUnlessResumed { backNavigator.navigateBack() }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.EmptyResultBackNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
Expand Down Expand Up @@ -57,7 +58,7 @@ fun MtuDialog(mtuInitial: Mtu?, navigator: ResultBackNavigator<Boolean>) {
onInputChanged = viewModel::onInputChanged,
onSaveMtu = viewModel::onSaveClick,
onResetMtu = viewModel::onRestoreClick,
onDismiss = { navigator.navigateBack(true) }
onDismiss = dropUnlessResumed { navigator.navigateBack() }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.EmptyResultBackNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
Expand Down Expand Up @@ -42,7 +43,7 @@ private fun PreviewRemoveDeviceConfirmationDialog(
@Composable
fun RemoveDeviceConfirmationDialog(navigator: ResultBackNavigator<DeviceId>, device: Device) {
AlertDialog(
onDismissRequest = navigator::navigateBack,
onDismissRequest = dropUnlessResumed { navigator.navigateBack() },
icon = {
Icon(
modifier = Modifier.fillMaxWidth().height(Dimens.dialogIconHeight),
Expand All @@ -62,14 +63,14 @@ fun RemoveDeviceConfirmationDialog(navigator: ResultBackNavigator<DeviceId>, dev
},
dismissButton = {
NegativeButton(
onClick = { navigator.navigateBack(result = device.id) },
onClick = dropUnlessResumed { navigator.navigateBack(result = device.id) },
text = stringResource(id = R.string.confirm_removal)
)
},
confirmButton = {
PrimaryButton(
modifier = Modifier.focusRequester(FocusRequester()),
onClick = { navigator.navigateBack() },
onClick = dropUnlessResumed { navigator.navigateBack() },
text = stringResource(id = R.string.back)
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.EmptyResultBackNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
Expand All @@ -32,7 +33,7 @@ private fun PreviewReportProblemNoEmailDialog() {
@Composable
fun ReportProblemNoEmailDialog(resultBackNavigator: ResultBackNavigator<Boolean>) {
AlertDialog(
onDismissRequest = resultBackNavigator::navigateBack,
onDismissRequest = dropUnlessResumed {resultBackNavigator.navigateBack() },
icon = {
Icon(
painter = painterResource(id = R.drawable.icon_alert),
Expand All @@ -52,14 +53,14 @@ fun ReportProblemNoEmailDialog(resultBackNavigator: ResultBackNavigator<Boolean>
dismissButton = {
NegativeButton(
modifier = Modifier.fillMaxWidth(),
onClick = { resultBackNavigator.navigateBack(result = true) },
onClick = dropUnlessResumed { resultBackNavigator.navigateBack(result = true) },
text = stringResource(id = R.string.send_anyway)
)
},
confirmButton = {
PrimaryButton(
modifier = Modifier.fillMaxWidth(),
onClick = resultBackNavigator::navigateBack,
onClick = dropUnlessResumed { resultBackNavigator.navigateBack() },
text = stringResource(id = R.string.back)
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
Expand Down Expand Up @@ -43,7 +44,7 @@ fun ResetServerIpOverridesConfirmation(resultBackNavigator: ResultBackNavigator<
}
ResetServerIpOverridesConfirmationDialog(
onClearAllOverrides = vm::clearAllOverrides,
resultBackNavigator::navigateBack
dropUnlessResumed { resultBackNavigator.navigateBack() }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.EmptyResultBackNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.NavResult
Expand Down Expand Up @@ -117,7 +118,7 @@ fun EditCustomList(
launchSingleTop = true
}
},
onBackClick = backNavigator::navigateBack
onBackClick = dropUnlessResumed { backNavigator.navigateBack()}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.result.ResultBackNavigator
import net.mullvad.mullvadvpn.R
Expand All @@ -40,7 +41,7 @@ fun ImportOverridesByText(
resultNavigator: ResultBackNavigator<String>,
) {
ImportOverridesByTextScreen(
onNavigateBack = resultNavigator::navigateBack,
onNavigateBack = dropUnlessResumed { resultNavigator.navigateBack() },
onImportClicked = { resultNavigator.navigateBack(result = it) }
)
}
Expand All @@ -67,7 +68,7 @@ fun ImportOverridesByTextScreen(
colors =
ButtonDefaults.textButtonColors()
.copy(contentColor = MaterialTheme.colorScheme.onPrimary),
onClick = { onImportClicked(text) }
onClick = dropUnlessResumed { onImportClicked(text) }
) {
Text(
text = stringResource(R.string.import_overrides_import),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.NavResult
Expand Down Expand Up @@ -149,9 +150,10 @@ fun SelectLocation(
val snackbarHostState = remember { SnackbarHostState() }
val context = LocalContext.current

val navigateBack = dropUnlessResumed { backNavigator.navigateBack(result = true)}
LaunchedEffectCollect(vm.uiSideEffect) {
when (it) {
SelectLocationSideEffect.CloseScreen -> backNavigator.navigateBack(result = true, true)
SelectLocationSideEffect.CloseScreen -> navigateBack()
is SelectLocationSideEffect.LocationAddedToCustomList ->
launch {
snackbarHostState.showResultSnackbar(
Expand Down Expand Up @@ -200,8 +202,8 @@ fun SelectLocation(
snackbarHostState = snackbarHostState,
onSelectRelay = vm::selectRelay,
onSearchTermInput = vm::onSearchTermInput,
onBackClick = { backNavigator.navigateBack(true) },
onFilterClick = { navigator.navigate(FilterScreenDestination, true) },
onBackClick = dropUnlessResumed { backNavigator.navigateBack() },
onFilterClick = dropUnlessResumed { navigator.navigate(FilterScreenDestination) },
onCreateCustomList = { relayItem ->
navigator.navigate(CreateCustomListDestination(locationCode = relayItem?.id)) {
launchSingleTop = true
Expand Down
2 changes: 1 addition & 1 deletion android/buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object Versions {
const val appcompat = "1.6.1"
const val coreKtx = "1.12.0"
const val espresso = "3.5.1"
const val lifecycle = "2.7.0"
const val lifecycle = "2.8.2"
const val fragment = "1.6.2"
const val test = "1.5.0"
const val testMonitor = "1.6.1"
Expand Down
Loading

0 comments on commit 001ef93

Please sign in to comment.