Merge remote-tracking branch 'origin/Florian' into Xaver

# Conflicts:
#	Ledger/app/src/main/java/at/xaxa/ledger/ui/category/edit/EditCategory.kt
#	Ledger/app/src/main/java/at/xaxa/ledger/ui/entry/edit/EditUI.kt
This commit is contained in:
Xaver 2025-01-16 14:54:11 +01:00
commit 6c9c2117fe
6 changed files with 67 additions and 20 deletions

View File

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

View File

@ -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>>

View File

@ -139,6 +139,8 @@ fun EditCategory(
modifier = Modifier,
"Save Category",
onClick = {
editCategoryViewModel.findEntryByCategoryId()
Log.d("dsdsssee", editCategoryViewModel.entryUIState.toString())
editCategoryViewModel.saveCategory()
onButtonClick()
}

View File

@ -10,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
@ -28,6 +29,8 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle, priv
var categoryUi by mutableStateOf(CategoryUIState())
private set
var entryUIState by mutableStateOf(0)
init {
@ -63,4 +66,16 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle, priv
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.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]
)
}
)
}

View File

@ -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,32 +68,41 @@ 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]
)
}
)
}