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

# Conflicts:
#	Ledger/app/src/main/java/at/xaxa/ledger/ui/category/edit/EditCategory.kt
This commit is contained in:
Florian 2025-01-16 11:07:21 +01:00
commit 5a13a3ee41
9 changed files with 38 additions and 35 deletions

View File

@ -5,10 +5,10 @@ import androidx.lifecycle.createSavedStateHandle
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import at.xaxa.ledger.LedgerApplication
import at.xaxa.ledger.ui.add.AddViewModel
import at.xaxa.ledger.ui.entry.add.AddViewModel
import at.xaxa.ledger.ui.category.CategoryViewModel
import at.xaxa.ledger.ui.category.edit.EditCategoryViewModel
import at.xaxa.ledger.ui.edit.EditViewModel
import at.xaxa.ledger.ui.entry.edit.EditViewModel
import at.xaxa.ledger.ui.home.HomeViewModel

View File

@ -11,11 +11,11 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import at.xaxa.ledger.ui.add.Add
import at.xaxa.ledger.ui.entry.add.Add
import at.xaxa.ledger.ui.category.CategoryOverview
import at.xaxa.ledger.ui.category.add.AddCategory
import at.xaxa.ledger.ui.category.edit.EditCategory
import at.xaxa.ledger.ui.edit.Edit
import at.xaxa.ledger.ui.entry.edit.Edit
import at.xaxa.ledger.ui.home.Home
enum class AppRoutes(val route: String) {

View File

@ -34,7 +34,9 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@ -337,27 +339,31 @@ private fun CustomButton(modifier: Modifier = Modifier, text: String, onClick: (
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatePickerDocked(entry: Entry? = null, onDateSelected: (Long) -> Unit) {
fun DatePickerDocked(entry: Entry = Entry(0, "", 0f, 0L, 0), onDateSelected: (Long) -> Unit) {
var showDatePicker by remember { mutableStateOf(false) }
var selectedDateMillis by remember { mutableLongStateOf(System.currentTimeMillis()) }
// Update selectedDateMillis when entry.date changes
LaunchedEffect(entry.date) {
selectedDateMillis = if (entry.date > 0L) entry.date else System.currentTimeMillis()
}
// Initialize datePickerState with the provided date (if not -1)
val datePickerState = rememberDatePickerState(
initialSelectedDateMillis = System.currentTimeMillis()
initialSelectedDateMillis = selectedDateMillis
)
// Convert the selected date to a readable format
val selectedDate = datePickerState.selectedDateMillis?.let {
val selectedDate = selectedDateMillis.let {
convertMillisToDate(it)
} ?: ""
}
Box(
modifier = Modifier.fillMaxWidth()
) {
OutlinedTextField(
value = entry?.date?.let { convertMillisToDate(it) }.toString() ,
onValueChange = {
entry?.copy(date = selectedDate.toLong())
},
value = selectedDate,
onValueChange = {},
label = { Text("Date") },
readOnly = true,
trailingIcon = {
@ -368,8 +374,7 @@ fun DatePickerDocked(entry: Entry? = null, onDateSelected: (Long) -> Unit) {
)
}
},
modifier = Modifier
.fillMaxWidth()
modifier = Modifier.fillMaxWidth()
)
if (showDatePicker) {
@ -379,8 +384,9 @@ fun DatePickerDocked(entry: Entry? = null, onDateSelected: (Long) -> Unit) {
TextButton(
onClick = {
showDatePicker = false
// Notify the parent about the selected date
// Update the selected date
datePickerState.selectedDateMillis?.let { millis ->
selectedDateMillis = millis
onDateSelected(millis)
}
}

View File

@ -17,6 +17,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@ -33,7 +34,6 @@ 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.icons
import at.xaxa.ledger.ui.edit.EditViewModel
@ -47,13 +47,7 @@ fun EditCategory(
var expanded by remember { mutableStateOf(false) } // Controls dropdown visibility
val category = editCategoryViewModel.categoryUi.category
var selectedIconIndex = category.icon // Store index of selected icon
Log.d(
"kkjkjkjkj",
category.categoryName)
var selectedIconIndex by remember { mutableIntStateOf(category.icon) } // Store index of selected icon
/*
val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
val category = categoryViewModel.categoryUi.category
@ -106,8 +100,7 @@ fun EditCategory(
onClick = {
selectedIconIndex = index // Update selected index
category.copy(icon = index)
editCategoryViewModel.updateCategory(category)
editCategoryViewModel.updateCategory(category.copy(icon = index))
expanded = false
},
leadingIcon = {
@ -130,6 +123,7 @@ fun EditCategory(
modifier = Modifier,
"Edit Category",
onClick = {
editCategoryViewModel.saveCategory()
onButtonClick()
}

View File

@ -44,11 +44,13 @@ class EditCategoryViewModel(private val savedStateHandle: SavedStateHandle,
fun updateCategory(category: CategoryEntity) {
categoryUi = categoryUi.copy(category = category)
}
fun onDeleteEntry(category: CategoryEntity) {
viewModelScope.launch {
entryRepository.deleteCategory(category)
}
}
fun saveCategory() {
viewModelScope.launch {
entryRepository.updateCategory(categoryUi.category)

View File

@ -1,4 +1,4 @@
package at.xaxa.ledger.ui.add
package at.xaxa.ledger.ui.entry.add
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column

View File

@ -1,4 +1,4 @@
package at.xaxa.ledger.ui.add
package at.xaxa.ledger.ui.entry.add
import android.os.Debug
import android.util.Log

View File

@ -1,4 +1,4 @@
package at.xaxa.ledger.ui.edit
package at.xaxa.ledger.ui.entry.edit
import android.util.Log
import androidx.compose.foundation.layout.Column
@ -29,8 +29,7 @@ import at.xaxa.ledger.ui.DatePickerDocked
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory), onValueChange: (Entry) -> Unit = {},
) {
fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory), onValueChange: (Entry) -> Unit = {}) {
val entry = editViewModel.editUiState.entry
val category = editViewModel.categoryUi.category
val categories by editViewModel.categoryListUiState.categories.collectAsState(initial = emptyList())
@ -113,7 +112,10 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
.fillMaxWidth()
.padding(bottom = 8.dp),
text = "Delete",
onClick = { onCardClick() }
onClick = {
editViewModel.deleteEntry()
onCardClick()
}
)
ButtonSuccess(
modifier = Modifier.fillMaxWidth(), // Add spacing between buttons

View File

@ -1,6 +1,5 @@
package at.xaxa.ledger.ui.edit
package at.xaxa.ledger.ui.entry.edit
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
@ -52,9 +51,9 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
fun updateEntry(entry: Entry) {
editUiState = editUiState.copy(entry=entry)
}
fun onDeleteEntry(entry: Entry) {
fun deleteEntry() {
viewModelScope.launch {
entryRepository.deleteEntry(entry)
entryRepository.deleteEntry(entryRepository.findEntryById(entryId))
}
}
fun saveEntry() {