From 78aa7578ce107b5d35d20169a01e9f4d36de55bc Mon Sep 17 00:00:00 2001 From: tuancoltech Date: Tue, 8 Oct 2024 12:44:29 +0700 Subject: [PATCH] Deleted notes are not removed from NoteAdapter's backed data until notes are reloaded --- .../arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt | 2 +- .../arkbuilders/arkmemo/ui/fragments/NotesFragment.kt | 8 +++++--- .../arkbuilders/arkmemo/ui/viewmodels/NotesViewModel.kt | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt index f7fc183f..fa568624 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt @@ -192,7 +192,7 @@ class NotesListAdapter( notifyDataSetChanged() } - fun getNotes(): List { + fun getNotes(): MutableList { return notes } diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt index 8c15527f..cba37b5c 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt @@ -184,11 +184,11 @@ class NotesFragment: BaseFragment() { } } - private fun onNotesLoaded(notes: List) { + private fun onNotesLoaded(notes: MutableList) { binding.pbLoading.gone() if (notesAdapter == null) { notesAdapter = NotesListAdapter( - notes.toMutableList(), + notes, onPlayPauseClick = { path, pos, onStop -> playingAudioPath = path if (playingAudioPosition >= 0) { @@ -452,7 +452,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() diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/NotesViewModel.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/NotesViewModel.kt index c9191b3e..1e6fbb2a 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/NotesViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/NotesViewModel.kt @@ -50,12 +50,13 @@ class NotesViewModel @Inject constructor( } } - fun readAllNotes(onSuccess: (notes: List) -> Unit) { + fun readAllNotes(onSuccess: (notes: MutableList) -> 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()) } } } @@ -124,7 +125,7 @@ class NotesViewModel @Inject constructor( } } - fun onDeleteConfirmed(notes: List, onSuccess: () -> Unit) { + fun onDeleteConfirmed(notes: List, onSuccess: (newList: MutableList) -> Unit) { viewModelScope.launch(iODispatcher) { notes.forEach { note -> when (note) { @@ -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()) } } }