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

# Conflicts:
#	Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditUI.kt
#	Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt
This commit is contained in:
Florian 2025-01-15 14:54:07 +01:00
commit b32cfe1844
3 changed files with 37 additions and 36 deletions

View File

@ -1,6 +1,7 @@
package at.xaxa.ledger.ui package at.xaxa.ledger.ui
import android.icu.text.SimpleDateFormat import android.icu.text.SimpleDateFormat
import android.util.Log
import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
@ -48,6 +49,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import at.xaxa.ledger.data.Entry
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
@ -266,9 +268,15 @@ private fun CustomButton(modifier: Modifier = Modifier, text: String, onClick: (
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun DatePickerDocked(onDateSelected: (Long) -> Unit) { fun DatePickerDocked(entry: Entry? = null, onDateSelected: (Long) -> Unit) {
var showDatePicker by remember { mutableStateOf(false) } var showDatePicker by remember { mutableStateOf(false) }
val datePickerState = rememberDatePickerState()
// Initialize datePickerState with the provided date (if not -1)
val datePickerState = rememberDatePickerState(
initialSelectedDateMillis = System.currentTimeMillis()
)
// Convert the selected date to a readable format
val selectedDate = datePickerState.selectedDateMillis?.let { val selectedDate = datePickerState.selectedDateMillis?.let {
convertMillisToDate(it) convertMillisToDate(it)
} ?: "" } ?: ""
@ -277,8 +285,10 @@ fun DatePickerDocked(onDateSelected: (Long) -> Unit) {
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) { ) {
OutlinedTextField( OutlinedTextField(
value = selectedDate, value = entry?.date?.let { convertMillisToDate(it) }.toString() ,
onValueChange = { }, onValueChange = {
entry?.copy(date = selectedDate.toLong())
},
label = { Text("Date") }, label = { Text("Date") },
readOnly = true, readOnly = true,
trailingIcon = { trailingIcon = {
@ -326,8 +336,9 @@ fun DatePickerDocked(onDateSelected: (Long) -> Unit) {
} }
} }
// Helper function to convert milliseconds to a readable date format
fun convertMillisToDate(millis: Long): String { fun convertMillisToDate(millis: Long): String {
val formatter = SimpleDateFormat("DD.MM.yyyy", Locale.getDefault()) val formatter = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
return formatter.format(Date(millis)) return formatter.format(Date(millis))
} }

View File

@ -14,8 +14,6 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
@ -34,21 +32,10 @@ import at.xaxa.ledger.ui.DatePickerDocked
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 entry = editViewModel.editUiState.entry
val categories by editViewModel.categoryUiState.categories.collectAsState(initial = emptyList()) val category = editViewModel.categoryUi.category
val categories by editViewModel.categoryListUiState.categories.collectAsState(initial = emptyList())
Log.w("xaver", entry.name)
Log.w("xaver", entry.amount.toString())
Log.w("xaver", entry.date.toString())
Log.w("xaver", editViewModel.categoryUi.categories.categoryName)
Log.w("xaver", entry.categoryID.toString())
var name by remember { mutableStateOf(entry.name) }
var spending by remember { mutableStateOf(entry.amount.toString()) }
var selectedDate by remember { mutableLongStateOf(entry.date) }
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
var selectedItem by remember { mutableStateOf(editViewModel.categoryUi.categories.categoryName) }
var selectedCategory by remember { mutableIntStateOf(entry.categoryID) }
Column( Column(
modifier = modifier.fillMaxSize() modifier = modifier.fillMaxSize()
@ -60,16 +47,14 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
) { ) {
OutlinedTextField( OutlinedTextField(
value = entry.name, value = entry.name,
onValueChange = { newText -> onValueChange = {editViewModel.updateEntry(entry.copy(name = it))},
onValueChange(entry.copy(name = newText))
},
label = { Text("Name") }, label = { Text("Name") },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
) )
OutlinedTextField( OutlinedTextField(
value = spending, value = entry.amount.toString(),
onValueChange = { spending = it }, onValueChange = { editViewModel.updateEntry(entry.copy(amount = it.toFloat())) },
label = { Text("Spending") }, label = { Text("Spending") },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -81,7 +66,7 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
) { ) {
// TextField to display the selected item and trigger the dropdown // TextField to display the selected item and trigger the dropdown
OutlinedTextField( OutlinedTextField(
value = selectedItem, value = category.categoryName,
onValueChange = {}, onValueChange = {},
label = { Text("Category") }, label = { Text("Category") },
readOnly = true, readOnly = true,
@ -102,8 +87,8 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = item.categoryName) }, text = { Text(text = item.categoryName) },
onClick = { onClick = {
selectedItem = item.categoryName entry.copy(categoryID = item._id)
selectedCategory = item._id editViewModel.updateEntry(entry)
expanded = false expanded = false
} }
) )
@ -111,8 +96,11 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
} }
} }
DatePickerDocked{
dateMilis -> selectedDate = dateMilis Log.w("xaver", entry.date.toString())
DatePickerDocked(entry){
dateMilis -> editViewModel.updateEntry(entry.copy(date = dateMilis))
} }
} }
@ -130,7 +118,10 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
ButtonSuccess( ButtonSuccess(
modifier = Modifier.fillMaxWidth(), // Add spacing between buttons modifier = Modifier.fillMaxWidth(), // Add spacing between buttons
text = "Done", text = "Done",
onClick = { onCardClick() } onClick = {
editViewModel.saveEntry()
onCardClick()
}
) )
} }
} }

View File

@ -17,7 +17,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList())) data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
data class CategoryUIState(val categories: CategoryEntity = CategoryEntity(0,"",0)) data class CategoryUIState(val category: CategoryEntity = CategoryEntity(0,"",0))
data class EditUI( data class EditUI(
@ -32,8 +32,9 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
var editUiState by mutableStateOf(EditUI()) var editUiState by mutableStateOf(EditUI())
private set private set
var categoryUiState by mutableStateOf(CategoryListUIState()) var categoryListUiState by mutableStateOf(CategoryListUIState())
var categoryUi by mutableStateOf(CategoryUIState()) var categoryUi by mutableStateOf(CategoryUIState())
private set
init { init {
@ -65,7 +66,7 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
val categories = withContext(Dispatchers.IO) { val categories = withContext(Dispatchers.IO) {
entryRepository.getAllCategories() entryRepository.getAllCategories()
} }
categoryUiState = CategoryListUIState(categories) categoryListUiState = CategoryListUIState(categories)
} }
@ -76,9 +77,7 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
entryRepository.findCategoryById(categoryId) entryRepository.findCategoryById(categoryId)
} }
categoryUi = CategoryUIState(category) categoryUi = CategoryUIState(category)
}
}
} }
} }