Skip to content
This repository has been archived by the owner. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.example.epicdatabaseexample.db.note.NoteDao
import com.example.epicdatabaseexample.db.note.NoteEntity

@Database(
version = 2,
version = 3,
exportSchema = false,
entities = [
NoteEntity::class,
Expand All @@ -39,16 +39,27 @@ abstract class NoteDatabase : RoomDatabase() {
private fun migrationList() = arrayOf(
object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE $PERSON_TABLE (" +
"${PersonEntity.COLUMN_NAME} TEXT NOT NULL," +
"PRIMARY KEY(${PersonEntity.COLUMN_NAME})" +
")")
database.execSQL("ALTER TABLE $NOTE_TABLE " +
"ADD COLUMN ${NoteEntity.COLUMN_PERSON_NAME} TEXT NOT NULL " +
"DEFAULT ('${PersonEntity.DEFAULT_PERSON_NAME}')")
database.execSQL(
"CREATE TABLE $PERSON_TABLE (" +
"${PersonEntity.COLUMN_NAME} TEXT NOT NULL," +
"PRIMARY KEY(${PersonEntity.COLUMN_NAME})" +
")"
)
database.execSQL(
"ALTER TABLE $NOTE_TABLE " +
"ADD COLUMN ${NoteEntity.COLUMN_PERSON_NAME} TEXT NOT NULL " +
"DEFAULT ('${PersonEntity.DEFAULT_PERSON_NAME}')"
)
}
},
// TODO Добавить миграцию.
object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"ALTER TABLE $NOTE_TABLE " +
"ADD COLUMN ${NoteEntity.COLUMN_IS_COMPLETED} INTEGER NOT NULL DEFAULT 0"
)
}
}
)

private fun createDatabaseInstance(application: Application): NoteDatabase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ abstract class NoteDao {
@Query("DELETE FROM note_table")
abstract fun deleteAllNote(): Completable

// TODO Добавить метод для обновления одного элемента.
// Что можно указать в качестве возвращаемого типа?
// Напомню, что выше есть метод observeNoteList, который будет вызываться при любом
// изменении списка в Базе Данных.
@Update
abstract fun updateNote(note: NoteEntity): Completable

@Query("UPDATE note_table SET is_completed = :isCompleted where id = :noteId")
abstract fun setCompleted(noteId: Int, isCompleted: Boolean): Completable
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ data class NoteEntity(
@ColumnInfo(name = COLUMN_PERSON_NAME)
var personName: String = PersonEntity.DEFAULT_PERSON_NAME,

// TODO Нужно добавить нове поле isCompleted типа Boolean.
// В SQLite не поддерживается Boolean, но в Room его можно использовать.
// Поэтому поле делаем именно Boolean, а при добавлении миграции - нужно будет погуглить,
// как обойти это ограничение.
@ColumnInfo(name = COLUMN_IS_COMPLETED)
var isCompleted: Boolean = false
) {

companion object {
const val COLUMN_ID = "id"
const val COLUMN_TITLE = "title"
const val COLUMN_DESCRIPTION = "descriptions"
const val COLUMN_PERSON_NAME = "person_name"
const val COLUMN_IS_COMPLETED = "is_completed"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ class NoteDiffUtilCallback : DiffUtil.ItemCallback<NoteEntity>() {
}

override fun areContentsTheSame(oldItem: NoteEntity, newItem: NoteEntity): Boolean {
return false
return oldItem == newItem
}

override fun getChangePayload(oldItem: NoteEntity, newItem: NoteEntity): Any? {
val isCompletedChanged = oldItem.isCompleted != newItem.isCompleted
return if (isCompletedChanged)
NotePayload(isCompletedChanged)
else null
}

data class NotePayload(
val isCompleteChanged: Boolean
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,38 @@ class NoteListAdapter(
onNoteClick.invoke(note.id)
}
holder.personName.text = note.personName
// TODO Раскомментировать, после добавления нового поля в NoteEntity.
// holder.isCompleted.setOnClickListener {
// onIsCompletedClick.invoke(note.copy(
// isCompleted = !note.isCompleted
// ))
// }
// val isCompletedChanged = note.isCompleted != holder.isCompleted.isChecked
// if (isCompletedChanged) {
// holder.isCompleted.isChecked = note.isCompleted
// }
holder.isCompleted.setOnClickListener {
onIsCompletedClick.invoke(
note.copy(
isCompleted = !note.isCompleted
)
)
}
val isCompletedChanged = note.isCompleted != holder.isCompleted.isChecked
if (isCompletedChanged) {
holder.isCompleted.isChecked = note.isCompleted
}
}

override fun onBindViewHolder(
holder: NoteViewHolder,
position: Int,
payloads: MutableList<Any>
) {
if (payloads.isEmpty()) {
super.onBindViewHolder(holder, position, payloads)
return
}
val note = getItem(position)
holder.isCompleted.setOnClickListener {
onIsCompletedClick.invoke(
note.copy(
isCompleted = !note.isCompleted
)
)
}
val notePayload = payloads.first() as NoteDiffUtilCallback.NotePayload
if (notePayload.isCompleteChanged)
holder.isCompleted.isChecked = note.isCompleted
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class NoteListViewModel constructor(
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { noteList ->
state.postValue(NoteListViewState(
noteList = noteList
))
state.postValue(
NoteListViewState(
noteList = noteList
)
)
}
)
}
Expand All @@ -48,9 +50,11 @@ class NoteListViewModel constructor(
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
state.postValue(state.value!!.copy(
noteList = emptyList()
))
state.postValue(
state.value!!.copy(
noteList = emptyList()
)
)
}
)
}
Expand All @@ -64,7 +68,27 @@ class NoteListViewModel constructor(
}

fun onIsCompletedClick(note: NoteEntity) {
// TODO Добавить вызов noteDao для обновления элемента в таблице.
/*compositeDisposable.add(
noteDao.insertNote(note)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
)*/

/* compositeDisposable.add(
noteDao.updateNote(note)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
)*/

compositeDisposable.add(
noteDao.setCompleted(note.id, note.isCompleted)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
)

}

override fun onCleared() {
Expand Down