Connection between AddUI and AddViewmodel?!?!?
This commit is contained in:
parent
df8fae5eec
commit
29fe4182be
@ -5,6 +5,7 @@ import androidx.lifecycle.createSavedStateHandle
|
||||
import androidx.lifecycle.viewmodel.initializer
|
||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||
import at.xaxa.ledger.LedgerApplication
|
||||
import at.xaxa.ledger.ui.add.AddViewModel
|
||||
import at.xaxa.ledger.ui.edit.EditViewModel
|
||||
import at.xaxa.ledger.ui.home.HomeViewModel
|
||||
|
||||
@ -15,9 +16,9 @@ object AppViewModelProvider {
|
||||
HomeViewModel((this[APPLICATION_KEY] as LedgerApplication).entryRepository)
|
||||
}
|
||||
|
||||
/*initializer {
|
||||
initializer {
|
||||
AddViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository)
|
||||
}*/
|
||||
}
|
||||
|
||||
initializer {
|
||||
EditViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository)
|
||||
|
@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.ExposedDropdownMenuBox
|
||||
@ -12,6 +13,7 @@ import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@ -19,19 +21,30 @@ import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import at.xaxa.ledger.data.Entry
|
||||
import at.xaxa.ledger.ui.AppViewModelProvider
|
||||
import at.xaxa.ledger.ui.DatePicker
|
||||
import at.xaxa.ledger.ui.home.HomeViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun Add(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, HomeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
fun Add(modifier: Modifier = Modifier, onCardClick: () -> Unit, addViewModel: AddViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
|
||||
val state by addViewModel.addUiState.entries.collectAsState(initial = emptyList())
|
||||
|
||||
var spending by remember { mutableStateOf("") }
|
||||
|
||||
var expanded by remember { mutableStateOf(false) }
|
||||
var selectedItem by remember { mutableStateOf("") }
|
||||
val categories = listOf("Option 1", "Option 2", "Option 3")
|
||||
|
||||
var entryName by remember { mutableStateOf("") }
|
||||
var entryAmount by remember { mutableStateOf(0.0f) }
|
||||
var entryDate by remember { mutableStateOf(0L) }
|
||||
var category by remember { mutableStateOf(0) }
|
||||
|
||||
|
||||
|
||||
|
||||
Column(
|
||||
modifier = modifier.fillMaxSize()
|
||||
@ -88,5 +101,26 @@ fun Add(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, HomeViewModel
|
||||
}
|
||||
|
||||
DatePicker(onDateSelected = { date -> println("Selected date in milliseconds: $date") }, onDismiss = { println("Dismissed") })
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
if (entryName.isNotBlank() && entryAmount != null && entryDate != null && categories != null) {
|
||||
val newEntry = Entry(
|
||||
id = 0,
|
||||
name = entryName,
|
||||
amount = entryAmount,
|
||||
date = entryDate,
|
||||
categoryID = category
|
||||
)
|
||||
|
||||
addViewModel.addEntryToDB(newEntry)
|
||||
|
||||
onCardClick()
|
||||
}
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Add Item")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,20 +26,17 @@ var categoryUiState by mutableStateOf(CategoryListUIState())
|
||||
private set
|
||||
|
||||
// UI State to hold a list of games
|
||||
data class EntryListUi(
|
||||
val entries: Flow<List<Entry>> = flow{emit(emptyList()) }
|
||||
data class EntryUi(
|
||||
val entries: Flow<List<Entry>> = flow { emit(emptyList()) }
|
||||
)
|
||||
|
||||
class AddViewModel(
|
||||
private val savedStateHandle: SavedStateHandle,
|
||||
private val repository: EntryRepository
|
||||
) : ViewModel() {
|
||||
var addUiState by mutableStateOf(EntryUi())
|
||||
|
||||
var addUiState by mutableStateOf(EntryListUi())
|
||||
private set
|
||||
|
||||
|
||||
fun addEntryToDB(entry: Entry){
|
||||
fun addEntryToDB(entry: Entry) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val entryEntity = Entry(
|
||||
@ -58,6 +55,7 @@ class AddViewModel(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun addCategory(category: CategoryEntity) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
@ -75,9 +73,10 @@ class AddViewModel(
|
||||
}
|
||||
}
|
||||
}
|
||||
fun getAllCategories(){
|
||||
|
||||
fun getAllCategories() {
|
||||
viewModelScope.launch {
|
||||
val categories = withContext(Dispatchers.IO){
|
||||
val categories = withContext(Dispatchers.IO) {
|
||||
repository.getAllCategories()
|
||||
}
|
||||
categoryUiState = CategoryListUIState(categories)
|
||||
@ -85,4 +84,4 @@ class AddViewModel(
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user