2025-08-08 23:21:33 +03:00
|
|
|
package xyz.pixelatedw.recipe.data
|
|
|
|
|
|
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.asStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.update
|
|
|
|
|
|
|
|
|
|
class RecipesView : ViewModel() {
|
2025-08-09 18:09:09 +03:00
|
|
|
private val _activeRecipe = MutableStateFlow<RecipeWithTags?>( null )
|
2025-08-08 23:21:33 +03:00
|
|
|
val activeRecipe = _activeRecipe.asStateFlow()
|
|
|
|
|
|
2025-08-09 18:09:09 +03:00
|
|
|
private val _recipes = MutableStateFlow<List<RecipeWithTags>>( arrayListOf() )
|
2025-08-08 23:21:33 +03:00
|
|
|
val recipes = _recipes.asStateFlow()
|
|
|
|
|
|
2025-09-08 01:48:54 +03:00
|
|
|
private val _tagFilters = MutableStateFlow<List<TagFilter>>( arrayListOf() )
|
|
|
|
|
val tagFilters = _tagFilters.asStateFlow()
|
|
|
|
|
|
2025-08-09 13:21:06 +03:00
|
|
|
private val _search = MutableStateFlow<String?>(null)
|
|
|
|
|
val search = _search.asStateFlow()
|
|
|
|
|
|
2025-08-31 01:11:04 +03:00
|
|
|
private val _keepScreenOn = MutableStateFlow<Boolean>(false)
|
|
|
|
|
val keepScreenOn = _keepScreenOn.asStateFlow()
|
|
|
|
|
|
2025-09-08 14:04:33 +03:00
|
|
|
fun reloadTagFilterState() {
|
|
|
|
|
_tagFilters.value = _tagFilters.value
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-08 01:48:54 +03:00
|
|
|
fun setTagFilterState(tag: String, state: Boolean) {
|
|
|
|
|
_tagFilters.value = _tagFilters.value.toMutableList().apply { replaceAll { it -> if (tag == it.tag) it.checked = state; it } }.toList()
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 01:11:04 +03:00
|
|
|
fun setKeepScreenOn(flag: Boolean) {
|
|
|
|
|
_keepScreenOn.update { flag }
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 18:09:09 +03:00
|
|
|
fun setRecipes(recipes: List<RecipeWithTags>) {
|
2025-09-10 00:57:18 +03:00
|
|
|
_recipes.update { recipes.sortedBy { it.recipe.title } }
|
2025-09-08 01:48:54 +03:00
|
|
|
|
2025-09-27 00:17:27 +03:00
|
|
|
val filtersMap = mutableMapOf<String, Int>()
|
2025-09-08 01:48:54 +03:00
|
|
|
|
|
|
|
|
_recipes.value.stream()
|
|
|
|
|
.filter { it.tags.isNotEmpty() }
|
|
|
|
|
.forEach {
|
2025-09-27 00:17:27 +03:00
|
|
|
it.tags.forEach { tag ->
|
|
|
|
|
val count = filtersMap[tag.name] ?: 0
|
|
|
|
|
filtersMap[tag.name] = count + 1;
|
|
|
|
|
}
|
2025-09-08 01:48:54 +03:00
|
|
|
}
|
|
|
|
|
|
2025-09-27 00:17:27 +03:00
|
|
|
val filters = filtersMap.map { it -> TagFilter(it.key, count = it.value) }.toList()
|
|
|
|
|
|
2025-09-08 01:48:54 +03:00
|
|
|
_tagFilters.update { filters.distinct() }
|
2025-08-09 13:21:06 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-31 01:11:04 +03:00
|
|
|
fun removeRecipe(recipe: RecipeWithTags) {
|
|
|
|
|
_recipes.value = _recipes.value.toMutableList().apply { remove(recipe) }.toList()
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 18:09:09 +03:00
|
|
|
fun setSearch(search: String) {
|
|
|
|
|
_search.update { search }
|
2025-08-08 23:21:33 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-09 18:09:09 +03:00
|
|
|
fun setActive(recipe: RecipeWithTags) {
|
2025-08-08 23:21:33 +03:00
|
|
|
_activeRecipe.update { recipe }
|
|
|
|
|
}
|
|
|
|
|
}
|