From 13557c00e64cd89c03e5176bd3948040a17c1e21 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Mon, 19 Feb 2024 13:27:43 +0100 Subject: [PATCH] Add dialog to delete account in AccountTab --- .../app/compose/account/AccountTab.kt | 27 ++++++++++++++++++- .../app/compose/account/AccountViewModel.kt | 16 ++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt b/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt index 1d9404b8..dd58c8c3 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt @@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.AccountCircle +import androidx.compose.material.icons.filled.Delete import androidx.compose.material.icons.filled.MoreVert import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.FloatingActionButton @@ -34,6 +35,7 @@ import cafe.adriel.voyager.navigator.tab.TabOptions import com.readrops.app.compose.R import com.readrops.app.compose.account.selection.AccountSelectionScreen import com.readrops.app.compose.util.components.SelectableIconText +import com.readrops.app.compose.util.components.TwoChoicesDialog import com.readrops.app.compose.util.theme.LargeSpacer import com.readrops.app.compose.util.theme.MediumSpacer import com.readrops.app.compose.util.theme.spacing @@ -61,6 +63,29 @@ object AccountTab : Tab { navigator.replaceAll(AccountSelectionScreen()) } + when (state.dialog) { + DialogState.DeleteAccount -> { + TwoChoicesDialog( + title = stringResource(R.string.delete_account), + text = stringResource(R.string.delete_account_question), + icon = rememberVectorPainter(image = Icons.Default.Delete), + confirmText = stringResource(R.string.delete), + dismissText = stringResource(R.string.cancel), + onDismiss = { viewModel.closeDialog() }, + onConfirm = { + viewModel.closeDialog() + viewModel.deleteAccount() + } + ) + } + + DialogState.NewAccount -> { + + } + + else -> {} + } + Scaffold( topBar = { TopAppBar( @@ -136,7 +161,7 @@ object AccountTab : Tab { padding = MaterialTheme.spacing.mediumSpacing, color = MaterialTheme.colorScheme.error, tint = MaterialTheme.colorScheme.error, - onClick = { /*viewModel.deleteAccount()*/ } + onClick = { viewModel.openDialog(DialogState.DeleteAccount) } ) } } diff --git a/appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt b/appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt index 084afe1b..612c9cc6 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt @@ -13,7 +13,7 @@ import kotlinx.coroutines.launch class AccountViewModel( private val database: Database -) : TabViewModel(database) { +) : TabViewModel(database) { private val _closeHome = MutableStateFlow(false) val closeHome = _closeHome.asStateFlow() @@ -33,11 +33,14 @@ class AccountViewModel( } } + fun openDialog(dialog: DialogState) = _accountState.update { it.copy(dialog = dialog) } + + fun closeDialog() = _accountState.update { it.copy(dialog = null) } fun deleteAccount() { viewModelScope.launch(Dispatchers.IO) { database.newAccountDao() - .deleteAllAccounts() + .delete(currentAccount!!) _closeHome.update { true } } @@ -46,5 +49,10 @@ class AccountViewModel( data class AccountState( val account: Account = Account(accountName = "account", accountType = AccountType.LOCAL), - val dialog: Unit = Unit, -) \ No newline at end of file + val dialog: DialogState? = null, +) + +sealed interface DialogState { + object DeleteAccount : DialogState + object NewAccount : DialogState +} \ No newline at end of file