recipe-kt/app/src/main/java/xyz/pixelatedw/recipe/data/Recipe.kt

50 lines
1.1 KiB
Kotlin
Raw Normal View History

2025-08-08 23:21:33 +03:00
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
2025-08-08 23:21:33 +03:00
data class Recipe(
@PrimaryKey
2025-08-08 23:21:33 +03:00
val title: String,
val preview: String?,
2025-08-08 23:21:33 +03:00
val lastModified: Long,
val content: String,
val hash: Int
) {
2025-08-10 02:07:45 +03:00
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 {
2025-08-10 02:07:45 +03:00
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
}