.
This commit is contained in:
parent
a3cb3bfa7c
commit
d22ebcd162
@ -1,5 +1,6 @@
|
||||
package at.xaxa.ledger.ui.add
|
||||
|
||||
import android.util.Log
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
@ -15,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
|
||||
@ -30,15 +32,20 @@ import at.xaxa.ledger.ui.home.HomeViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun Add(modifier: Modifier = Modifier, onCardClick: () -> Unit, addViewModel: AddViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
fun Add(
|
||||
modifier: Modifier = Modifier,
|
||||
onCardClick: () -> Unit,
|
||||
addViewModel: AddViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
||||
) {
|
||||
var name by remember { mutableStateOf("") }
|
||||
val state by addViewModel.addUiState.entries.collectAsState(initial = emptyList())
|
||||
val categories by addViewModel.categoryUiState.categories.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 selectedCategory by remember { mutableIntStateOf(-1) }
|
||||
|
||||
var entryName by remember { mutableStateOf("") }
|
||||
var entryAmount by remember { mutableStateOf(0.0f) }
|
||||
@ -49,7 +56,8 @@ fun Add(modifier: Modifier = Modifier, onCardClick: () -> Unit, addViewModel: Ad
|
||||
|
||||
|
||||
Column(
|
||||
modifier = modifier.fillMaxSize()
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp, 0.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
@ -96,9 +104,10 @@ fun Add(modifier: Modifier = Modifier, onCardClick: () -> Unit, addViewModel: Ad
|
||||
) {
|
||||
categories.forEach { item ->
|
||||
DropdownMenuItem(
|
||||
text = { Text(text = item) },
|
||||
text = { Text(text = item.categoryName) },
|
||||
onClick = {
|
||||
selectedItem = item
|
||||
selectedItem = item.categoryName
|
||||
selectedCategory = item._id
|
||||
expanded = false
|
||||
}
|
||||
)
|
||||
@ -106,7 +115,7 @@ fun Add(modifier: Modifier = Modifier, onCardClick: () -> Unit, addViewModel: Ad
|
||||
}
|
||||
}
|
||||
|
||||
DatePickerDocked()
|
||||
entryDate = DatePickerDocked()
|
||||
}
|
||||
|
||||
Box(
|
||||
@ -114,22 +123,29 @@ fun Add(modifier: Modifier = Modifier, onCardClick: () -> Unit, addViewModel: Ad
|
||||
.fillMaxWidth(),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
ButtonSuccess(modifier = Modifier, "Add Transaction",
|
||||
ButtonSuccess(
|
||||
modifier = Modifier,
|
||||
"Add Transaction",
|
||||
onClick = {
|
||||
if (entryName.isNotBlank() && entryAmount != null && entryDate != null && categories != null) {
|
||||
Log.d("Kraushosdsdddad", "entryName " +entryName)
|
||||
Log.d("Kraushosdsdddad", "entryAmount " +entryAmount)
|
||||
Log.d("Kraushosdsdddad", "entryDate " +entryDate)
|
||||
Log.d("Kraushosdsdddad", "category " +category)
|
||||
|
||||
|
||||
if (name.isNotBlank() && spending.toFloat() != 0f && entryDate != 0L && selectedCategory != -1) {
|
||||
Log.d("Kraushosdsdddad", "BITTTTEEEEEE")
|
||||
val newEntry = Entry(
|
||||
id = 0,
|
||||
name = entryName,
|
||||
amount = entryAmount,
|
||||
date = entryDate,
|
||||
categoryID = category
|
||||
categoryID = selectedCategory
|
||||
)
|
||||
|
||||
addViewModel.addEntryToDB(newEntry)
|
||||
|
||||
onCardClick()
|
||||
}
|
||||
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package at.xaxa.ledger.ui.add
|
||||
|
||||
import android.os.Debug
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
@ -19,11 +21,9 @@ import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.math.log
|
||||
|
||||
data class CategoryListUIState(val entry: Flow<List<CategoryEntity>> = flowOf(emptyList()))
|
||||
|
||||
var categoryUiState by mutableStateOf(CategoryListUIState())
|
||||
private set
|
||||
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
|
||||
|
||||
// UI State to hold a list of games
|
||||
data class EntryUi(
|
||||
@ -35,8 +35,14 @@ class AddViewModel(
|
||||
private val repository: EntryRepository
|
||||
) : ViewModel() {
|
||||
var addUiState by mutableStateOf(EntryUi())
|
||||
var categoryUiState by mutableStateOf(CategoryListUIState())
|
||||
|
||||
init {
|
||||
getAllCategories()
|
||||
}
|
||||
|
||||
fun addEntryToDB(entry: Entry) {
|
||||
//Log.d("KRAUSI", "WE ARE INNNN")
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val entryEntity = Entry(
|
||||
|
Loading…
Reference in New Issue
Block a user