ui
This commit is contained in:
parent
ac7e35d54f
commit
120a24bd3e
@ -1,5 +1,6 @@
|
||||
package at.xaxa.ledger.data
|
||||
|
||||
import android.util.Log
|
||||
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||
import at.xaxa.ledger.data.db.Entry.EntryEntity
|
||||
import at.xaxa.ledger.data.db.LedgerDao
|
||||
@ -28,6 +29,7 @@ class EntryRepository(private val ledgerDao: LedgerDao){
|
||||
)
|
||||
}
|
||||
suspend fun findCategoryById(id: Int): CategoryEntity {
|
||||
Log.w("xaver", id.toString())
|
||||
val category = ledgerDao.findCategoryById(id)
|
||||
return CategoryEntity(
|
||||
category._id, category.categoryName, category.icon
|
||||
|
@ -55,7 +55,7 @@ fun LedgerApp(modifier: Modifier = Modifier){
|
||||
|
||||
composable(AppRoutes.Add.route) {
|
||||
Add(onCardClick = {
|
||||
navController.navigate("home")
|
||||
navController.popBackStack()
|
||||
})
|
||||
}
|
||||
composable(
|
||||
@ -68,7 +68,7 @@ fun LedgerApp(modifier: Modifier = Modifier){
|
||||
Edit(
|
||||
modifier = Modifier,
|
||||
onCardClick = {
|
||||
navController.navigate("home")
|
||||
navController.popBackStack()
|
||||
}
|
||||
)
|
||||
}
|
||||
@ -82,14 +82,14 @@ fun LedgerApp(modifier: Modifier = Modifier){
|
||||
EditCategory(
|
||||
modifier = Modifier,
|
||||
onButtonClick = {
|
||||
navController.navigate("category")
|
||||
navController.popBackStack()
|
||||
}
|
||||
)
|
||||
}
|
||||
composable(AppRoutes.AddCategory.route){
|
||||
AddCategory(
|
||||
onButtonClick = {
|
||||
navController.navigate("home")
|
||||
navController.popBackStack()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -10,12 +10,11 @@ import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.ExposedDropdownMenuBox
|
||||
import androidx.compose.material3.ExposedDropdownMenuDefaults
|
||||
import androidx.compose.material3.ExposedDropdownMenuDefaults.TrailingIcon
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
@ -25,13 +24,9 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import at.xaxa.ledger.data.Entry
|
||||
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||
import at.xaxa.ledger.ui.AppViewModelProvider
|
||||
import at.xaxa.ledger.ui.ButtonDanger
|
||||
import at.xaxa.ledger.ui.ButtonSuccess
|
||||
import at.xaxa.ledger.ui.DatePickerDocked
|
||||
import at.xaxa.ledger.ui.category.CategoryViewModel
|
||||
import at.xaxa.ledger.ui.category.iconNames
|
||||
import at.xaxa.ledger.ui.category.icons
|
||||
|
||||
@ -44,86 +39,106 @@ fun EditCategory(
|
||||
modifier: Modifier = Modifier,
|
||||
editCategoryViewModel: EditCategoryViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
||||
) {
|
||||
|
||||
var expanded by remember { mutableStateOf(false) } // Controls dropdown visibility
|
||||
val category = editCategoryViewModel.categoryUi.category
|
||||
var expanded by remember { mutableStateOf(false) } // Controls dropdown visibility
|
||||
var selectedIconIndex by remember { mutableIntStateOf(category.icon) } // Store index of selected icon
|
||||
|
||||
LaunchedEffect(category) {
|
||||
selectedIconIndex = category.icon
|
||||
}
|
||||
|
||||
/*
|
||||
val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
|
||||
val category = categoryViewModel.categoryUi.category
|
||||
var expanded by remember { mutableStateOf(false) }*/
|
||||
|
||||
|
||||
Log.w("vm", category.categoryName.toString())
|
||||
Log.w("vm", category.icon.toString())
|
||||
Log.w("vm", selectedIconIndex.toString())
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp, 0.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = category.categoryName,
|
||||
onValueChange = { editCategoryViewModel.updateCategory(category.copy(categoryName = it)) },
|
||||
label = { Text("Category Name") },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
)
|
||||
|
||||
ExposedDropdownMenuBox(
|
||||
expanded = expanded,
|
||||
onExpandedChange = { expanded = it }
|
||||
Column(
|
||||
Modifier.weight(1f)
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = iconNames[selectedIconIndex], // Show selected icon name
|
||||
onValueChange = {},
|
||||
label = { Text("Icon") },
|
||||
readOnly = true,
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
imageVector = icons[selectedIconIndex], // Replace with your desired icon
|
||||
contentDescription = "Leading Icon"
|
||||
)
|
||||
},
|
||||
trailingIcon = {
|
||||
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
|
||||
},
|
||||
value = category.categoryName,
|
||||
onValueChange = { editCategoryViewModel.updateCategory(category.copy(categoryName = it)) },
|
||||
label = { Text("Category Name") },
|
||||
modifier = Modifier
|
||||
.menuAnchor()
|
||||
.fillMaxWidth()
|
||||
)
|
||||
|
||||
ExposedDropdownMenu(
|
||||
ExposedDropdownMenuBox(
|
||||
expanded = expanded,
|
||||
onDismissRequest = { expanded = false }
|
||||
onExpandedChange = { expanded = it }
|
||||
) {
|
||||
icons.forEachIndexed { index, icon ->
|
||||
DropdownMenuItem(
|
||||
text = { Text(text = iconNames[index]) }, // Use name from iconNames
|
||||
onClick = {
|
||||
selectedIconIndex = index // Update selected index
|
||||
OutlinedTextField(
|
||||
value = iconNames[selectedIconIndex], // Show selected icon name
|
||||
onValueChange = {},
|
||||
label = { Text("Icon") },
|
||||
readOnly = true,
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
imageVector = icons[selectedIconIndex], // Replace with your desired icon
|
||||
contentDescription = "Leading Icon"
|
||||
)
|
||||
},
|
||||
trailingIcon = {
|
||||
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
|
||||
},
|
||||
modifier = Modifier
|
||||
.menuAnchor()
|
||||
.fillMaxWidth()
|
||||
)
|
||||
|
||||
editCategoryViewModel.updateCategory(category.copy(icon = index))
|
||||
expanded = false
|
||||
},
|
||||
leadingIcon = {
|
||||
androidx.compose.material3.Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = iconNames[index]
|
||||
)
|
||||
}
|
||||
)
|
||||
ExposedDropdownMenu(
|
||||
expanded = expanded,
|
||||
onDismissRequest = { expanded = false }
|
||||
) {
|
||||
icons.forEachIndexed { index, icon ->
|
||||
DropdownMenuItem(
|
||||
text = { Text(text = iconNames[index]) }, // Use name from iconNames
|
||||
onClick = {
|
||||
selectedIconIndex = index // Update selected index
|
||||
|
||||
editCategoryViewModel.updateCategory(category.copy(icon = index))
|
||||
expanded = false
|
||||
},
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = iconNames[index]
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(),
|
||||
contentAlignment = Alignment.Center
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally // Center buttons horizontally
|
||||
) {
|
||||
ButtonDanger(
|
||||
modifier = Modifier
|
||||
.padding(bottom = 8.dp),
|
||||
"Delete Category",
|
||||
onClick = {
|
||||
editCategoryViewModel.deleteEntry()
|
||||
onButtonClick()
|
||||
}
|
||||
)
|
||||
ButtonSuccess(
|
||||
modifier = Modifier,
|
||||
"Edit Category",
|
||||
"Save Category",
|
||||
onClick = {
|
||||
|
||||
editCategoryViewModel.saveCategory()
|
||||
onButtonClick()
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package at.xaxa.ledger.ui.category.edit
|
||||
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
@ -19,15 +20,12 @@ data class CategoryUIState(val category: CategoryEntity = CategoryEntity(0,"",0)
|
||||
|
||||
|
||||
|
||||
class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle,
|
||||
private val entryRepository: EntryRepository
|
||||
) : ViewModel() {
|
||||
class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle, private val entryRepository: EntryRepository) : ViewModel() {
|
||||
|
||||
|
||||
private val categoryId: Int = checkNotNull(savedStateHandle["categoryId"])
|
||||
|
||||
|
||||
var categoryUiState by mutableStateOf(CategoryUIState())
|
||||
var categoryUi by mutableStateOf(CategoryUIState())
|
||||
private set
|
||||
|
||||
@ -45,9 +43,9 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle,
|
||||
categoryUi = categoryUi.copy(category = category)
|
||||
}
|
||||
|
||||
fun onDeleteEntry(category: CategoryEntity) {
|
||||
fun deleteEntry() {
|
||||
viewModelScope.launch {
|
||||
entryRepository.deleteCategory(category)
|
||||
entryRepository.deleteCategory(categoryUi.category)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,9 +95,6 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Log.w("xaver", entry.date.toString())
|
||||
|
||||
DatePickerDocked(entry){
|
||||
dateMilis -> editViewModel.updateEntry(entry.copy(date = dateMilis))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user