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