From 7f9eccba343fad24329ea58240b4c36cc6c89ca8 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Thu, 15 Aug 2024 16:25:10 +0200 Subject: [PATCH] make it possible to rename a wallet sorry for being lazy for some days --- .../eu/m724/coincounter/WalletActivity.kt | 84 ++++++++++++++++++- .../eu/m724/coincounter/WalletViewModel.kt | 12 +++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/m724/coincounter/WalletActivity.kt b/app/src/main/java/eu/m724/coincounter/WalletActivity.kt index bc42dbe..864fb6f 100644 --- a/app/src/main/java/eu/m724/coincounter/WalletActivity.kt +++ b/app/src/main/java/eu/m724/coincounter/WalletActivity.kt @@ -111,10 +111,17 @@ fun App( val wallet = walletState var actionsVisible by rememberSaveable { mutableStateOf(false) } + var renameVisible by rememberSaveable { mutableStateOf(false) } if (actionsVisible) { ActionsDialog( - onDismiss = { actionsVisible = false }, + onRename = { + actionsVisible = false + renameVisible = true + }, + onDismiss = { + actionsVisible = false + }, onDelete = { viewModel.delete() 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) { Column( 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 fun ActionsDialog( + onRename: () -> Unit, onDismiss: () -> Unit, onDelete: () -> Unit ) { @@ -305,6 +384,9 @@ fun ActionsDialog( Column( modifier = Modifier.padding(16.dp) ) { + TextButton(onClick = onRename) { + Text("Rename wallet") + } TextButton(onClick = onDelete) { Text("Delete wallet") } diff --git a/app/src/main/java/eu/m724/coincounter/WalletViewModel.kt b/app/src/main/java/eu/m724/coincounter/WalletViewModel.kt index 9e17164..d691a71 100644 --- a/app/src/main/java/eu/m724/coincounter/WalletViewModel.kt +++ b/app/src/main/java/eu/m724/coincounter/WalletViewModel.kt @@ -8,6 +8,7 @@ import eu.m724.coincounter.data.entity.Wallet import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import javax.inject.Inject 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?) { val transaction = Transaction( walletId = walletId,