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