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 Down Expand Up @@ -48,7 +48,13 @@ abstract class NoteDatabase : RoomDatabase() {
"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,6 @@ abstract class NoteDao {
@Query("DELETE FROM note_table")
abstract fun deleteAllNote(): Completable

// TODO Добавить метод для обновления одного элемента.
// Что можно указать в качестве возвращаемого типа?
// Напомню, что выше есть метод observeNoteList, который будет вызываться при любом
// изменении списка в Базе Данных.
@Update
abstract fun updateNote(note: NoteEntity): 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 @@ -20,15 +20,14 @@ 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ class NoteListViewModel constructor(
}

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

override fun onCleared() {
Expand Down