Created
August 30, 2021 07:18
-
-
Save widiarifki/da139b87dff102b52116a36e4af7f732 to your computer and use it in GitHub Desktop.
This gist is part of https://github.com/widiarifki/android-playground/tree/master/compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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