From 821c81cac2b74ff1c91a2e13d9256df453d398f8 Mon Sep 17 00:00:00 2001 From: Wynd Date: Wed, 10 Sep 2025 00:57:18 +0300 Subject: [PATCH] Fixed recipe tags not being properly deleted and sorting recipes alphabetically --- app/src/main/java/xyz/pixelatedw/recipe/MainActivity.kt | 5 ++--- .../main/java/xyz/pixelatedw/recipe/data/RecipeWithTags.kt | 5 ++++- .../main/java/xyz/pixelatedw/recipe/data/RecipesView.kt | 2 +- .../main/java/xyz/pixelatedw/recipe/utils/RecipeParser.kt | 7 +++++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/xyz/pixelatedw/recipe/MainActivity.kt b/app/src/main/java/xyz/pixelatedw/recipe/MainActivity.kt index 3fc9dc8..a37f460 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/MainActivity.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/MainActivity.kt @@ -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) diff --git a/app/src/main/java/xyz/pixelatedw/recipe/data/RecipeWithTags.kt b/app/src/main/java/xyz/pixelatedw/recipe/data/RecipeWithTags.kt index 8cb532d..57a9263 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/data/RecipeWithTags.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/data/RecipeWithTags.kt @@ -41,9 +41,12 @@ interface RecipeWithTagsDao { @Query("SELECT * FROM recipe") fun getAll(): List + @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) } diff --git a/app/src/main/java/xyz/pixelatedw/recipe/data/RecipesView.kt b/app/src/main/java/xyz/pixelatedw/recipe/data/RecipesView.kt index 0ff669f..77c6009 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/data/RecipesView.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/data/RecipesView.kt @@ -34,7 +34,7 @@ class RecipesView : ViewModel() { } fun setRecipes(recipes: List) { - _recipes.update { recipes } + _recipes.update { recipes.sortedBy { it.recipe.title } } val filters = arrayListOf() diff --git a/app/src/main/java/xyz/pixelatedw/recipe/utils/RecipeParser.kt b/app/src/main/java/xyz/pixelatedw/recipe/utils/RecipeParser.kt index 24efe15..e5ea252 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/utils/RecipeParser.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/utils/RecipeParser.kt @@ -16,7 +16,7 @@ import java.io.InputStreamReader private val recipeFiles = mutableListOf() -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() 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)