diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt index 7ebc600..63d4c42 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepoHelper.kt @@ -91,7 +91,7 @@ class NotesRepoHelper return UserNoteProperties(title, description) } - suspend fun deleteNote(notes: List): Unit = + suspend fun deleteNotes(notes: List): Unit = withContext(Dispatchers.IO) { notes.forEach { note -> deleteNote(note) diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt index bc78b59..055773d 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/graphics/GraphicNotesRepo.kt @@ -71,7 +71,7 @@ class GraphicNotesRepo } override suspend fun delete(notes: List) { - helper.deleteNote(notes) + helper.deleteNotes(notes) } override suspend fun read(): List = diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt index d6dfe7b..93521be 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/text/TextNotesRepo.kt @@ -47,7 +47,7 @@ class TextNotesRepo } override suspend fun delete(notes: List) { - helper.deleteNote(notes) + helper.deleteNotes(notes) } override suspend fun delete(note: TextNote) { diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/repo/voices/VoiceNotesRepo.kt b/app/src/main/java/dev/arkbuilders/arkmemo/repo/voices/VoiceNotesRepo.kt index bd9dade..e83a4e3 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/repo/voices/VoiceNotesRepo.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/repo/voices/VoiceNotesRepo.kt @@ -43,7 +43,7 @@ class VoiceNotesRepo } override suspend fun delete(notes: List) { - helper.deleteNote(notes) + helper.deleteNotes(notes) } override suspend fun delete(note: VoiceNote) { 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 5006e4a..2b0383d 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 @@ -53,9 +53,9 @@ class NotesListAdapter( var onItemLongPressed: ((pos: Int, note: Note) -> Unit)? = null var onItemClicked: (() -> Unit)? = null - private val selectedNoteCount by lazy { MutableLiveData() } - val observableSelectedNoteCount by lazy { selectedNoteCount } - val selectedNotedForDelete = mutableListOf() + private val selectedNotesCount by lazy { MutableLiveData() } + val observableSelectedNotesCount by lazy { selectedNotesCount } + val selectedNotesForDelete = mutableListOf() fun setActivity(activity: AppCompatActivity) { this.activity = activity as MainActivity @@ -232,13 +232,17 @@ class NotesListAdapter( notifyDataSetChanged() } - fun getNotes(): MutableList { + fun getNotes(): List { return notes } fun removeNote(noteToRemove: Note) { notes.remove(noteToRemove) - selectedNoteCount.postValue(notes.size) + selectedNotesCount.postValue(notes.size) + } + + fun removeNotes(notesToRemove: List) { + notes.removeAll(notesToRemove) } fun setNotes(notes: List) { @@ -250,20 +254,21 @@ class NotesListAdapter( var selectedCount = 0 notes.forEachIndexed { index, note -> note.selected = mActionMode && index == pos - if (index == pos) { + if (note.selected) { selectedCount++ + selectedNotesForDelete.add(note) } } - selectedNoteCount.postValue(selectedCount) + selectedNotesCount.postValue(selectedCount) notifyDataSetChanged() } fun toggleSelectAllItems(selected: Boolean) { notes.forEach { it.selected = selected } - selectedNotedForDelete.clear() - selectedNoteCount.postValue( + selectedNotesForDelete.clear() + selectedNotesCount.postValue( if (selected) { - selectedNotedForDelete.addAll(notes) + selectedNotesForDelete.addAll(notes) notes.size } else { 0 @@ -319,15 +324,15 @@ class NotesListAdapter( val selectedNote = notes[bindingAdapterPosition] selectedNote.selected = isChecked if (isChecked) { - selectedNoteCount.value?.let { count -> - selectedNoteCount.postValue(count + 1) + selectedNotesCount.value?.let { count -> + selectedNotesCount.postValue(count + 1) } - selectedNotedForDelete.add(selectedNote) + selectedNotesForDelete.add(selectedNote) } else { - selectedNoteCount.value?.let { count -> - selectedNoteCount.postValue(count - 1) + selectedNotesCount.value?.let { count -> + selectedNotesCount.postValue(count - 1) } - selectedNotedForDelete.remove(selectedNote) + selectedNotesForDelete.remove(selectedNote) } buttonView.post { diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt index 3c9b264..fba1b95 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/EditTextNotesFragment.kt @@ -70,7 +70,7 @@ class EditTextNotesFragment : BaseEditNoteFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) notesViewModel.init {} - observeSaveResult(notesViewModel.getSaveNoteResultLiveData()) + if (arguments != null) { requireArguments().getParcelableCompat(NOTE_KEY, TextNote::class.java)?.let { note = it @@ -155,6 +155,7 @@ class EditTextNotesFragment : BaseEditNoteFragment() { ?: binding.toolbar.ivRightActionIcon.gone() view.viewTreeObserver.addOnWindowFocusChangeListener(windowFocusedListener) + observeSaveResult(notesViewModel.getSaveNoteResultLiveData()) } override fun isContentChanged(): Boolean { 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 afb31a3..c69c1d9 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 @@ -112,6 +112,7 @@ class NotesFragment : BaseFragment() { notesAdapter?.removeNote(noteToDelete) toast(requireContext(), getString(R.string.note_deleted)) binding.rvPinnedNotes.adapter?.notifyItemRemoved(deletePosition) + checkForEmptyState() } }, onNegativeClicked = { @@ -200,7 +201,7 @@ class NotesFragment : BaseFragment() { } } - private fun onNotesLoaded(notes: MutableList) { + private fun onNotesLoaded(notes: List) { binding.pbLoading.gone() if (notesAdapter == null) { notesAdapter = @@ -260,16 +261,20 @@ class NotesFragment : BaseFragment() { mItemTouchHelper = ItemTouchHelper(mItemTouchCallback) mItemTouchHelper?.attachToRecyclerView(binding.rvPinnedNotes) - if (notes.isNotEmpty()) { - binding.layoutBottomControl.visible() - binding.groupEmptyState.gone() - binding.rvPinnedNotes.visible() - binding.edtSearch.visible() - } else { + showEmptyState(isEmpty = notes.isEmpty()) + } + + private fun showEmptyState(isEmpty: Boolean) { + if (isEmpty) { binding.layoutBottomControl.gone() binding.groupEmptyState.visible() binding.rvPinnedNotes.gone() binding.edtSearch.gone() + } else { + binding.layoutBottomControl.visible() + binding.groupEmptyState.gone() + binding.rvPinnedNotes.visible() + binding.edtSearch.visible() } } @@ -341,7 +346,7 @@ class NotesFragment : BaseFragment() { activity.fragment = this observeClipboardContent() binding.rvPinnedNotes.layoutManager?.scrollToPosition(lastNoteItemPosition) - if (notesAdapter?.observableSelectedNoteCount?.hasActiveObservers() == false) { + if (notesAdapter?.observableSelectedNotesCount?.hasActiveObservers() == false) { observeSelectedNoteForDelete() } } @@ -431,6 +436,7 @@ class NotesFragment : BaseFragment() { binding.layoutBottomControl.visible() binding.edtSearch.visible() binding.ivSettings.visible() + notesAdapter?.toggleSelectAllItems(false) } else { binding.groupActionModeTexts.visible() updateSelectStateTexts(selectedCountForDelete) @@ -468,13 +474,14 @@ class NotesFragment : BaseFragment() { isAlert = true, onPositiveClick = { binding.pbLoading.visible() - val selectedNotes = notesAdapter?.selectedNotedForDelete ?: emptyList() + val selectedNotes = notesAdapter?.selectedNotesForDelete ?: emptyList() notesViewModel.onDeleteConfirmed(selectedNotes) { - notesAdapter?.getNotes()?.removeAll(selectedNotes) + notesAdapter?.removeNotes(selectedNotes) binding.pbLoading.gone() toast(requireContext(), getString(R.string.note_deleted)) binding.rvPinnedNotes.adapter?.notifyDataSetChanged() toggleActionMode() + checkForEmptyState() } }, onNegativeClicked = {}, @@ -506,7 +513,7 @@ class NotesFragment : BaseFragment() { } private fun observeSelectedNoteForDelete() { - notesAdapter?.observableSelectedNoteCount?.observe(viewLifecycleOwner) { count -> + notesAdapter?.observableSelectedNotesCount?.observe(viewLifecycleOwner) { count -> selectedCountForDelete = count updateSelectStateTexts(count) @@ -526,6 +533,12 @@ class NotesFragment : BaseFragment() { } } + private fun checkForEmptyState() { + if (notesAdapter?.getNotes()?.isEmpty() == true) { + showEmptyState(true) + } + } + companion object { const val TAG = "NotesFragment" } 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 fe3934b..c4671dd 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 @@ -52,13 +52,13 @@ class NotesViewModel } } - fun readAllNotes(onSuccess: (notes: MutableList) -> Unit) { + fun readAllNotes(onSuccess: (notes: List) -> Unit) { viewModelScope.launch(iODispatcher) { notes.value = textNotesRepo.read() + graphicNotesRepo.read() + voiceNotesRepo.read() notes.value.let { withContext(Dispatchers.Main) { notes.value = it.sortedByDescending { note -> note.resource?.modified } - onSuccess(notes.value.toMutableList()) + onSuccess(notes.value) } } }