diff --git a/app/src/main/java/xyz/pixelatedw/recipe/data/Recipe.kt b/app/src/main/java/xyz/pixelatedw/recipe/data/Recipe.kt index 5b62da3..f966765 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/data/Recipe.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/data/Recipe.kt @@ -10,6 +10,7 @@ import androidx.room.Entity import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.PrimaryKey +import androidx.room.Query import java.io.File @Entity @@ -18,7 +19,8 @@ data class Recipe( val title: String, val preview: String?, val lastModified: Long, - val content: String + val content: String, + val hash: Int ) { fun previewImage(ctx: Context): Bitmap? { if (this.preview != null) { @@ -41,4 +43,7 @@ interface RecipeDao { @Delete fun delete(recipe: Recipe) + + @Query("SELECT EXISTS(SELECT hash FROM recipe WHERE recipe.hash = :hash LIMIT 1)") + fun hasHash(hash: Int): Boolean } diff --git a/app/src/main/java/xyz/pixelatedw/recipe/ui/components/RecipePreview.kt b/app/src/main/java/xyz/pixelatedw/recipe/ui/components/RecipePreview.kt index 8000043..3128392 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/ui/components/RecipePreview.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/ui/components/RecipePreview.kt @@ -33,11 +33,11 @@ fun RecipePreview(entry: RecipeWithTags, previewUri: Bitmap?, onClick: () -> Uni bitmap = previewUri.asImageBitmap(), contentDescription = "Recipe image", contentScale = ContentScale.Crop, - modifier = Modifier.size(256.dp).padding(top = 16.dp, bottom = 16.dp), + modifier = Modifier.size(256.dp).padding(top = 16.dp, bottom = 8.dp), ) } } - Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth()) { + Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp)) { Text( text = entry.recipe.title, modifier = Modifier.fillMaxWidth(), 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 e5ea252..f3fdfe2 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/utils/RecipeParser.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/utils/RecipeParser.kt @@ -65,6 +65,12 @@ private fun parseRecipe(ctx: Context, db: AppDatabase, file: DocumentFile) { val inputStream = ctx.contentResolver.openInputStream(file.uri) val reader = BufferedReader(InputStreamReader(inputStream)) val text = reader.readText() + + val hash = text.hashCode() // Probably not the best way but its stable and works + if (db.recipeDao().hasHash(hash)) { + return + } + val lines = text.lines() val sb = StringBuilder() @@ -133,12 +139,11 @@ private fun parseRecipe(ctx: Context, db: AppDatabase, file: DocumentFile) { title = recipeTitle, preview = recipePreview, lastModified = lastModified, - content = content + content = content, + hash = hash ) db.recipeDao().insert(recipe) - println(recipe) - reader.close() }