addCategory added and semifunctional

This commit is contained in:
Florian 2025-01-15 14:52:42 +01:00
parent 6e17aff90d
commit f585e0c01c
7 changed files with 135 additions and 8 deletions

View File

@ -2,7 +2,7 @@ package at.xaxa.ledger.data
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
class Entry ( data class Entry (
val id: Int, val id: Int,
val name: String, val name: String,
val amount: Float, val amount: Float,

View File

@ -6,6 +6,7 @@ import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory import androidx.lifecycle.viewmodel.viewModelFactory
import at.xaxa.ledger.LedgerApplication import at.xaxa.ledger.LedgerApplication
import at.xaxa.ledger.ui.add.AddViewModel import at.xaxa.ledger.ui.add.AddViewModel
import at.xaxa.ledger.ui.category.CategoryViewModel
import at.xaxa.ledger.ui.edit.EditViewModel import at.xaxa.ledger.ui.edit.EditViewModel
import at.xaxa.ledger.ui.home.HomeViewModel import at.xaxa.ledger.ui.home.HomeViewModel
@ -23,5 +24,8 @@ object AppViewModelProvider {
initializer { initializer {
EditViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository) EditViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository)
} }
initializer {
CategoryViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository)
}
} }
} }

View File

@ -13,13 +13,15 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument import androidx.navigation.navArgument
import at.xaxa.ledger.ui.add.Add import at.xaxa.ledger.ui.add.Add
import at.xaxa.ledger.ui.category.addCategory
import at.xaxa.ledger.ui.edit.Edit import at.xaxa.ledger.ui.edit.Edit
import at.xaxa.ledger.ui.home.Home import at.xaxa.ledger.ui.home.Home
enum class AppRoutes(val route: String) { enum class AppRoutes(val route: String) {
Home("home"), Home("home"),
Add("add"), Add("add"),
Edit("edit/{entryId}") Edit("edit/{entryId}"),
Category("category")
} }
@Composable @Composable
@ -41,6 +43,9 @@ fun LedgerApp(modifier: Modifier = Modifier){
}, },
onButtonClick = { onButtonClick = {
navController.navigate("add") navController.navigate("add")
},
onCatButtonClick = {
navController.navigate("category")
} }
) )
} }
@ -64,6 +69,14 @@ fun LedgerApp(modifier: Modifier = Modifier){
} }
) )
} }
composable(AppRoutes.Category.route){
addCategory(
onButtonClick = {
navController.navigate("home")
}
)
} }
} }
} }
}

View File

@ -1,2 +1,92 @@
package at.xaxa.ledger.ui.category package at.xaxa.ledger.ui.category
import androidx.compose.foundation.layout.Box
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.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
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
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import at.xaxa.ledger.data.db.Category.CategoryEntity
import at.xaxa.ledger.ui.AppViewModelProvider
import at.xaxa.ledger.ui.ButtonSuccess
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun addCategory(
onButtonClick: () -> Unit,
modifier: Modifier = Modifier,
categoryViewModel: CategoryViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
var name by remember { mutableStateOf("") }
var icon by remember { mutableStateOf(0) }
val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
var expanded by remember { mutableStateOf(false) }
Column(
modifier = modifier
.fillMaxSize()
.padding(16.dp, 0.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Column(
Modifier.weight(1f)
) {
OutlinedTextField(
value = name,
onValueChange = { name = it },
label = { Text("Name") },
modifier = Modifier
.fillMaxWidth()
)
OutlinedTextField(
value = icon.toString(),
onValueChange = { icon = it.toInt() },
label = { Text("Icon") },
modifier = Modifier
.fillMaxWidth()
)
Box(
modifier = Modifier
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
ButtonSuccess(
modifier = Modifier,
"Add Category",
onClick = {
if (name.isNotBlank()) {
val newCategory = CategoryEntity(
_id = 0,
categoryName = name,
icon = icon
)
categoryViewModel.addCategory(newCategory)
onButtonClick()
}
}
)
}
}
}
}

View File

@ -23,6 +23,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import at.xaxa.ledger.data.Entry
import at.xaxa.ledger.ui.AppViewModelProvider import at.xaxa.ledger.ui.AppViewModelProvider
import at.xaxa.ledger.ui.ButtonDanger import at.xaxa.ledger.ui.ButtonDanger
import at.xaxa.ledger.ui.ButtonSuccess import at.xaxa.ledger.ui.ButtonSuccess
@ -30,7 +31,8 @@ import at.xaxa.ledger.ui.DatePickerDocked
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory)) { fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel : EditViewModel = viewModel(factory = AppViewModelProvider.Factory), onValueChange: (Entry) -> Unit = {},
) {
val entry = editViewModel.editUiState.entry val entry = editViewModel.editUiState.entry
val categories by editViewModel.categoryUiState.categories.collectAsState(initial = emptyList()) val categories by editViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
@ -57,8 +59,10 @@ fun Edit(modifier: Modifier = Modifier, onCardClick: () -> Unit, editViewModel :
Modifier.weight(1f) Modifier.weight(1f)
) { ) {
OutlinedTextField( OutlinedTextField(
value = name, value = entry.name,
onValueChange = { name = it }, onValueChange = { newText ->
onValueChange(entry.copy(name = newText))
},
label = { Text("Name") }, label = { Text("Name") },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -21,7 +21,7 @@ data class CategoryUIState(val categories: CategoryEntity = CategoryEntity(0,"",
data class EditUI( data class EditUI(
val entry: Entry = Entry(0, "", 0.0f, 0, 0) val entry: Entry = Entry(0, "AS", 0.0f, 0, 0)
) )
class EditViewModel(private val savedStateHandle: SavedStateHandle, class EditViewModel(private val savedStateHandle: SavedStateHandle,
@ -31,6 +31,7 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
var editUiState by mutableStateOf(EditUI()) var editUiState by mutableStateOf(EditUI())
private set
var categoryUiState by mutableStateOf(CategoryListUIState()) var categoryUiState by mutableStateOf(CategoryListUIState())
var categoryUi by mutableStateOf(CategoryUIState()) var categoryUi by mutableStateOf(CategoryUIState())

View File

@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -23,7 +24,7 @@ import at.xaxa.ledger.ui.convertMillisToDate
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClick: () -> Unit, HomeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) { 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()) val state by HomeViewModel.entryUIState.entry.collectAsState(initial = emptyList())
Column( Column(
@ -39,6 +40,20 @@ fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClic
) { ) {
stickyHeader { stickyHeader {
HeaderCard(modifier = modifier, "-13563.00€") HeaderCard(modifier = modifier, "-13563.00€")
Box(
modifier = Modifier
.fillMaxWidth()
.padding(top = 8.dp), // Add slight padding from the header
contentAlignment = Alignment.TopStart // Align button to the top-left
) {
ButtonSuccess(
modifier = Modifier
.width(120.dp) // Make the button smaller
.padding(4.dp), // Add some padding for better touch area
text = "Add Category",
onClick = { onCatButtonClick() }
)
}
} }
items(state) { item -> items(state) { item ->
Column( Column(