delete working

This commit is contained in:
Xaver 2025-01-16 15:16:12 +01:00
parent 6c9c2117fe
commit a0860c85ba
4 changed files with 102 additions and 66 deletions

View File

@ -32,7 +32,6 @@ 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

View File

@ -46,59 +46,62 @@ fun AddCategory(
.padding(16.dp, 0.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedTextField(
value = name,
onValueChange = { name = 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 = name,
onValueChange = { name = 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
expanded = false
},
leadingIcon = {
androidx.compose.material3.Icon(
imageVector = icon,
contentDescription = iconNames[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()
)
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
expanded = false
},
leadingIcon = {
androidx.compose.material3.Icon(
imageVector = icon,
contentDescription = iconNames[index]
)
}
)
}
}
}
}
Box(
modifier = Modifier
.fillMaxWidth(),

View File

@ -6,6 +6,9 @@ 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
@ -13,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.LaunchedEffect
import androidx.compose.runtime.getValue
@ -40,22 +44,55 @@ fun EditCategory(
editCategoryViewModel: EditCategoryViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
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 showError by remember { mutableStateOf(editCategoryViewModel.entryUIState) }
var deleteStarted by remember { mutableStateOf(false) }
var expanded by remember { mutableStateOf(false) }
var selectedIconIndex by remember { mutableIntStateOf(category.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) }*/
LaunchedEffect(deleteStarted) {
if (deleteStarted) {
editCategoryViewModel.findEntryByCategoryId()
showError = editCategoryViewModel.entryUIState
}
}
if (deleteStarted && showError) {
AlertDialog(
icon = {
Icon(Icons.Default.Info, contentDescription = "Info Icon")
},
title = {
Text(text = "Category in use")
},
text = {
Text(text = "You are trying to delete a Category which is still in use. Please delete or change the Category in your entries which are in use.")
},
onDismissRequest = {
deleteStarted = false
showError = false
},
confirmButton = {
TextButton(
onClick = {
deleteStarted = false
showError = false
}
) {
Text("Okay")
}
}
)
}else if(deleteStarted && !showError){
Log.w("xaver", "delete")
editCategoryViewModel.deleteEntry()
onButtonClick()
}
Log.w("vm", category.categoryName.toString())
Log.w("vm", category.icon.toString())
Log.w("vm", selectedIconIndex.toString())
Log.w("xaver", "deleteStarted $deleteStarted : showError $showError" )
Column(
modifier = modifier
@ -79,13 +116,13 @@ fun EditCategory(
onExpandedChange = { expanded = it }
) {
OutlinedTextField(
value = iconNames[selectedIconIndex], // Show selected icon name
value = iconNames[selectedIconIndex],
onValueChange = {},
label = { Text("Icon") },
readOnly = true,
leadingIcon = {
Icon(
imageVector = icons[selectedIconIndex], // Replace with your desired icon
imageVector = icons[selectedIconIndex],
contentDescription = "Leading Icon"
)
},
@ -103,10 +140,9 @@ fun EditCategory(
) {
icons.forEachIndexed { index, icon ->
DropdownMenuItem(
text = { Text(text = iconNames[index]) }, // Use name from iconNames
text = { Text(text = iconNames[index]) },
onClick = {
selectedIconIndex = index // Update selected index
selectedIconIndex = index
editCategoryViewModel.updateCategory(category.copy(icon = index))
expanded = false
},
@ -124,23 +160,21 @@ fun EditCategory(
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally // Center buttons horizontally
horizontalAlignment = Alignment.CenterHorizontally
) {
ButtonDanger(
modifier = Modifier
.padding(bottom = 8.dp),
"Delete Category",
onClick = {
editCategoryViewModel.deleteEntry()
onButtonClick()
deleteStarted = true
editCategoryViewModel.findEntryByCategoryId()
}
)
ButtonSuccess(
modifier = Modifier,
"Save Category",
onClick = {
editCategoryViewModel.findEntryByCategoryId()
Log.d("dsdsssee", editCategoryViewModel.entryUIState.toString())
editCategoryViewModel.saveCategory()
onButtonClick()
}

View File

@ -29,7 +29,7 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle, priv
var categoryUi by mutableStateOf(CategoryUIState())
private set
var entryUIState by mutableStateOf(0)
var entryUIState by mutableStateOf(false)
@ -71,10 +71,10 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle, priv
val fetchedEntries = withContext(Dispatchers.IO) {
entryRepository.findEntryByCategoryId(categoryId)
}
entryUIState = fetchedEntries
Log.w("ASASDADS", categoryId.toString())
Log.w("ASASDADS", fetchedEntries.toString())
if(fetchedEntries>0){
entryUIState = true
}
}
//print("ASASDADS$categoryId")
}