findEntryByCategoryId not working

This commit is contained in:
Florian 2025-01-16 14:52:56 +01:00
parent 5a13a3ee41
commit 0027631611
7 changed files with 74 additions and 25 deletions

View File

@ -20,6 +20,9 @@ class EntryRepository(private val ledgerDao: LedgerDao){
it.map {category -> CategoryEntity(category._id, category.categoryName, category.icon) } it.map {category -> CategoryEntity(category._id, category.categoryName, category.icon) }
} }
} }
suspend fun findEntryByCategoryId(id: Int): Int {
return ledgerDao.findEntryByCategoryId(id)
}
suspend fun findEntryById(id: Int): Entry { suspend fun findEntryById(id: Int): Entry {
val entry = ledgerDao.findEntryById(id) val entry = ledgerDao.findEntryById(id)

View File

@ -49,6 +49,8 @@ interface LedgerDao {
@Query("SELECT * FROM _transaction WHERE _id = :id") @Query("SELECT * FROM _transaction WHERE _id = :id")
suspend fun findEntryById(id: Int) : EntryEntity suspend fun findEntryById(id: Int) : EntryEntity
@Query("SELECT COUNT (*) FROM _transaction WHERE categoryID = :id LIMIT 1")
fun findEntryByCategoryId(id: Int) : Int
@Query("SELECT * FROM _transaction") @Query("SELECT * FROM _transaction")
fun getAllEntries(): Flow<List<EntryEntity>> fun getAllEntries(): Flow<List<EntryEntity>>

View File

@ -6,12 +6,10 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import at.xaxa.ledger.data.Entry
import at.xaxa.ledger.data.EntryRepository import at.xaxa.ledger.data.EntryRepository
import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Category.CategoryEntity
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
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

View File

@ -47,12 +47,17 @@ fun EditCategory(
var expanded by remember { mutableStateOf(false) } // Controls dropdown visibility var expanded by remember { mutableStateOf(false) } // Controls dropdown visibility
val category = editCategoryViewModel.categoryUi.category val category = editCategoryViewModel.categoryUi.category
var selectedIconIndex by remember { mutableIntStateOf(category.icon) } // Store index of selected icon var selectedIconIndex by remember { mutableIntStateOf(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.d(
"ajsdasdj",
"$selectedIconIndex"
)
Column( Column(
modifier = modifier modifier = modifier
.fillMaxSize() .fillMaxSize()
@ -123,7 +128,8 @@ fun EditCategory(
modifier = Modifier, modifier = Modifier,
"Edit Category", "Edit Category",
onClick = { onClick = {
editCategoryViewModel.findEntryByCategoryId()
Log.d("dsdsssee", editCategoryViewModel.entryUIState.toString())
editCategoryViewModel.saveCategory() editCategoryViewModel.saveCategory()
onButtonClick() onButtonClick()
} }

View File

@ -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
@ -9,6 +10,7 @@ import androidx.lifecycle.viewModelScope
import at.xaxa.ledger.data.Entry import at.xaxa.ledger.data.Entry
import at.xaxa.ledger.data.EntryRepository import at.xaxa.ledger.data.EntryRepository
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 kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
@ -30,6 +32,8 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle,
var categoryUiState by mutableStateOf(CategoryUIState()) var categoryUiState by mutableStateOf(CategoryUIState())
var categoryUi by mutableStateOf(CategoryUIState()) var categoryUi by mutableStateOf(CategoryUIState())
private set private set
var entryUIState by mutableStateOf(0)
init { init {
@ -65,4 +69,16 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle,
categoryUi = CategoryUIState(category) categoryUi = CategoryUIState(category)
} }
} }
fun findEntryByCategoryId() {
viewModelScope.launch {
val fetchedEntries = withContext(Dispatchers.IO) {
entryRepository.findEntryByCategoryId(categoryId)
}
entryUIState = fetchedEntries
Log.w("ASASDADS", categoryId.toString())
Log.w("ASASDADS", fetchedEntries.toString())
}
//print("ASASDADS$categoryId")
}
} }

View File

@ -9,6 +9,7 @@ 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.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
@ -27,6 +28,8 @@ import at.xaxa.ledger.data.Entry
import at.xaxa.ledger.ui.AppViewModelProvider import at.xaxa.ledger.ui.AppViewModelProvider
import at.xaxa.ledger.ui.ButtonSuccess import at.xaxa.ledger.ui.ButtonSuccess
import at.xaxa.ledger.ui.DatePickerDocked import at.xaxa.ledger.ui.DatePickerDocked
import at.xaxa.ledger.ui.category.iconNames
import at.xaxa.ledger.ui.category.icons
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
@ -36,13 +39,11 @@ fun Add(
addViewModel: AddViewModel = viewModel(factory = AppViewModelProvider.Factory) addViewModel: AddViewModel = viewModel(factory = AppViewModelProvider.Factory)
) { ) {
var name by remember { mutableStateOf("") } var name by remember { mutableStateOf("") }
val state by addViewModel.addUiState.entries.collectAsState(initial = emptyList())
val categories by addViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
var spending by remember { mutableStateOf("") } var spending by remember { mutableStateOf("") }
var selectedDate by remember { mutableLongStateOf(0) } var selectedDate by remember { mutableLongStateOf(0) }
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
var selectedItem by remember { mutableStateOf("") } var selectedIconIndex by remember { mutableStateOf(0) } // Store index of selected icon
var selectedCategory by remember { mutableIntStateOf(-1) } var selectedCategory by remember { mutableIntStateOf(-1) }
Column( Column(
@ -69,16 +70,22 @@ fun Add(
.fillMaxWidth() .fillMaxWidth()
) )
ExposedDropdownMenuBox( ExposedDropdownMenuBox(
expanded = expanded, expanded = expanded,
onExpandedChange = { expanded = it } onExpandedChange = { expanded = it }
) { ) {
// TextField to display the selected item and trigger the dropdown
OutlinedTextField( OutlinedTextField(
value = selectedItem, value = iconNames[selectedIconIndex], // Show selected icon name
onValueChange = {}, onValueChange = {},
label = { Text("Category") }, label = { Text("Icon") },
readOnly = true, readOnly = true,
leadingIcon = {
Icon(
imageVector = icons[selectedIconIndex], // Replace with your desired icon
contentDescription = "Leading Icon"
)
},
trailingIcon = { trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
}, },
@ -87,18 +94,22 @@ fun Add(
.fillMaxWidth() .fillMaxWidth()
) )
// Dropdown menu
ExposedDropdownMenu( ExposedDropdownMenu(
expanded = expanded, expanded = expanded,
onDismissRequest = { expanded = false } onDismissRequest = { expanded = false }
) { ) {
categories.forEach { item -> icons.forEachIndexed { index, icon ->
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = item.categoryName) }, text = { Text(text = iconNames[index]) }, // Use name from iconNames
onClick = { onClick = {
selectedItem = item.categoryName selectedIconIndex = index // Update selected index
selectedCategory = item._id
expanded = false expanded = false
},
leadingIcon = {
androidx.compose.material3.Icon(
imageVector = icon,
contentDescription = iconNames[index]
)
} }
) )
} }

View File

@ -8,7 +8,9 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material3.DropdownMenuItem 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.TrailingIcon import androidx.compose.material3.ExposedDropdownMenuDefaults.TrailingIcon
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
@ -26,6 +28,8 @@ 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.DatePickerDocked
import at.xaxa.ledger.ui.category.iconNames
import at.xaxa.ledger.ui.category.icons
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
@ -33,6 +37,7 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
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(0) } // Store index of selected icon
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
@ -63,39 +68,47 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
expanded = expanded, expanded = expanded,
onExpandedChange = { expanded = it } onExpandedChange = { expanded = it }
) { ) {
// TextField to display the selected item and trigger the dropdown
OutlinedTextField( OutlinedTextField(
value = category.categoryName, value = iconNames[selectedIconIndex], // Show selected icon name
onValueChange = {}, onValueChange = {},
label = { Text("Category") }, label = { Text("Icon") },
readOnly = true, readOnly = true,
leadingIcon = {
Icon(
imageVector = icons[selectedIconIndex], // Replace with your desired icon
contentDescription = "Leading Icon"
)
},
trailingIcon = { trailingIcon = {
TrailingIcon(expanded = expanded) ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
}, },
modifier = Modifier modifier = Modifier
.menuAnchor() .menuAnchor()
.fillMaxWidth() .fillMaxWidth()
) )
// Dropdown menu
ExposedDropdownMenu( ExposedDropdownMenu(
expanded = expanded, expanded = expanded,
onDismissRequest = { expanded = false } onDismissRequest = { expanded = false }
) { ) {
categories.forEach { item -> icons.forEachIndexed { index, icon ->
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = item.categoryName) }, text = { Text(text = iconNames[index]) }, // Use name from iconNames
onClick = { onClick = {
entry.copy(categoryID = item._id) selectedIconIndex = index // Update selected index
editViewModel.updateEntry(entry)
expanded = false expanded = false
},
leadingIcon = {
androidx.compose.material3.Icon(
imageVector = icon,
contentDescription = iconNames[index]
)
} }
) )
} }
} }
} }
Log.w("xaver", entry.date.toString()) Log.w("xaver", entry.date.toString())
DatePickerDocked(entry){ DatePickerDocked(entry){