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
class Entry (
data class Entry (
val id: Int,
val name: String,
val amount: Float,

View File

@ -1,6 +1,7 @@
package at.xaxa.ledger.ui
import android.icu.text.SimpleDateFormat
import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
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.em
import androidx.compose.ui.unit.sp
import at.xaxa.ledger.data.Entry
import java.util.Date
import java.util.Locale
@ -266,9 +268,15 @@ private fun CustomButton(modifier: Modifier = Modifier, text: String, onClick: (
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatePickerDocked(onDateSelected: (Long) -> Unit) {
fun DatePickerDocked(entry: Entry? = null, onDateSelected: (Long) -> Unit) {
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 {
convertMillisToDate(it)
} ?: ""
@ -277,8 +285,10 @@ fun DatePickerDocked(onDateSelected: (Long) -> Unit) {
modifier = Modifier.fillMaxWidth()
) {
OutlinedTextField(
value = selectedDate,
onValueChange = { },
value = entry?.date?.let { convertMillisToDate(it) }.toString() ,
onValueChange = {
entry?.copy(date = selectedDate.toLong())
},
label = { Text("Date") },
readOnly = true,
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 {
val formatter = SimpleDateFormat("DD.MM.yyyy", Locale.getDefault())
val formatter = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
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.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@ -32,21 +30,10 @@ import at.xaxa.ledger.ui.DatePickerDocked
@Composable
fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
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 selectedItem by remember { mutableStateOf(editViewModel.categoryUi.categories.categoryName) }
var selectedCategory by remember { mutableIntStateOf(entry.categoryID) }
Column(
modifier = modifier.fillMaxSize()
@ -57,15 +44,15 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
Modifier.weight(1f)
) {
OutlinedTextField(
value = name,
onValueChange = { name = it },
value = entry.name,
onValueChange = {editViewModel.updateEntry(entry.copy(name = it))},
label = { Text("Name") },
modifier = Modifier
.fillMaxWidth()
)
OutlinedTextField(
value = spending,
onValueChange = { spending = it },
value = entry.amount.toString(),
onValueChange = { editViewModel.updateEntry(entry.copy(amount = it.toFloat())) },
label = { Text("Spending") },
modifier = Modifier
.fillMaxWidth()
@ -77,7 +64,7 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
) {
// TextField to display the selected item and trigger the dropdown
OutlinedTextField(
value = selectedItem,
value = category.categoryName,
onValueChange = {},
label = { Text("Category") },
readOnly = true,
@ -98,8 +85,8 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
DropdownMenuItem(
text = { Text(text = item.categoryName) },
onClick = {
selectedItem = item.categoryName
selectedCategory = item._id
entry.copy(categoryID = item._id)
editViewModel.updateEntry(entry)
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(
modifier = Modifier.fillMaxWidth(), // Add spacing between buttons
text = "Done",
onClick = { onCardClick() }
onClick = {
editViewModel.saveEntry()
onCardClick()
}
)
}
}

View File

@ -17,7 +17,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
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(
@ -31,8 +31,10 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
var editUiState by mutableStateOf(EditUI())
var categoryUiState by mutableStateOf(CategoryListUIState())
private set
var categoryListUiState by mutableStateOf(CategoryListUIState())
var categoryUi by mutableStateOf(CategoryUIState())
private set
init {
@ -64,7 +66,7 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
val categories = withContext(Dispatchers.IO) {
entryRepository.getAllCategories()
}
categoryUiState = CategoryListUIState(categories)
categoryListUiState = CategoryListUIState(categories)
}
@ -75,9 +77,7 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
entryRepository.findCategoryById(categoryId)
}
categoryUi = CategoryUIState(category)
}
}
}
}