Add dialog to delete account in AccountTab
This commit is contained in:
parent
5b74480398
commit
13557c00e6
@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.padding
|
|||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.AccountCircle
|
import androidx.compose.material.icons.filled.AccountCircle
|
||||||
|
import androidx.compose.material.icons.filled.Delete
|
||||||
import androidx.compose.material.icons.filled.MoreVert
|
import androidx.compose.material.icons.filled.MoreVert
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.FloatingActionButton
|
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.R
|
||||||
import com.readrops.app.compose.account.selection.AccountSelectionScreen
|
import com.readrops.app.compose.account.selection.AccountSelectionScreen
|
||||||
import com.readrops.app.compose.util.components.SelectableIconText
|
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.LargeSpacer
|
||||||
import com.readrops.app.compose.util.theme.MediumSpacer
|
import com.readrops.app.compose.util.theme.MediumSpacer
|
||||||
import com.readrops.app.compose.util.theme.spacing
|
import com.readrops.app.compose.util.theme.spacing
|
||||||
@ -61,6 +63,29 @@ object AccountTab : Tab {
|
|||||||
navigator.replaceAll(AccountSelectionScreen())
|
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(
|
Scaffold(
|
||||||
topBar = {
|
topBar = {
|
||||||
TopAppBar(
|
TopAppBar(
|
||||||
@ -136,7 +161,7 @@ object AccountTab : Tab {
|
|||||||
padding = MaterialTheme.spacing.mediumSpacing,
|
padding = MaterialTheme.spacing.mediumSpacing,
|
||||||
color = MaterialTheme.colorScheme.error,
|
color = MaterialTheme.colorScheme.error,
|
||||||
tint = MaterialTheme.colorScheme.error,
|
tint = MaterialTheme.colorScheme.error,
|
||||||
onClick = { /*viewModel.deleteAccount()*/ }
|
onClick = { viewModel.openDialog(DialogState.DeleteAccount) }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import kotlinx.coroutines.launch
|
|||||||
|
|
||||||
class AccountViewModel(
|
class AccountViewModel(
|
||||||
private val database: Database
|
private val database: Database
|
||||||
) : TabViewModel(database) {
|
) : TabViewModel(database) {
|
||||||
|
|
||||||
private val _closeHome = MutableStateFlow(false)
|
private val _closeHome = MutableStateFlow(false)
|
||||||
val closeHome = _closeHome.asStateFlow()
|
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() {
|
fun deleteAccount() {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
database.newAccountDao()
|
database.newAccountDao()
|
||||||
.deleteAllAccounts()
|
.delete(currentAccount!!)
|
||||||
|
|
||||||
_closeHome.update { true }
|
_closeHome.update { true }
|
||||||
}
|
}
|
||||||
@ -46,5 +49,10 @@ class AccountViewModel(
|
|||||||
|
|
||||||
data class AccountState(
|
data class AccountState(
|
||||||
val account: Account = Account(accountName = "account", accountType = AccountType.LOCAL),
|
val account: Account = Account(accountName = "account", accountType = AccountType.LOCAL),
|
||||||
val dialog: Unit = Unit,
|
val dialog: DialogState? = null,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
sealed interface DialogState {
|
||||||
|
object DeleteAccount : DialogState
|
||||||
|
object NewAccount : DialogState
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user