Edit Screen Func done

This commit is contained in:
Xaver 2025-01-15 14:50:08 +01:00
parent 80a8c8cb06
commit cd2337722a
4 changed files with 40 additions and 36 deletions

View File

@ -2,7 +2,7 @@ package at.xaxa.ledger.data
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
class Entry ( data class Entry (
val id: Int, val id: Int,
val name: String, val name: String,
val amount: Float, val amount: Float,

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
@ -32,21 +30,10 @@ import at.xaxa.ledger.ui.DatePickerDocked
@Composable @Composable
fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory)) { fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
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()
@ -57,15 +44,15 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
Modifier.weight(1f) Modifier.weight(1f)
) { ) {
OutlinedTextField( OutlinedTextField(
value = name, value = entry.name,
onValueChange = { name = it }, onValueChange = {editViewModel.updateEntry(entry.copy(name = it))},
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()
@ -77,7 +64,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,
@ -98,8 +85,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
} }
) )
@ -107,8 +94,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))
} }
} }
@ -126,7 +116,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(
@ -31,8 +31,10 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
var editUiState by mutableStateOf(EditUI()) var editUiState by mutableStateOf(EditUI())
var categoryUiState by mutableStateOf(CategoryListUIState()) private set
var categoryListUiState by mutableStateOf(CategoryListUIState())
var categoryUi by mutableStateOf(CategoryUIState()) var categoryUi by mutableStateOf(CategoryUIState())
private set
init { init {
@ -64,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)
} }
@ -75,9 +77,7 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
entryRepository.findCategoryById(categoryId) entryRepository.findCategoryById(categoryId)
} }
categoryUi = CategoryUIState(category) categoryUi = CategoryUIState(category)
} }
} }
} }