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

42 lines
895 B
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.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
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
) {
fun mainImage(ctx: Context): Bitmap? {
if (this.preview != null) {
val file = File(ctx.filesDir, this.preview)
if (file.exists()) {
ctx.contentResolver.openInputStream(file.toUri()).use {
val bitmap: Bitmap? = BitmapFactory.decodeStream(it)
return bitmap
}
}
}
return null
}
}
@Dao
interface RecipeDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(recipe: Recipe)
}