category on load shown

This commit is contained in:
Xaver 2025-01-17 10:15:00 +01:00
parent 9f22ce84fd
commit d4116b5a7f
2 changed files with 28 additions and 19 deletions

View File

@ -14,6 +14,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableIntStateOf
@ -43,12 +44,20 @@ fun Edit(
val entry = editViewModel.editUiState.entry val entry = editViewModel.editUiState.entry
val category = editViewModel.categoryUi.category val category = editViewModel.categoryUi.category
val categories by editViewModel.categoryListUiState.categories.collectAsState(initial = emptyList()) 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 selectedIconIndex by remember { mutableStateOf(category.icon) }
var selectedCategory by remember { mutableIntStateOf(category.icon) } var selectedCategoryName by remember { mutableStateOf(category.categoryName) }
var selectedCategory by remember { mutableIntStateOf(category._id) }
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
LaunchedEffect(category) {
selectedIconIndex = category.icon
selectedCategoryName = category.categoryName
selectedCategory = category._id
}
Column( Column(
modifier = modifier modifier = modifier
.fillMaxSize() .fillMaxSize()
@ -62,20 +71,18 @@ fun Edit(
value = entry.name, value = entry.name,
onValueChange = { editViewModel.updateEntry(entry.copy(name = it)) }, onValueChange = { editViewModel.updateEntry(entry.copy(name = it)) },
label = { Text("Name") }, label = { Text("Name") },
modifier = Modifier modifier = Modifier.fillMaxWidth()
.fillMaxWidth()
) )
OutlinedTextField( OutlinedTextField(
value = entry.amount.toString(), value = entry.amount.toString(),
onValueChange = { onValueChange = {
val isValidSpending = it.matches(Regex("^[+-]?\\d*(\\.\\d{0,2})?$")) val isValidSpending = it.matches(Regex("^[+-]?\\d*(\\.\\d{0,2})?$"))
if (isValidSpending){ if (isValidSpending) {
editViewModel.updateEntry(entry.copy(amount = it.toFloat())) editViewModel.updateEntry(entry.copy(amount = it.toFloat()))
} }
}, },
label = { Text("Spending") }, label = { Text("Spending") },
modifier = Modifier modifier = Modifier.fillMaxWidth()
.fillMaxWidth()
) )
ExposedDropdownMenuBox( ExposedDropdownMenuBox(
@ -83,18 +90,18 @@ fun Edit(
onExpandedChange = { expanded = it } onExpandedChange = { expanded = it }
) { ) {
OutlinedTextField( OutlinedTextField(
value = selectedCategoryName, // Show selected icon name value = selectedCategoryName, // Show selected category name
onValueChange = {}, onValueChange = {},
label = { Text("Category") }, label = { Text("Category") },
readOnly = true, readOnly = true,
leadingIcon = { leadingIcon = {
Icon( Icon(
imageVector = icons[selectedIconIndex], // Replace with your desired icon imageVector = icons[selectedIconIndex], // Show selected icon
contentDescription = "Leading Icon" contentDescription = "Leading Icon"
) )
}, },
trailingIcon = { trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) TrailingIcon(expanded = expanded)
}, },
modifier = Modifier modifier = Modifier
.menuAnchor() .menuAnchor()
@ -107,15 +114,15 @@ fun Edit(
) { ) {
categories.forEachIndexed { index, category -> categories.forEachIndexed { index, category ->
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = category.categoryName) }, // Use name from iconNames text = { Text(text = category.categoryName) },
onClick = { 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 expanded = false
selectedCategoryName = category.categoryName
selectedCategory = category._id
}, },
leadingIcon = { leadingIcon = {
androidx.compose.material3.Icon( Icon(
imageVector = icons[category.icon], imageVector = icons[category.icon],
contentDescription = iconNames[category.icon] contentDescription = iconNames[category.icon]
) )
@ -132,7 +139,7 @@ fun Edit(
Column( Column(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally // Center buttons horizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
ButtonDanger( ButtonDanger(
modifier = Modifier modifier = Modifier
@ -145,7 +152,7 @@ fun Edit(
} }
) )
ButtonSuccess( ButtonSuccess(
modifier = Modifier.fillMaxWidth(), // Add spacing between buttons modifier = Modifier.fillMaxWidth(),
text = "Done", text = "Done",
onClick = { onClick = {
editViewModel.saveEntry() editViewModel.saveEntry()
@ -154,4 +161,4 @@ fun Edit(
) )
} }
} }
} }

View File

@ -1,5 +1,6 @@
package at.xaxa.ledger.ui.entry.edit package at.xaxa.ledger.ui.entry.edit
import android.util.Log
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
@ -14,6 +15,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.math.log
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList())) data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
data class CategoryUIState(val category: CategoryEntity = CategoryEntity(0,"",0)) data class CategoryUIState(val category: CategoryEntity = CategoryEntity(0,"",0))