From 99db47a16c19f31eb89c2b34d903093f3017e349 Mon Sep 17 00:00:00 2001 From: sim Date: Tue, 19 Nov 2024 13:25:22 +0000 Subject: [PATCH] Add confirmation dialog before logging out --- .../nextpush/activities/ui/AppBarUi.kt | 77 ++++++++++++++++--- app/src/main/res/values/strings.xml | 2 + 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt index 91252dc..3e0222e 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt @@ -2,6 +2,7 @@ package org.unifiedpush.distributor.nextpush.activities.ui import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.MoreVert +import androidx.compose.material3.AlertDialog import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ExperimentalMaterial3Api @@ -9,6 +10,7 @@ import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text +import androidx.compose.material3.TextButton import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable @@ -28,6 +30,8 @@ import org.unifiedpush.distributor.nextpush.activities.publishAction fun AppBarUi(viewModel: ViewModel) { var expanded by remember { mutableStateOf(false) } var showNotificationDialog by remember { mutableStateOf(false) } + var showLogoutDialog by remember { mutableStateOf(false) } + TopAppBar( colors = TopAppBarDefaults .topAppBarColors( @@ -52,7 +56,14 @@ fun AppBarUi(viewModel: ViewModel) { } Dropdown( expanded, - actionHandler = { a -> viewModel.publishAction(a) }, + onRestart = { + viewModel.publishAction(AppAction(AppAction.Type.RestartService, null)) + expanded = false + }, + onLogout = { + showLogoutDialog = true + expanded = false + }, onDismiss = { expanded = false }, @@ -78,30 +89,41 @@ fun AppBarUi(viewModel: ViewModel) { } ) } + if (showLogoutDialog) { + LogoutConfirmationDialog( + onDismissRequest = { + showLogoutDialog = false + }, + onConfirmation = { + viewModel.publishAction(AppAction(AppAction.Type.Logout, null)) + showLogoutDialog = false + } + ) + } } ) } @Composable -fun Dropdown(expanded: Boolean, actionHandler: (AppAction) -> Unit, onDismiss: () -> Unit, onNewChannel: () -> Unit) { +fun Dropdown( + expanded: Boolean, + onRestart: () -> Unit, + onLogout: () -> Unit, + onDismiss: () -> Unit, + onNewChannel: () -> Unit +) { DropdownMenu( expanded = expanded, onDismissRequest = onDismiss ) { DropdownMenuItem( - onClick = { - actionHandler(AppAction(AppAction.Type.RestartService, null)) - onDismiss() - }, + onClick = onRestart, text = { Text(stringResource(R.string.app_dropdown_restart)) } ) DropdownMenuItem( - onClick = { - actionHandler(AppAction(AppAction.Type.Logout, null)) - onDismiss() - }, + onClick = onLogout, text = { Text( stringResource(R.string.app_dropdown_logout) @@ -122,6 +144,41 @@ fun Dropdown(expanded: Boolean, actionHandler: (AppAction) -> Unit, onDismiss: ( } } + +@Preview +@Composable +fun LogoutConfirmationDialog(onDismissRequest: () -> Unit = {}, onConfirmation: () -> Unit = {}) { + AlertDialog( + title = { + Text(stringResource(R.string.dialog_logout_title)) + }, + text = { Text(stringResource(R.string.dialog_logout_content)) }, + onDismissRequest = { + onDismissRequest() + }, + confirmButton = { + TextButton( + onClick = { + onConfirmation() + } + ) { + Text(stringResource(android.R.string.ok)) + } + }, + dismissButton = { + TextButton( + onClick = { + onDismissRequest() + } + ) { + Text( + stringResource(android.R.string.cancel) + ) + } + } + ) +} + @Preview @Composable fun AppBarPreview() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9371da0..1e77be9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -51,4 +51,6 @@ Unregister selection Permissions This application requires notifications permission to work. + Logout + Are you sure you want to log out?