overview calc

This commit is contained in:
Xaver 2025-01-17 09:43:52 +01:00
parent 7e66cdc30b
commit db33c08fab
8 changed files with 57 additions and 41 deletions

View File

@ -10,7 +10,6 @@ import kotlinx.coroutines.flow.map
class EntryRepository(private val ledgerDao: LedgerDao){
fun getAllEntries(): Flow<List<Entry>> {
return ledgerDao.getAllEntries().map {
it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryID) }
@ -25,6 +24,10 @@ class EntryRepository(private val ledgerDao: LedgerDao){
return ledgerDao.findEntryByCategoryId(id)
}
suspend fun calculateBalance(): Float{
return ledgerDao.calculateBalance()
}
suspend fun findEntryById(id: Int): Entry {
val entry = ledgerDao.findEntryById(id)
return Entry(

View File

@ -55,4 +55,6 @@ interface LedgerDao {
@Query("SELECT * FROM _transaction")
fun getAllEntries(): Flow<List<EntryEntity>>
@Query("SELECT SUM(amount) From _transaction")
suspend fun calculateBalance(): Float
}

View File

@ -87,13 +87,10 @@ fun EditCategory(
}
)
}else if(deleteStarted && !showError){
Log.w("xaver", "delete")
editCategoryViewModel.deleteEntry()
onButtonClick()
}
Log.w("xaver", "deleteStarted $deleteStarted : showError $showError" )
Column(
modifier = modifier
.fillMaxSize()

View File

@ -168,10 +168,6 @@ fun Add(
"Add Transaction",
onClick = {
val isValidSpending = spending.matches(Regex("^[+-]?\\d*(\\.\\d+)?$"))
Log.w("xaxaxa", name.isNotBlank().toString() )
Log.w("xaxaxa", isValidSpending.toString() )
Log.w("xaxaxa", (selectedDate != 0L).toString() )
Log.w("xaxaxa", (selectedCategory != -1).toString() )
if (name.isNotBlank() && isValidSpending && selectedDate != 0L && selectedCategory != -1) {
val newEntry = Entry(
id = 0,

View File

@ -41,7 +41,6 @@ class AddViewModel(
}
fun addEntryToDB(entry: Entry) {
//Log.d("KRAUSI", "WE ARE INNNN")
viewModelScope.launch {
try {
val entryEntity = Entry(
@ -61,7 +60,6 @@ class AddViewModel(
}
}
fun getAllCategories() {
viewModelScope.launch {
val categories = withContext(Dispatchers.IO) {

View File

@ -16,6 +16,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
@ -32,17 +33,25 @@ import at.xaxa.ledger.ui.category.iconNames
import at.xaxa.ledger.ui.category.icons
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory), onValueChange: (Entry) -> Unit = {}) {
val entry = editViewModel.editUiState.entry
@Composable
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())
var selectedIconIndex by remember { mutableStateOf(0) } // Store index of selected icon
var selectedIconIndex by remember { mutableStateOf(category.icon) } // Store index of selected icon
var selectedCategoryName by remember { mutableStateOf(category.categoryName) } // Store index of selected icon
var selectedCategory by remember { mutableIntStateOf(category.icon) }
var expanded by remember { mutableStateOf(false) }
Column(
modifier = modifier.fillMaxSize()
modifier = modifier
.fillMaxSize()
.padding(16.dp, 0.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
@ -51,14 +60,19 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
) {
OutlinedTextField(
value = entry.name,
onValueChange = {editViewModel.updateEntry(entry.copy(name = it))},
onValueChange = { editViewModel.updateEntry(entry.copy(name = it)) },
label = { Text("Name") },
modifier = Modifier
.fillMaxWidth()
)
OutlinedTextField(
value = entry.amount.toString(),
onValueChange = { editViewModel.updateEntry(entry.copy(amount = it.toFloat())) },
onValueChange = {
val isValidSpending = it.matches(Regex("^[+-]?\\d*(\\.\\d{0,2})?$"))
if (isValidSpending){
editViewModel.updateEntry(entry.copy(amount = it.toFloat()))
}
},
label = { Text("Spending") },
modifier = Modifier
.fillMaxWidth()
@ -69,9 +83,9 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
onExpandedChange = { expanded = it }
) {
OutlinedTextField(
value = iconNames[selectedIconIndex], // Show selected icon name
value = selectedCategoryName, // Show selected icon name
onValueChange = {},
label = { Text("Icon") },
label = { Text("Category") },
readOnly = true,
leadingIcon = {
Icon(
@ -91,17 +105,19 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
expanded = expanded,
onDismissRequest = { expanded = false }
) {
icons.forEachIndexed { index, icon ->
categories.forEachIndexed { index, category ->
DropdownMenuItem(
text = { Text(text = iconNames[index]) }, // Use name from iconNames
text = { Text(text = category.categoryName) }, // Use name from iconNames
onClick = {
selectedIconIndex = index // Update selected index
selectedIconIndex = category.icon // Update selected index
expanded = false
selectedCategoryName = category.categoryName
selectedCategory = category._id
},
leadingIcon = {
androidx.compose.material3.Icon(
imageVector = icon,
contentDescription = iconNames[index]
imageVector = icons[category.icon],
contentDescription = iconNames[category.icon]
)
}
)
@ -109,8 +125,8 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
}
}
DatePickerDocked(entry){
dateMilis -> editViewModel.updateEntry(entry.copy(date = dateMilis))
DatePickerDocked(entry) { dateMilis ->
editViewModel.updateEntry(entry.copy(date = dateMilis))
}
}

View File

@ -24,8 +24,10 @@ import at.xaxa.ledger.ui.convertMillisToDate
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClick: () -> Unit, onCatButtonClick: () -> Unit, HomeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
val state by HomeViewModel.entryUIState.entry.collectAsState(initial = emptyList())
fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClick: () -> Unit, onCatButtonClick: () -> Unit, homeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
val state by homeViewModel.entryUIState.entry.collectAsState(initial = emptyList())
homeViewModel.calculateBalance()
val balance = homeViewModel.balance
Column(
modifier = Modifier
@ -33,13 +35,12 @@ fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClic
.padding(16.dp, 0.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
val items = (1..20).toList()
LazyColumn(
modifier = Modifier
.weight(1f)
) {
stickyHeader {
HeaderCard(modifier = modifier, "-13563.00", onCatButtonClick)
HeaderCard(modifier = modifier, balance.toString()+"", onCatButtonClick)
}
items(state) { item ->
Column(

View File

@ -1,6 +1,7 @@
package at.xaxa.ledger.ui.home
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
@ -9,6 +10,8 @@ import at.xaxa.ledger.data.Entry
import at.xaxa.ledger.data.EntryRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -16,23 +19,23 @@ import kotlinx.coroutines.withContext
data class EntryListUIState(val entry: Flow<List<Entry>> = flowOf(emptyList()))
class HomeViewModel(private val repository: EntryRepository): ViewModel() {
class HomeViewModel(private val repository: EntryRepository) : ViewModel() {
var entryUIState by mutableStateOf(EntryListUIState())
init{
var balance by mutableFloatStateOf(0.0f)
private set // Restrict external modification
init {
viewModelScope.launch {
val entries = withContext(Dispatchers.IO){
val entries = withContext(Dispatchers.IO) {
repository.getAllEntries()
}
entryUIState = EntryListUIState(entries)
}
}
/* val entryUIState = repository.getAllEntries()
.map {EntryListUIState(it)}
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = EntryListUIState(emptyList())
)*/
fun calculateBalance() {
viewModelScope.launch {
balance = repository.calculateBalance()
}
}
}