Skip to content

Commit

Permalink
Rename delete confirmation dialog and make more dialogs use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Aug 22, 2024
1 parent a1fad72 commit b98b40d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 133 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ fun DeleteApiAccessMethodConfirmationDialog(
onDelete: () -> Unit = {},
onBack: () -> Unit = {}
) {
DeleteConfirmationDialog(
onDelete = onDelete,
NegativeConfirmationDialog(
onConfirm = onDelete,
onBack = onBack,
message =
stringResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ fun DeleteCustomListConfirmationDialog(
onDelete: () -> Unit = {},
onBack: () -> Unit = {}
) {
DeleteConfirmationDialog(
onDelete = onDelete,
NegativeConfirmationDialog(
onConfirm = onDelete,
onBack = onBack,
message =
stringResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -15,6 +16,8 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.NegativeButton
Expand All @@ -26,7 +29,7 @@ import net.mullvad.mullvadvpn.lib.theme.Dimens
@Composable
private fun PreviewDeleteConfirmationDialog() {
AppTheme {
DeleteConfirmationDialog(
NegativeConfirmationDialog(
message = "Do you want to delete Cookie?",
errorMessage = null,
)
Expand All @@ -37,18 +40,42 @@ private fun PreviewDeleteConfirmationDialog() {
@Composable
private fun PreviewDeleteConfirmationDialogError() {
AppTheme {
DeleteConfirmationDialog(
NegativeConfirmationDialog(
message = "Do you want to delete Cookie?",
errorMessage = "An error occured"
)
}
}

@Composable
fun DeleteConfirmationDialog(
fun NegativeConfirmationDialog(
message: String,
messageStyle: TextStyle? = null,
errorMessage: String?,
onDelete: () -> Unit = {},
confirmationText: String = stringResource(id = R.string.delete),
cancelText: String = stringResource(id = R.string.cancel),
onConfirm: () -> Unit = {},
onBack: () -> Unit = {}
) {
NegativeConfirmationDialog(
message = AnnotatedString(message),
messageStyle = messageStyle,
errorMessage = errorMessage,
confirmationText = confirmationText,
cancelText = cancelText,
onConfirm = onConfirm,
onBack = onBack
)
}

@Composable
fun NegativeConfirmationDialog(
message: AnnotatedString,
messageStyle: TextStyle? = null,
errorMessage: String?,
confirmationText: String = stringResource(id = R.string.delete),
cancelText: String = stringResource(id = R.string.cancel),
onConfirm: () -> Unit = {},
onBack: () -> Unit = {}
) {
AlertDialog(
Expand All @@ -63,7 +90,7 @@ fun DeleteConfirmationDialog(
},
title = {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(text = message)
Text(text = message, style = messageStyle ?: LocalTextStyle.current)
if (errorMessage != null) {
Text(
text = errorMessage,
Expand All @@ -78,12 +105,10 @@ fun DeleteConfirmationDialog(
PrimaryButton(
modifier = Modifier.focusRequester(FocusRequester()),
onClick = onBack,
text = stringResource(id = R.string.cancel)
text = cancelText
)
},
confirmButton = {
NegativeButton(onClick = onDelete, text = stringResource(id = R.string.delete))
},
confirmButton = { NegativeButton(onClick = onConfirm, text = confirmationText) },
containerColor = MaterialTheme.colorScheme.surface
)
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package net.mullvad.mullvadvpn.compose.dialog

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.sp
import androidx.core.text.HtmlCompat
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
import com.ramcosta.composedestinations.result.EmptyResultBackNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.NegativeButton
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.component.HtmlText
import net.mullvad.mullvadvpn.compose.component.textResource
import net.mullvad.mullvadvpn.compose.extensions.toAnnotatedString
import net.mullvad.mullvadvpn.compose.preview.DevicePreviewParameterProvider
import net.mullvad.mullvadvpn.lib.model.Device
import net.mullvad.mullvadvpn.lib.model.DeviceId
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens

@Preview
@Composable
Expand All @@ -42,38 +32,18 @@ private fun PreviewRemoveDeviceConfirmationDialog(
@Destination<RootGraph>(style = DestinationStyle.Dialog::class)
@Composable
fun RemoveDeviceConfirmation(navigator: ResultBackNavigator<DeviceId>, device: Device) {
AlertDialog(
onDismissRequest = dropUnlessResumed { navigator.navigateBack() },
icon = {
Icon(
modifier = Modifier.fillMaxWidth().height(Dimens.dialogIconHeight),
painter = painterResource(id = R.drawable.icon_alert),
contentDescription = stringResource(id = R.string.remove_button),
tint = MaterialTheme.colorScheme.error
)
},
text = {
val htmlFormattedDialogText =
textResource(
id = R.string.max_devices_confirm_removal_description,
device.displayName()
)

HtmlText(htmlFormattedString = htmlFormattedDialogText, textSize = 16.sp.value)
},
dismissButton = {
PrimaryButton(
modifier = Modifier.focusRequester(FocusRequester()),
onClick = dropUnlessResumed { navigator.navigateBack() },
text = stringResource(id = R.string.back)
)
},
confirmButton = {
NegativeButton(
onClick = dropUnlessResumed { navigator.navigateBack(result = device.id) },
text = stringResource(id = R.string.confirm_removal)
)
},
containerColor = MaterialTheme.colorScheme.surface
val htmlFormattedString =
textResource(id = R.string.max_devices_confirm_removal_description, device.displayName())
val message =
HtmlCompat.fromHtml(htmlFormattedString, HtmlCompat.FROM_HTML_MODE_COMPACT)
.toAnnotatedString(boldFontWeight = FontWeight.Bold)
NegativeConfirmationDialog(
message = message,
messageStyle = MaterialTheme.typography.labelLarge,
errorMessage = null,
confirmationText = stringResource(id = R.string.confirm_removal),
cancelText = stringResource(id = R.string.back),
onBack = dropUnlessResumed { navigator.navigateBack() },
onConfirm = dropUnlessResumed { navigator.navigateBack(result = device.id) },
)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package net.mullvad.mullvadvpn.compose.dialog

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.dropUnlessResumed
Expand All @@ -18,10 +11,7 @@ import com.ramcosta.composedestinations.result.EmptyResultBackNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.NegativeButton
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens

@Preview
@Composable
Expand All @@ -32,38 +22,13 @@ private fun PreviewReportProblemNoEmailDialog() {
@Destination<RootGraph>(style = DestinationStyle.Dialog::class)
@Composable
fun ReportProblemNoEmail(resultBackNavigator: ResultBackNavigator<Boolean>) {
AlertDialog(
onDismissRequest = dropUnlessResumed { resultBackNavigator.navigateBack() },
icon = {
Icon(
painter = painterResource(id = R.drawable.icon_alert),
contentDescription = null,
modifier = Modifier.size(Dimens.dialogIconHeight),
tint = MaterialTheme.colorScheme.error
)
},
text = {
Text(
text = stringResource(id = R.string.confirm_no_email),
modifier = Modifier.fillMaxWidth(),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface
)
},
dismissButton = {
PrimaryButton(
modifier = Modifier.fillMaxWidth(),
onClick = dropUnlessResumed { resultBackNavigator.navigateBack() },
text = stringResource(id = R.string.back)
)
},
confirmButton = {
NegativeButton(
modifier = Modifier.fillMaxWidth(),
onClick = dropUnlessResumed { resultBackNavigator.navigateBack(result = true) },
text = stringResource(id = R.string.send_anyway)
)
},
containerColor = MaterialTheme.colorScheme.surface
NegativeConfirmationDialog(
message = stringResource(id = R.string.confirm_no_email),
errorMessage = null,
confirmationText = stringResource(id = R.string.send_anyway),
cancelText = stringResource(id = R.string.back),
messageStyle = MaterialTheme.typography.bodySmall,
onBack = dropUnlessResumed { resultBackNavigator.navigateBack() },
onConfirm = dropUnlessResumed { resultBackNavigator.navigateBack(result = true) },
)
}

0 comments on commit b98b40d

Please sign in to comment.