CategoryUI updated

This commit is contained in:
Florian 2025-01-15 15:26:52 +01:00
parent b32cfe1844
commit adcd8bb3a0
3 changed files with 55 additions and 8 deletions

View File

@ -141,6 +141,27 @@ fun HorizontalCard(modifier: Modifier = Modifier, name: String, date: String, am
} }
} }
@Composable
fun CategoryCard(modifier: Modifier = Modifier, name: String, icon : Int, onClick: () -> Unit ) {
Surface(
onClick = onClick,
shape = RoundedCornerShape(12.dp),
color = Color(0xfff9f9f9),
border = BorderStroke(1.dp, Color(0xffc6c6c6)),
modifier = modifier
.clip(shape = RoundedCornerShape(12.dp))
) {
Row(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
LayoutMediaText(modifier, name, date, amount)
}
}
}
@Composable @Composable
fun LayoutMediaText(modifier: Modifier = Modifier, name: String, date: String, amount:String) { fun LayoutMediaText(modifier: Modifier = Modifier, name: String, date: String, amount:String) {
Row( Row(

View File

@ -5,6 +5,8 @@ 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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text import androidx.compose.material3.Text
@ -21,6 +23,9 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Category.CategoryEntity
import at.xaxa.ledger.ui.AppViewModelProvider import at.xaxa.ledger.ui.AppViewModelProvider
import at.xaxa.ledger.ui.ButtonSuccess import at.xaxa.ledger.ui.ButtonSuccess
import at.xaxa.ledger.ui.CategoryCard
import at.xaxa.ledger.ui.HorizontalCard
import at.xaxa.ledger.ui.convertMillisToDate
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
@ -33,7 +38,7 @@ fun addCategory(
var icon by remember { mutableStateOf(0) } var icon by remember { mutableStateOf(0) }
val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList()) val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
val category = categoryViewModel.categoryUi.category
var expanded by remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
@ -43,25 +48,23 @@ fun addCategory(
.padding(16.dp, 0.dp), .padding(16.dp, 0.dp),
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
Column( LazyColumn(
Modifier.weight(1f) Modifier.weight(1f)
) { ) {
OutlinedTextField( OutlinedTextField(
value = name, value = category.categoryName,
onValueChange = { name = it }, onValueChange = { name = it },
label = { Text("Name") }, label = { Text("Name") },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
) )
OutlinedTextField( OutlinedTextField(
value = icon.toString(), value = category.icon.toString(),
onValueChange = { icon = it.toInt() }, onValueChange = { category.copy(icon = it.toInt()) },
label = { Text("Icon") }, label = { Text("Icon") },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
) )
Box( Box(
modifier = Modifier modifier = Modifier
.fillMaxWidth(), .fillMaxWidth(),
@ -83,6 +86,19 @@ fun addCategory(
} }
) )
} }
items(categories) { item ->
Column(
modifier = Modifier.padding(vertical = 4.dp)
) {
CategoryCard(
modifier = modifier,
name = category.categoryName,
icon = category.icon,
onClick = { onCardClick(category._id) }
)
}
}
} }
} }
} }

View File

@ -16,7 +16,7 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
data class CategoryUIState(val category: CategoryEntity = CategoryEntity(0,"",0))
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList())) data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
@ -25,6 +25,8 @@ class CategoryViewModel(
private val repository: EntryRepository private val repository: EntryRepository
) : ViewModel() { ) : ViewModel() {
var categoryUiState by mutableStateOf(CategoryListUIState()) var categoryUiState by mutableStateOf(CategoryListUIState())
var categoryUi by mutableStateOf(CategoryUIState())
private set
init { init {
getAllCategories() getAllCategories()
@ -56,6 +58,14 @@ class CategoryViewModel(
categoryUiState = CategoryListUIState(categories) categoryUiState = CategoryListUIState(categories)
} }
}
fun findCategoryByID(categoryId: Int) {
viewModelScope.launch {
val category = withContext(Dispatchers.IO) {
repository.findCategoryById(categoryId)
}
categoryUi = CategoryUIState(category)
}
} }
} }