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.Insert
|
||||||
import androidx.room.OnConflictStrategy
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
|
import androidx.room.Query
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
|
@ -18,7 +19,8 @@ data class Recipe(
|
||||||
val title: String,
|
val title: String,
|
||||||
val preview: String?,
|
val preview: String?,
|
||||||
val lastModified: Long,
|
val lastModified: Long,
|
||||||
val content: String
|
val content: String,
|
||||||
|
val hash: Int
|
||||||
) {
|
) {
|
||||||
fun previewImage(ctx: Context): Bitmap? {
|
fun previewImage(ctx: Context): Bitmap? {
|
||||||
if (this.preview != null) {
|
if (this.preview != null) {
|
||||||
|
|
@ -41,4 +43,7 @@ interface RecipeDao {
|
||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
fun delete(recipe: Recipe)
|
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(),
|
bitmap = previewUri.asImageBitmap(),
|
||||||
contentDescription = "Recipe image",
|
contentDescription = "Recipe image",
|
||||||
contentScale = ContentScale.Crop,
|
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(
|
||||||
text = entry.recipe.title,
|
text = entry.recipe.title,
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,12 @@ private fun parseRecipe(ctx: Context, db: AppDatabase, file: DocumentFile) {
|
||||||
val inputStream = ctx.contentResolver.openInputStream(file.uri)
|
val inputStream = ctx.contentResolver.openInputStream(file.uri)
|
||||||
val reader = BufferedReader(InputStreamReader(inputStream))
|
val reader = BufferedReader(InputStreamReader(inputStream))
|
||||||
val text = reader.readText()
|
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 lines = text.lines()
|
||||||
|
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
|
|
@ -133,12 +139,11 @@ private fun parseRecipe(ctx: Context, db: AppDatabase, file: DocumentFile) {
|
||||||
title = recipeTitle,
|
title = recipeTitle,
|
||||||
preview = recipePreview,
|
preview = recipePreview,
|
||||||
lastModified = lastModified,
|
lastModified = lastModified,
|
||||||
content = content
|
content = content,
|
||||||
|
hash = hash
|
||||||
)
|
)
|
||||||
|
|
||||||
db.recipeDao().insert(recipe)
|
db.recipeDao().insert(recipe)
|
||||||
|
|
||||||
println(recipe)
|
|
||||||
|
|
||||||
reader.close()
|
reader.close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue