make it possible to rename a wallet

sorry for being lazy for some days
This commit is contained in:
Minecon724 2024-08-15 16:25:10 +02:00
parent 88095d1edb
commit 7f9eccba34
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 95 additions and 1 deletions

View file

@ -111,10 +111,17 @@ fun App(
val wallet = walletState val wallet = walletState
var actionsVisible by rememberSaveable { mutableStateOf(false) } var actionsVisible by rememberSaveable { mutableStateOf(false) }
var renameVisible by rememberSaveable { mutableStateOf(false) }
if (actionsVisible) { if (actionsVisible) {
ActionsDialog( ActionsDialog(
onDismiss = { actionsVisible = false }, onRename = {
actionsVisible = false
renameVisible = true
},
onDismiss = {
actionsVisible = false
},
onDelete = { onDelete = {
viewModel.delete() viewModel.delete()
finish() finish()
@ -122,6 +129,18 @@ fun App(
) )
} }
if (renameVisible) {
RenameDialog(
name = wallet!!.label,
onDismiss = { renameVisible = false },
onRename = {
viewModel.rename(it)
wallet.label = it
renameVisible = false
}
)
}
if (wallet != null) { if (wallet != null) {
Column( Column(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
@ -288,8 +307,68 @@ fun TransactionDialog(
} }
} }
@Composable
fun RenameDialog(
name: String,
onDismiss: () -> Unit,
onRename: (String) -> Unit
) {
var value by rememberSaveable { mutableStateOf(name) }
val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
Dialog(
onDismissRequest = {
onDismiss()
}
) {
Card {
Column(
modifier = Modifier.padding(16.dp)
) {
Row {
TextField(
value = value,
onValueChange = { value = it },
modifier = Modifier
.fillMaxWidth()
.weight(1f)
.focusRequester(focusRequester),
supportingText = { Text("New name") },
singleLine = true
)
}
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End
) {
TextButton(
onClick = {
value = name
onDismiss()
}
) {
Text(text = "Cancel")
}
TextButton(
onClick = {
onRename(value)
}
) {
Text("Rename")
}
}
}
}
}
}
@Composable @Composable
fun ActionsDialog( fun ActionsDialog(
onRename: () -> Unit,
onDismiss: () -> Unit, onDismiss: () -> Unit,
onDelete: () -> Unit onDelete: () -> Unit
) { ) {
@ -305,6 +384,9 @@ fun ActionsDialog(
Column( Column(
modifier = Modifier.padding(16.dp) modifier = Modifier.padding(16.dp)
) { ) {
TextButton(onClick = onRename) {
Text("Rename wallet")
}
TextButton(onClick = onDelete) { TextButton(onClick = onDelete) {
Text("Delete wallet") Text("Delete wallet")
} }

View file

@ -8,6 +8,7 @@ import eu.m724.coincounter.data.entity.Wallet
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import kotlin.properties.Delegates import kotlin.properties.Delegates
@ -33,6 +34,17 @@ class WalletViewModel @Inject constructor(
} }
} }
fun rename(label: String) {
wallet = wallet.map {
it.copy(label = label)
}
viewModelScope.launch(Dispatchers.IO) {
val wallet = wallet.first()
repository.updateWallet(wallet)
}
}
fun transact(value: Int, label: String?) { fun transact(value: Int, label: String?) {
val transaction = Transaction( val transaction = Transaction(
walletId = walletId, walletId = walletId,