50 lines
1.1 KiB
Kotlin
50 lines
1.1 KiB
Kotlin
package xyz.pixelatedw.recipe.data
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import android.graphics.BitmapFactory
|
|
import androidx.core.net.toUri
|
|
import androidx.room.Dao
|
|
import androidx.room.Delete
|
|
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
|
|
data class Recipe(
|
|
@PrimaryKey
|
|
val title: String,
|
|
val preview: String?,
|
|
val lastModified: Long,
|
|
val content: String,
|
|
val hash: Int
|
|
) {
|
|
fun previewImage(ctx: Context): Bitmap? {
|
|
if (this.preview != null) {
|
|
val file = File(ctx.filesDir, this.preview)
|
|
if (file.exists()) {
|
|
ctx.contentResolver.openInputStream(file.toUri()).use {
|
|
return BitmapFactory.decodeStream(it)
|
|
}
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|
|
}
|
|
|
|
@Dao
|
|
interface RecipeDao {
|
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
|
fun insert(recipe: Recipe)
|
|
|
|
@Delete
|
|
fun delete(recipe: Recipe)
|
|
|
|
@Query("SELECT EXISTS(SELECT hash FROM recipe WHERE recipe.hash = :hash LIMIT 1)")
|
|
fun hasHash(hash: Int): Boolean
|
|
}
|