Save User State

Sep 10 2024 · Kotlin 1.9, Android 14, Android Studio Koala | 2024.1.1

Lesson 06: CRUD Operations

Demo: Update & Delete Notes from Room Database

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this section, you’ll add the remaining CRUD operations - update and delete. These new functions will be added to your DAO and repository to update and delete Room database notes.

@Update
suspend fun update(note: NoteEntity)

@Delete
suspend fun delete(note: NoteEntity)
suspend fun update(noteEntity: NoteEntity)

suspend fun delete(noteEntity: NoteEntity)
override suspend fun update(noteEntity: NoteEntity) {
  withContext(ioDispatcher) {
    notesDao.update(noteEntity)
  }
}

override suspend fun delete(noteEntity: NoteEntity) {
  withContext(ioDispatcher) {
    notesDao.delete(noteEntity)
  }
}
data object UpdateNote : CreateNoteEvents
CreateNoteEvents.UpdateNote -> {
  if (createNoteState.value.isValid()) {
    viewModelScope.launch {
      val noteEntity = NoteEntity(
        id = currentNote.value?.id ?: 0,
        title = createNoteState.value.title ?: "",
        description = createNoteState.value.description ?: "",
        priority = createNoteState.value.priority ?: "",
        timestamp = System.currentTimeMillis(),
        noteLocation = createNoteState.value.noteLocation ?: ""
      )
      notesRepository.update(noteEntity)
    }
  }
}
private val _currentNote = MutableStateFlow<NoteEntity?>(null)
val currentNote = _currentNote.asStateFlow()
fun updateNoteWithPreviousDetails(noteEntity: NoteEntity) {
  _currentNote.update {
    noteEntity
  }
  _createNoteState.update {
    it.copy(
      title = noteEntity.title,
      description = noteEntity.description,
      priority = noteEntity.priority,
      noteLocation = noteEntity.noteLocation
    )
  }
}
viewModel.updateNoteWithPreviousDetails(it)
UpdateNoteScreenContent(
  createNoteState = createNoteState,
  onTitleChange = { title ->
    viewModel.handleCreateNoteEvents(CreateNoteEvents.TitleChanged(title))
  },
  onDescriptionChange = { description ->
    viewModel.handleCreateNoteEvents(CreateNoteEvents.DescriptionChanged(description))
  },
  onPriorityChange = { priority ->
    viewModel.handleCreateNoteEvents(CreateNoteEvents.PriorityChanged(priority))
  },
  onUpdateNote = {
    viewModel.handleCreateNoteEvents(CreateNoteEvents.UpdateNote)
    navigateToHome()
  },
  onNoteLocationChange = { noteLocation ->
    viewModel.handleCreateNoteEvents(CreateNoteEvents.NoteLocationChanged(noteLocation))
  }
)
fun delete(noteEntity: NoteEntity) {
  viewModelScope.launch {
    notesRepository.delete(noteEntity)
  }
}
viewModel.delete(it)
navigateBack()
See forum comments
Cinema mode Download course materials from Github
Previous: Demo: Create & Read Notes from Room Database Next: Conclusion