diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditUI.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditUI.kt index d9e4d0b..1f83206 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditUI.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditUI.kt @@ -14,6 +14,7 @@ import androidx.compose.material3.Icon import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf @@ -43,12 +44,20 @@ fun Edit( val entry = editViewModel.editUiState.entry val category = editViewModel.categoryUi.category val categories by editViewModel.categoryListUiState.categories.collectAsState(initial = emptyList()) - var selectedIconIndex by remember { mutableStateOf(category.icon) } // Store index of selected icon - var selectedCategoryName by remember { mutableStateOf(category.categoryName) } // Store index of selected icon - var selectedCategory by remember { mutableIntStateOf(category.icon) } + + var selectedIconIndex by remember { mutableStateOf(category.icon) } + var selectedCategoryName by remember { mutableStateOf(category.categoryName) } + var selectedCategory by remember { mutableIntStateOf(category._id) } var expanded by remember { mutableStateOf(false) } + LaunchedEffect(category) { + selectedIconIndex = category.icon + selectedCategoryName = category.categoryName + selectedCategory = category._id + } + + Column( modifier = modifier .fillMaxSize() @@ -62,20 +71,18 @@ fun Edit( value = entry.name, onValueChange = { editViewModel.updateEntry(entry.copy(name = it)) }, label = { Text("Name") }, - modifier = Modifier - .fillMaxWidth() + modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = entry.amount.toString(), onValueChange = { val isValidSpending = it.matches(Regex("^[+-]?\\d*(\\.\\d{0,2})?$")) - if (isValidSpending){ + if (isValidSpending) { editViewModel.updateEntry(entry.copy(amount = it.toFloat())) } }, label = { Text("Spending") }, - modifier = Modifier - .fillMaxWidth() + modifier = Modifier.fillMaxWidth() ) ExposedDropdownMenuBox( @@ -83,18 +90,18 @@ fun Edit( onExpandedChange = { expanded = it } ) { OutlinedTextField( - value = selectedCategoryName, // Show selected icon name + value = selectedCategoryName, // Show selected category name onValueChange = {}, label = { Text("Category") }, readOnly = true, leadingIcon = { Icon( - imageVector = icons[selectedIconIndex], // Replace with your desired icon + imageVector = icons[selectedIconIndex], // Show selected icon contentDescription = "Leading Icon" ) }, trailingIcon = { - ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) + TrailingIcon(expanded = expanded) }, modifier = Modifier .menuAnchor() @@ -107,15 +114,15 @@ fun Edit( ) { categories.forEachIndexed { index, category -> DropdownMenuItem( - text = { Text(text = category.categoryName) }, // Use name from iconNames + text = { Text(text = category.categoryName) }, onClick = { - selectedIconIndex = category.icon // Update selected index + selectedIconIndex = category.icon // Update selected icon index + selectedCategoryName = category.categoryName // Update selected category name + selectedCategory = category._id // Update selected category ID expanded = false - selectedCategoryName = category.categoryName - selectedCategory = category._id }, leadingIcon = { - androidx.compose.material3.Icon( + Icon( imageVector = icons[category.icon], contentDescription = iconNames[category.icon] ) @@ -132,7 +139,7 @@ fun Edit( Column( modifier = Modifier.fillMaxWidth(), - horizontalAlignment = Alignment.CenterHorizontally // Center buttons horizontally + horizontalAlignment = Alignment.CenterHorizontally ) { ButtonDanger( modifier = Modifier @@ -145,7 +152,7 @@ fun Edit( } ) ButtonSuccess( - modifier = Modifier.fillMaxWidth(), // Add spacing between buttons + modifier = Modifier.fillMaxWidth(), text = "Done", onClick = { editViewModel.saveEntry() @@ -154,4 +161,4 @@ fun Edit( ) } } -} +} \ No newline at end of file diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditViewModel.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditViewModel.kt index a805287..c4fb7d6 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditViewModel.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditViewModel.kt @@ -1,5 +1,6 @@ package at.xaxa.ledger.ui.entry.edit +import android.util.Log import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -14,6 +15,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kotlin.math.log data class CategoryListUIState(val categories: Flow> = flowOf(emptyList())) data class CategoryUIState(val category: CategoryEntity = CategoryEntity(0,"",0))