Skip to content

Commit

Permalink
Deleted notes are not removed from NoteAdapter's backed data until no…
Browse files Browse the repository at this point in the history
…tes are reloaded
  • Loading branch information
tuancoltech committed Oct 8, 2024
1 parent cb01a71 commit 10c794a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class NotesListAdapter(
notifyDataSetChanged()
}

fun getNotes(): List<Note> {
fun getNotes(): MutableList<Note> {
return notes
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ class NotesFragment: BaseFragment() {
}
}

private fun onNotesLoaded(notes: List<Note>) {
private fun onNotesLoaded(notes: MutableList<Note>) {
binding.pbLoading.gone()
if (notesAdapter == null) {
notesAdapter = NotesListAdapter(
notes.toMutableList(),
notes,
onPlayPauseClick = { path, pos, onStop ->
playingAudioPath = path
if (playingAudioPosition >= 0) {
Expand Down Expand Up @@ -447,7 +447,9 @@ class NotesFragment: BaseFragment() {
isAlert = true,
onPositiveClick = {
binding.pbLoading.visible()
notesViewModel.onDeleteConfirmed(notesAdapter?.selectedNotedForDelete ?: emptyList()) {
val selectedNotes = notesAdapter?.selectedNotedForDelete ?: emptyList()
notesViewModel.onDeleteConfirmed(selectedNotes) {
notesAdapter?.getNotes()?.removeAll(selectedNotes)
binding.pbLoading.gone()
toast(requireContext(), getString(R.string.note_deleted))
binding.rvPinnedNotes.adapter?.notifyDataSetChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ class NotesViewModel @Inject constructor(
}
}

fun readAllNotes(onSuccess: (notes: List<Note>) -> Unit) {
fun readAllNotes(onSuccess: (notes: MutableList<Note>) -> Unit) {
viewModelScope.launch(iODispatcher) {
notes.value = textNotesRepo.read() + graphicNotesRepo.read() + voiceNotesRepo.read()
notes.value.let {
withContext(Dispatchers.Main) {
onSuccess(it.sortedByDescending { note -> note.resource?.modified })
notes.value = it.sortedByDescending { note -> note.resource?.modified }
onSuccess(notes.value.toMutableList())
}
}
}
Expand Down Expand Up @@ -124,7 +125,7 @@ class NotesViewModel @Inject constructor(
}
}

fun onDeleteConfirmed(notes: List<Note>, onSuccess: () -> Unit) {
fun onDeleteConfirmed(notes: List<Note>, onSuccess: (newList: MutableList<Note>) -> Unit) {
viewModelScope.launch(iODispatcher) {
notes.forEach { note ->
when (note) {
Expand All @@ -136,7 +137,7 @@ class NotesViewModel @Inject constructor(
this@NotesViewModel.notes.value = this@NotesViewModel.notes.value.toMutableList()
.apply { removeAll(notes) }
withContext(Dispatchers.Main) {
onSuccess.invoke()
onSuccess.invoke(this@NotesViewModel.notes.value.toMutableList())
}
}
}
Expand Down

0 comments on commit 10c794a

Please sign in to comment.