Recipes count for tag filters + filter text being used to activate the checkbox

master
Wynd 2025-09-27 00:17:27 +03:00
parent b3e8958c5f
commit 7e04c31ae3
3 changed files with 39 additions and 23 deletions

View File

@ -36,13 +36,18 @@ class RecipesView : ViewModel() {
fun setRecipes(recipes: List<RecipeWithTags>) {
_recipes.update { recipes.sortedBy { it.recipe.title } }
val filters = arrayListOf<TagFilter>()
val filtersMap = mutableMapOf<String, Int>()
_recipes.value.stream()
.filter { it.tags.isNotEmpty() }
.forEach {
it.tags.forEach {tag -> filters.add(TagFilter(tag.name))}
it.tags.forEach { tag ->
val count = filtersMap[tag.name] ?: 0
filtersMap[tag.name] = count + 1;
}
}
val filters = filtersMap.map { it -> TagFilter(it.key, count = it.value) }.toList()
_tagFilters.update { filters.distinct() }
}

View File

@ -1,4 +1,4 @@
package xyz.pixelatedw.recipe.data
data class TagFilter(val tag: String, var checked: Boolean = false) {
data class TagFilter(val tag: String, var checked: Boolean = false, var count: Int = 0) {
}

View File

@ -1,22 +1,15 @@
package xyz.pixelatedw.recipe.ui.components
import androidx.collection.arraySetOf
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Text
@ -25,17 +18,14 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
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.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import xyz.pixelatedw.recipe.data.RecipesView
import xyz.pixelatedw.recipe.data.TagFilter
@Composable
fun TagFilterDialog(onAccept: () -> Unit, view: RecipesView) {
@ -54,24 +44,45 @@ fun TagFilterDialog(onAccept: ( ) -> Unit, view: RecipesView) {
modifier = Modifier
.fillMaxHeight(0.9f)
.padding(start = 16.dp, end = 16.dp, bottom = 16.dp)
.wrapContentSize(Alignment.TopStart)) {
.wrapContentSize(Alignment.TopStart)
) {
items(filters.value) { tag ->
// TODO This doesn't really feel right lmao, but for now it works
val filterChecked = remember { mutableStateOf(tag.checked) }
Row(verticalAlignment = Alignment.CenterVertically) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(checked = filterChecked.value, onCheckedChange = {
filterChecked.value = !tag.checked
view.setTagFilterState(tag.tag, !tag.checked)
})
Text(tag.tag)
TextButton(
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.buttonColors(
contentColor = Color.White,
containerColor = Color.Transparent
),
onClick = {
filterChecked.value = !tag.checked
view.setTagFilterState(tag.tag, !tag.checked)
}
) {
Text(
modifier = Modifier.fillMaxWidth(),
text = tag.tag + " (" + tag.count + ")",
textAlign = TextAlign.Left
)
}
}
}
}
Row(modifier = Modifier
Row(
modifier = Modifier
.fillMaxWidth()
.padding(end = 16.dp), horizontalArrangement = Arrangement.End) {
.padding(end = 16.dp), horizontalArrangement = Arrangement.End
) {
TextButton(onClick = onAccept) {
Text("Done")
}