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 23, 2024
1 parent 1803fad commit 0ccf11d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 34 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 @@ -53,9 +53,9 @@ class NotesListAdapter(
var onItemLongPressed: ((pos: Int, note: Note) -> Unit)? = null
var onItemClicked: (() -> Unit)? = null

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

fun setActivity(activity: AppCompatActivity) {
this.activity = activity as MainActivity
Expand Down Expand Up @@ -232,13 +232,17 @@ class NotesListAdapter(
notifyDataSetChanged()
}

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

fun removeNote(noteToRemove: Note) {
notes.remove(noteToRemove)
selectedNoteCount.postValue(notes.size)
selectedNotesCount.postValue(notes.size)
}

fun removeNotes(notesToRemove: List<Note>) {
notes.removeAll(notesToRemove)
}

fun setNotes(notes: List<Note>) {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
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 @@ -112,6 +112,7 @@ class NotesFragment : BaseFragment() {
notesAdapter?.removeNote(noteToDelete)
toast(requireContext(), getString(R.string.note_deleted))
binding.rvPinnedNotes.adapter?.notifyItemRemoved(deletePosition)
checkForEmptyState()
}
},
onNegativeClicked = {
Expand Down Expand Up @@ -200,7 +201,7 @@ class NotesFragment : BaseFragment() {
}
}

private fun onNotesLoaded(notes: MutableList<Note>) {
private fun onNotesLoaded(notes: List<Note>) {
binding.pbLoading.gone()
if (notesAdapter == null) {
notesAdapter =
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {},
Expand Down Expand Up @@ -506,7 +513,7 @@ class NotesFragment : BaseFragment() {
}

private fun observeSelectedNoteForDelete() {
notesAdapter?.observableSelectedNoteCount?.observe(viewLifecycleOwner) { count ->
notesAdapter?.observableSelectedNotesCount?.observe(viewLifecycleOwner) { count ->
selectedCountForDelete = count
updateSelectStateTexts(count)

Expand All @@ -526,6 +533,12 @@ class NotesFragment : BaseFragment() {
}
}

private fun checkForEmptyState() {
if (notesAdapter?.getNotes()?.isEmpty() == true) {
showEmptyState(true)
}
}

companion object {
const val TAG = "NotesFragment"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class NotesViewModel
}
}

fun readAllNotes(onSuccess: (notes: MutableList<Note>) -> Unit) {
fun readAllNotes(onSuccess: (notes: List<Note>) -> 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)
}
}
}
Expand Down

0 comments on commit 0ccf11d

Please sign in to comment.