From 7e66cdc30bdc14aab0acd4e0634c410d1e3d14ca Mon Sep 17 00:00:00 2001 From: Xaver Date: Thu, 16 Jan 2025 15:55:52 +0100 Subject: [PATCH] adding works again --- .../overview/OverviewCategoryViewModel.kt | 1 - .../java/at/xaxa/ledger/ui/entry/add/AddUI.kt | 66 +++++++++++++++---- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/category/overview/OverviewCategoryViewModel.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/category/overview/OverviewCategoryViewModel.kt index 150fa48..5272fa7 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/category/overview/OverviewCategoryViewModel.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/category/overview/OverviewCategoryViewModel.kt @@ -25,7 +25,6 @@ class OverviewCategoryViewModel( private val repository: EntryRepository ) : ViewModel() { var categoryUiState by mutableStateOf(CategoryListUIState()) - var categoryUi by mutableStateOf(CategoryUIState()) private set init { diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/add/AddUI.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/add/AddUI.kt index 2e70593..3c45955 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/add/AddUI.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/add/AddUI.kt @@ -1,10 +1,14 @@ package at.xaxa.ledger.ui.entry.add +import android.util.Log import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Info +import androidx.compose.material3.AlertDialog import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExposedDropdownMenuBox @@ -12,6 +16,7 @@ import androidx.compose.material3.ExposedDropdownMenuDefaults import androidx.compose.material3.Icon import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text +import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue @@ -30,6 +35,7 @@ import at.xaxa.ledger.ui.ButtonSuccess import at.xaxa.ledger.ui.DatePickerDocked import at.xaxa.ledger.ui.category.iconNames import at.xaxa.ledger.ui.category.icons +import kotlin.math.log @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -39,12 +45,41 @@ fun Add( addViewModel: AddViewModel = viewModel(factory = AppViewModelProvider.Factory) ) { var name by remember { mutableStateOf("") } + var categories = addViewModel.categoryUiState.categories.collectAsState(emptyList()).value var spending by remember { mutableStateOf("") } - var selectedDate by remember { mutableLongStateOf(0) } + var selectedDate by remember { mutableLongStateOf(System.currentTimeMillis()) } var expanded by remember { mutableStateOf(false) } var selectedIconIndex by remember { mutableStateOf(0) } // Store index of selected icon + var selectedCategoryName by remember { mutableStateOf("") } // Store index of selected icon var selectedCategory by remember { mutableIntStateOf(-1) } + var parsingError by remember { mutableStateOf(false) } + + if (parsingError) { + AlertDialog( + icon = { + Icon(Icons.Default.Info, contentDescription = "Info Icon") + }, + title = { + Text(text = "Parsing error") + }, + text = { + Text(text = "One or more fields contain invalid input. Please review your entries and try again.") + }, + onDismissRequest = { + parsingError = false + }, + confirmButton = { + TextButton( + onClick = { + parsingError = false + } + ) { + Text("Okay") + } + } + ) + } Column( modifier = modifier @@ -76,9 +111,9 @@ fun Add( onExpandedChange = { expanded = it } ) { OutlinedTextField( - value = iconNames[selectedIconIndex], // Show selected icon name + value = selectedCategoryName, // Show selected icon name onValueChange = {}, - label = { Text("Icon") }, + label = { Text("Category") }, readOnly = true, leadingIcon = { Icon( @@ -98,17 +133,19 @@ fun Add( expanded = expanded, onDismissRequest = { expanded = false } ) { - icons.forEachIndexed { index, icon -> + categories.forEachIndexed { index, category -> DropdownMenuItem( - text = { Text(text = iconNames[index]) }, // Use name from iconNames + text = { Text(text = category.categoryName) }, // Use name from iconNames onClick = { - selectedIconIndex = index // Update selected index + selectedIconIndex = category.icon // Update selected index expanded = false + selectedCategoryName = category.categoryName + selectedCategory = category._id }, leadingIcon = { androidx.compose.material3.Icon( - imageVector = icon, - contentDescription = iconNames[index] + imageVector = icons[category.icon], + contentDescription = iconNames[category.icon] ) } ) @@ -116,8 +153,8 @@ fun Add( } } - DatePickerDocked{ - dateMilis -> selectedDate = dateMilis + DatePickerDocked { dateMilis -> + selectedDate = dateMilis } } @@ -130,7 +167,12 @@ fun Add( modifier = Modifier, "Add Transaction", onClick = { - if (name.isNotBlank() && spending.toFloat() != 0f && selectedDate != 0L && selectedCategory != -1) { + val isValidSpending = spending.matches(Regex("^[+-]?\\d*(\\.\\d+)?$")) + Log.w("xaxaxa", name.isNotBlank().toString() ) + Log.w("xaxaxa", isValidSpending.toString() ) + Log.w("xaxaxa", (selectedDate != 0L).toString() ) + Log.w("xaxaxa", (selectedCategory != -1).toString() ) + if (name.isNotBlank() && isValidSpending && selectedDate != 0L && selectedCategory != -1) { val newEntry = Entry( id = 0, name = name, @@ -140,6 +182,8 @@ fun Add( ) addViewModel.addEntryToDB(newEntry) onCardClick() + } else { + parsingError = true; } } )