Skip to content

Commit

Permalink
Add dialog to delete account in AccountTab
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinokuni committed Feb 19, 2024
1 parent 5b74480 commit 13557c0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 }
}
Expand All @@ -46,5 +49,10 @@ class AccountViewModel(

data class AccountState(
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
}

0 comments on commit 13557c0

Please sign in to comment.