Added CategoryUI and ViewModel

This commit is contained in:
Florian 2025-01-15 09:48:49 +01:00
parent bb30679348
commit 3ca097a804
4 changed files with 26 additions and 4 deletions

View File

@ -0,0 +1,2 @@
package at.xaxa.ledger.ui.category

View File

@ -0,0 +1,2 @@
package at.xaxa.ledger.ui.category

View File

@ -14,6 +14,7 @@ import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@ -31,7 +32,8 @@ import at.xaxa.ledger.ui.home.HomeViewModel
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun Edit(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, HomeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) { fun Edit(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, EditViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
var name by remember { mutableStateOf("") } var name by remember { mutableStateOf("") }
var spending by remember { mutableStateOf("") } var spending by remember { mutableStateOf("") }
@ -111,12 +113,12 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, HomeViewMode
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(bottom = 8.dp), .padding(bottom = 8.dp),
text = "Delete Transaction", text = "Delete",
onClick = { print("test") } onClick = { print("test") }
) )
ButtonSuccess( ButtonSuccess(
modifier = Modifier.fillMaxWidth(), // Add spacing between buttons modifier = Modifier.fillMaxWidth(), // Add spacing between buttons
text = "Add Transaction", text = "Done",
onClick = { print("test") } onClick = { print("test") }
) )
} }

View File

@ -8,13 +8,19 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import at.xaxa.ledger.data.Entry import at.xaxa.ledger.data.Entry
import at.xaxa.ledger.data.EntryRepository import at.xaxa.ledger.data.EntryRepository
import at.xaxa.ledger.data.db.Category.CategoryEntity
import at.xaxa.ledger.ui.add.CategoryListUIState
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
data class EditUI( data class EditUI(
val entry: Entry = Entry(0, "", 0.0f, 0, 0) val entry: Entry = Entry(0, "", 0.0f, 0, 0)
@ -26,7 +32,7 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
private val entryId: Int = checkNotNull(savedStateHandle["entryId"]) private val entryId: Int = checkNotNull(savedStateHandle["entryId"])
var editUiState by mutableStateOf(EditUI()) var editUiState by mutableStateOf(EditUI())
private set var categoryUiState by mutableStateOf(CategoryListUIState())
init { init {
viewModelScope.launch { viewModelScope.launch {
@ -50,5 +56,15 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
entryRepository.updateEntry(editUiState.entry) entryRepository.updateEntry(editUiState.entry)
} }
} }
fun getAllCategories() {
viewModelScope.launch {
val categories = withContext(Dispatchers.IO) {
entryRepository.getAllCategories()
}
categoryUiState = CategoryListUIState(categories)
}
}
} }