Preventing some useless reads and writes when recipes already exist and the md file is the same
parent
821c81cac2
commit
b3e8958c5f
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue