Skip to content

Commit

Permalink
* Show empty state of notes list when all notes are deleted
Browse files Browse the repository at this point in the history
* Fix a bug in selecting notes for deletion logic
  • Loading branch information
tuancoltech committed Nov 15, 2024
1 parent 1803fad commit 4775613
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class NotesRepoHelper
return UserNoteProperties(title, description)
}

suspend fun deleteNote(notes: List<Note>): Unit =
suspend fun deleteNotes(notes: List<Note>): Unit =
withContext(Dispatchers.IO) {
notes.forEach { note ->
deleteNote(note)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class GraphicNotesRepo
}

override suspend fun delete(notes: List<GraphicNote>) {
helper.deleteNote(notes)
helper.deleteNotes(notes)
}

override suspend fun read(): List<GraphicNote> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TextNotesRepo
}

override suspend fun delete(notes: List<TextNote>) {
helper.deleteNote(notes)
helper.deleteNotes(notes)
}

override suspend fun delete(note: TextNote) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class VoiceNotesRepo
}

override suspend fun delete(notes: List<VoiceNote>) {
helper.deleteNote(notes)
helper.deleteNotes(notes)
}

override suspend fun delete(note: VoiceNote) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class NotesListAdapter(

private val selectedNoteCount by lazy { MutableLiveData<Int>() }
val observableSelectedNoteCount by lazy { selectedNoteCount }
val selectedNotedForDelete = mutableListOf<Note>()
val selectedNotesForDelete = mutableListOf<Note>()

fun setActivity(activity: AppCompatActivity) {
this.activity = activity as MainActivity
Expand Down Expand Up @@ -250,8 +250,9 @@ 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)
Expand All @@ -260,10 +261,10 @@ class NotesListAdapter(

fun toggleSelectAllItems(selected: Boolean) {
notes.forEach { it.selected = selected }
selectedNotedForDelete.clear()
selectedNotesForDelete.clear()
selectedNoteCount.postValue(
if (selected) {
selectedNotedForDelete.addAll(notes)
selectedNotesForDelete.addAll(notes)
notes.size
} else {
0
Expand Down Expand Up @@ -322,12 +323,12 @@ class NotesListAdapter(
selectedNoteCount.value?.let { count ->
selectedNoteCount.postValue(count + 1)
}
selectedNotedForDelete.add(selectedNote)
selectedNotesForDelete.add(selectedNote)
} else {
selectedNoteCount.value?.let { count ->
selectedNoteCount.postValue(count - 1)
}
selectedNotedForDelete.remove(selectedNote)
selectedNotesForDelete.remove(selectedNote)
}

buttonView.post {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -155,6 +155,7 @@ class EditTextNotesFragment : BaseEditNoteFragment() {
?: binding.toolbar.ivRightActionIcon.gone()

view.viewTreeObserver.addOnWindowFocusChangeListener(windowFocusedListener)
observeSaveResult(notesViewModel.getSaveNoteResultLiveData())
}

override fun isContentChanged(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,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()
}
}

Expand Down Expand Up @@ -431,6 +435,7 @@ class NotesFragment : BaseFragment() {
binding.layoutBottomControl.visible()
binding.edtSearch.visible()
binding.ivSettings.visible()
notesAdapter?.toggleSelectAllItems(false)
} else {
binding.groupActionModeTexts.visible()
updateSelectStateTexts(selectedCountForDelete)
Expand Down Expand Up @@ -468,13 +473,16 @@ 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)
binding.pbLoading.gone()
toast(requireContext(), getString(R.string.note_deleted))
binding.rvPinnedNotes.adapter?.notifyDataSetChanged()
toggleActionMode()
if (notesAdapter?.getNotes()?.isEmpty() == true) {
showEmptyState(true)
}
}
},
onNegativeClicked = {},
Expand Down

0 comments on commit 4775613

Please sign in to comment.