findEntryByCategoryId not working
This commit is contained in:
parent
5a13a3ee41
commit
0027631611
@ -20,6 +20,9 @@ class EntryRepository(private val ledgerDao: LedgerDao){
|
||||
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 {
|
||||
val entry = ledgerDao.findEntryById(id)
|
||||
|
@ -49,6 +49,8 @@ interface LedgerDao {
|
||||
@Query("SELECT * FROM _transaction WHERE _id = :id")
|
||||
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")
|
||||
fun getAllEntries(): Flow<List<EntryEntity>>
|
||||
|
@ -6,12 +6,10 @@ import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import at.xaxa.ledger.data.Entry
|
||||
import at.xaxa.ledger.data.EntryRepository
|
||||
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
@ -47,12 +47,17 @@ fun EditCategory(
|
||||
|
||||
var expanded by remember { mutableStateOf(false) } // Controls dropdown visibility
|
||||
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 category = categoryViewModel.categoryUi.category
|
||||
var expanded by remember { mutableStateOf(false) }*/
|
||||
|
||||
Log.d(
|
||||
"ajsdasdj",
|
||||
"$selectedIconIndex"
|
||||
)
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
@ -123,7 +128,8 @@ fun EditCategory(
|
||||
modifier = Modifier,
|
||||
"Edit Category",
|
||||
onClick = {
|
||||
|
||||
editCategoryViewModel.findEntryByCategoryId()
|
||||
Log.d("dsdsssee", editCategoryViewModel.entryUIState.toString())
|
||||
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
|
||||
@ -9,6 +10,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import at.xaxa.ledger.data.Entry
|
||||
import at.xaxa.ledger.data.EntryRepository
|
||||
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||
import at.xaxa.ledger.data.db.Entry.EntryEntity
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
@ -30,6 +32,8 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle,
|
||||
var categoryUiState by mutableStateOf(CategoryUIState())
|
||||
var categoryUi by mutableStateOf(CategoryUIState())
|
||||
private set
|
||||
var entryUIState by mutableStateOf(0)
|
||||
|
||||
|
||||
|
||||
init {
|
||||
@ -65,4 +69,16 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle,
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.ExposedDropdownMenuBox
|
||||
import androidx.compose.material3.ExposedDropdownMenuDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
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.ButtonSuccess
|
||||
import at.xaxa.ledger.ui.DatePickerDocked
|
||||
import at.xaxa.ledger.ui.category.iconNames
|
||||
import at.xaxa.ledger.ui.category.icons
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@ -36,13 +39,11 @@ fun Add(
|
||||
addViewModel: AddViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
||||
) {
|
||||
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 selectedDate by remember { mutableLongStateOf(0) }
|
||||
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) }
|
||||
|
||||
Column(
|
||||
@ -69,16 +70,22 @@ fun Add(
|
||||
.fillMaxWidth()
|
||||
)
|
||||
|
||||
|
||||
ExposedDropdownMenuBox(
|
||||
expanded = expanded,
|
||||
onExpandedChange = { expanded = it }
|
||||
) {
|
||||
// TextField to display the selected item and trigger the dropdown
|
||||
OutlinedTextField(
|
||||
value = selectedItem,
|
||||
value = iconNames[selectedIconIndex], // Show selected icon name
|
||||
onValueChange = {},
|
||||
label = { Text("Category") },
|
||||
label = { Text("Icon") },
|
||||
readOnly = true,
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
imageVector = icons[selectedIconIndex], // Replace with your desired icon
|
||||
contentDescription = "Leading Icon"
|
||||
)
|
||||
},
|
||||
trailingIcon = {
|
||||
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
|
||||
},
|
||||
@ -87,18 +94,22 @@ fun Add(
|
||||
.fillMaxWidth()
|
||||
)
|
||||
|
||||
// Dropdown menu
|
||||
ExposedDropdownMenu(
|
||||
expanded = expanded,
|
||||
onDismissRequest = { expanded = false }
|
||||
) {
|
||||
categories.forEach { item ->
|
||||
icons.forEachIndexed { index, icon ->
|
||||
DropdownMenuItem(
|
||||
text = { Text(text = item.categoryName) },
|
||||
text = { Text(text = iconNames[index]) }, // Use name from iconNames
|
||||
onClick = {
|
||||
selectedItem = item.categoryName
|
||||
selectedCategory = item._id
|
||||
selectedIconIndex = index // Update selected index
|
||||
expanded = false
|
||||
},
|
||||
leadingIcon = {
|
||||
androidx.compose.material3.Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = iconNames[index]
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -8,7 +8,9 @@ import androidx.compose.foundation.layout.padding
|
||||
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
|
||||
@ -26,6 +28,8 @@ 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.iconNames
|
||||
import at.xaxa.ledger.ui.category.icons
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@ -33,6 +37,7 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
|
||||
val entry = editViewModel.editUiState.entry
|
||||
val category = editViewModel.categoryUi.category
|
||||
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) }
|
||||
|
||||
@ -63,39 +68,47 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
|
||||
expanded = expanded,
|
||||
onExpandedChange = { expanded = it }
|
||||
) {
|
||||
// TextField to display the selected item and trigger the dropdown
|
||||
OutlinedTextField(
|
||||
value = category.categoryName,
|
||||
value = iconNames[selectedIconIndex], // Show selected icon name
|
||||
onValueChange = {},
|
||||
label = { Text("Category") },
|
||||
label = { Text("Icon") },
|
||||
readOnly = true,
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
imageVector = icons[selectedIconIndex], // Replace with your desired icon
|
||||
contentDescription = "Leading Icon"
|
||||
)
|
||||
},
|
||||
trailingIcon = {
|
||||
TrailingIcon(expanded = expanded)
|
||||
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded)
|
||||
},
|
||||
modifier = Modifier
|
||||
.menuAnchor()
|
||||
.fillMaxWidth()
|
||||
)
|
||||
|
||||
// Dropdown menu
|
||||
ExposedDropdownMenu(
|
||||
expanded = expanded,
|
||||
onDismissRequest = { expanded = false }
|
||||
) {
|
||||
categories.forEach { item ->
|
||||
icons.forEachIndexed { index, icon ->
|
||||
DropdownMenuItem(
|
||||
text = { Text(text = item.categoryName) },
|
||||
text = { Text(text = iconNames[index]) }, // Use name from iconNames
|
||||
onClick = {
|
||||
entry.copy(categoryID = item._id)
|
||||
editViewModel.updateEntry(entry)
|
||||
selectedIconIndex = index // Update selected index
|
||||
expanded = false
|
||||
},
|
||||
leadingIcon = {
|
||||
androidx.compose.material3.Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = iconNames[index]
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Log.w("xaver", entry.date.toString())
|
||||
|
||||
DatePickerDocked(entry){
|
||||
|
Loading…
Reference in New Issue
Block a user