@Dao
interface TodoDAO {
@Query("SELECT * from Todo where status = 'BEFORE'")
fun getTodoBeforeList(): LiveData<List<TodoModel>>
@Insert
fun insertTodo(todoModel: TodoModel)
@Query("SELECT * from Todo where status = 'MIDDLE'")
fun getTodoMiddleList(): LiveData<List<TodoModel>>
@Query("UPDATE Todo SET status = 'MIDDLE' WHERE status = 'BEFORE' and id = :todoModelID")
fun updateTodoBeforeToMiddle(todoModelID: Long?)
@Query("UPDATE Todo SET status = 'AFTER' WHERE status = 'MIDDLE' and id = :todoModelID")
fun updateTodoMiddleToAfter(todoModelID: Long?)
@Query("SELECT * from Todo where status = 'AFTER'")
fun getTodoAfterList(): LiveData<List<TodoModel>>
//@Query("DELETE FROM Todo where id = :todoModelID")
@Delete
fun deleteTodo(todoModel: TodoModel)
}
@Database(entities = [TodoModel::class], version = 1)
abstract class TodoDatabase: RoomDatabase() {
abstract fun todoDao(): TodoDAO
companion object {
@Volatile
private var INSTANCE: TodoDatabase? = null
fun getInstance(context: Context): TodoDatabase = INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(
context.applicationContext,
TodoDatabase::class.java, "Todo.db"
).build()
}
}
@Entity(tableName = "Todo")
data class TodoModel (
@PrimaryKey(autoGenerate = true)
var id: Long?,
@ColumnInfo(name = "description")
var description: String,
@ColumnInfo(name = "status")
var status: String
) { constructor(): this(null, "","")
}
class TodoRepository(application: Application) {
private var mTodoDatabase: TodoDatabase
private var mTodoDAO: TodoDAO
private var mTodoItems: LiveData<List<TodoModel>>
private var mTodoItemsMiddle: LiveData<List<TodoModel>>
private var mTodoItemsAfter: LiveData<List<TodoModel>>
init {
mTodoDatabase = TodoDatabase.getInstance(application)
mTodoDAO = mTodoDatabase.todoDao()
mTodoItems = mTodoDAO.getTodoBeforeList()
mTodoItemsMiddle = mTodoDAO.getTodoMiddleList()
mTodoItemsAfter = mTodoDAO.getTodoAfterList()
}
fun getTodoBeforeList(): LiveData<List<TodoModel>> {
return mTodoItems
}
fun getTodoMiddleList(): LiveData<List<TodoModel>> {
return mTodoItemsMiddle
}
fun getTodoAfterList(): LiveData<List<TodoModel>> {
return mTodoItemsAfter
}
fun insertTodo(todoModel: TodoModel) {
Thread{
mTodoDAO.insertTodo(todoModel)
}.start()
}
fun deleteTodo(todoModel: TodoModel) {
Thread{
mTodoDAO.deleteTodo(todoModel)
}.start()
}
fun updateTodoBeforeToMiddle(todoModelID: Long?) {
Thread{
mTodoDAO.updateTodoBeforeToMiddle(todoModelID)
}.start()
}
fun updateTodoMiddleToAfter(todoModelID: Long?) {
Thread{
mTodoDAO.updateTodoMiddleToAfter(todoModelID)
}.start()
}
}
class TodoViewModel(application: Application) : AndroidViewModel(application) {
private val mTodoRepository: TodoRepository
private var mTodoItems: LiveData<List<TodoModel>>
private var mTodoItemsMiddle: LiveData<List<TodoModel>>
private var mTodoItemsAfter: LiveData<List<TodoModel>>
val text = MutableLiveData("")
val addBtnClickEvent = SingleLiveEvent<Any>()
init {
mTodoRepository = TodoRepository(application)
mTodoItems = mTodoRepository.getTodoBeforeList()
mTodoItemsMiddle = mTodoRepository.getTodoMiddleList()
mTodoItemsAfter = mTodoRepository.getTodoAfterList()
}
fun insertTodo(todoModel: TodoModel) {
mTodoRepository.insertTodo(todoModel)
}
fun deleteTodo(todoModel: TodoModel) {
mTodoRepository.deleteTodo(todoModel)
}
fun getTodoBeforeList(): LiveData<List<TodoModel>> {
return mTodoItems
}
fun getTodoMiddleList(): LiveData<List<TodoModel>> {
return mTodoItemsMiddle
}
fun getTodoAfterList(): LiveData<List<TodoModel>> {
return mTodoItemsAfter
}
fun updateTodoBeforeToMiddle(todoModelID: Long?) {
mTodoRepository.updateTodoBeforeToMiddle(todoModelID)
}
fun updateTodoMiddleToAfter(todoModelID: Long?) {
mTodoRepository.updateTodoMiddleToAfter(todoModelID)
}
fun onClickAddButton() {
System.out.println("onClickAddButton")
addBtnClickEvent.call()
}
fun deleteText() {
text.value = null
}
}
SingleLiveEvent에 대한 것은 다른 포스팅 참조
https://velog.io/@dustndus8/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9CAndroid-MVVM-%EC%9D%B4%EB%B2%A4%ED%8A%B8-%EC%B2%98%EB%A6%AC-ovw9fsa1
ROOM 이용해 TodoList 만들기 참조 출처 : https://doitddo.tistory.com/52