Fixed recipe tags not being properly deleted and sorting recipes alphabetically

master
Wynd 2025-09-10 00:57:18 +03:00
parent 357cab1afa
commit 821c81cac2
4 changed files with 12 additions and 7 deletions

View File

@ -1,7 +1,6 @@
package xyz.pixelatedw.recipe
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
@ -16,7 +15,7 @@ import xyz.pixelatedw.recipe.data.AppDatabase
import xyz.pixelatedw.recipe.data.RecipesView
import xyz.pixelatedw.recipe.ui.components.MainScreen
import xyz.pixelatedw.recipe.ui.theme.RecipeTheme
import xyz.pixelatedw.recipe.utils.getRecipes
import xyz.pixelatedw.recipe.utils.parseRecipes
class MainActivity : ComponentActivity() {
private val recipeView: RecipesView by viewModels()
@ -50,7 +49,7 @@ class MainActivity : ComponentActivity() {
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
result.data?.data?.let { uri ->
getRecipes(this, db, uri)
parseRecipes(this, db, uri)
val recipes = db.recipeWithTagsDao().getAll()
recipeView.setRecipes(recipes)

View File

@ -41,9 +41,12 @@ interface RecipeWithTagsDao {
@Query("SELECT * FROM recipe")
fun getAll(): List<RecipeWithTags>
@Query("SELECT COUNT(*) FROM recipe WHERE recipe.title = :recipe")
fun count(recipe: String): Int
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(recipe: RecipeTag)
@Query("DELETE FROM recipe WHERE recipe.title = :recipe")
@Query("DELETE FROM recipetag WHERE recipetag.title = :recipe")
fun delete(recipe: String)
}

View File

@ -34,7 +34,7 @@ class RecipesView : ViewModel() {
}
fun setRecipes(recipes: List<RecipeWithTags>) {
_recipes.update { recipes }
_recipes.update { recipes.sortedBy { it.recipe.title } }
val filters = arrayListOf<TagFilter>()

View File

@ -16,7 +16,7 @@ import java.io.InputStreamReader
private val recipeFiles = mutableListOf<DocumentFile>()
fun getRecipes(ctx: Context, db: AppDatabase, uri: Uri?) {
fun parseRecipes(ctx: Context, db: AppDatabase, uri: Uri?) {
if (uri == null) {
return
}
@ -38,7 +38,7 @@ fun parseDir(ctx: Context, dir: DocumentFile, path: String) {
for (file in fileList) {
if (file.isDirectory) {
parseDir(ctx, file, path + File.separator + file.name)
continue;
continue
}
if (file.isFile && file.name?.endsWith(".jpg") == true) {
@ -109,6 +109,9 @@ private fun parseRecipe(ctx: Context, db: AppDatabase, file: DocumentFile) {
val tags = arrayListOf<Tag>()
if (doc.contains("tags")) {
// Delete already existing tags for this recipe
db.recipeWithTagsDao().delete(recipeTitle)
for (tomlElem in doc["tags"]!!.asArray()) {
val tag = Tag(tomlElem!!.asPrimitive().asString())
tags.add(tag)