Skip to content

Instantly share code, notes, and snippets.

@widiarifki
Created August 30, 2021 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save widiarifki/da139b87dff102b52116a36e4af7f732 to your computer and use it in GitHub Desktop.
Save widiarifki/da139b87dff102b52116a36e4af7f732 to your computer and use it in GitHub Desktop.
@Entity(tableName = "tbl_shopping_item")
data class ShoppingItem(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
@ColumnInfo(name = "name")
var name: String,
@ColumnInfo(name = "is_ticked")
var isTicked: Boolean = false
)
@Dao
interface ShoppingItemDao {
@Query("SELECT * FROM $TBL_SHOPPING_ITEM")
fun getAll(): LiveData<List<ShoppingItem>>
@Insert
suspend fun insert(item: ShoppingItem)
@Update
suspend fun update(item: ShoppingItem)
@Delete
suspend fun delete(item: ShoppingItem)
}
class ShoppingItemRepository(
private val shoppingItemDao: ShoppingItemDao = AppDatabase.getInstance().shoppingItemDao()
) {
val allItems = shoppingItemDao.getAll()
suspend fun insert(item: ShoppingItem) {
shoppingItemDao.insert(item)
}
suspend fun delete(item: ShoppingItem) {
shoppingItemDao.delete(item)
}
suspend fun update(item: ShoppingItem) {
shoppingItemDao.update(item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment