diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt index e8869376..0c7193a3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/ChecklistAdapter.kt @@ -78,13 +78,9 @@ class ChecklistAdapter( override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id == key } - override fun onActionModeCreated() { - notifyDataSetChanged() - } + override fun onActionModeCreated() {} - override fun onActionModeDestroyed() { - notifyDataSetChanged() - } + override fun onActionModeDestroyed() {} override fun prepareActionMode(menu: Menu) { val selectedItems = getSelectedItems() @@ -152,35 +148,44 @@ class ChecklistAdapter( positions.sortDescending() removeSelectedItems(positions) - listener?.saveChecklist() - if (items.isEmpty()) { - listener?.refreshItems() + listener?.saveChecklist { + if (items.isEmpty()) { + listener.refreshItems() + } } } private fun moveSelectedItemsToTop() { activity.config.sorting = SORT_BY_CUSTOM + val movedPositions = mutableListOf() selectedKeys.reversed().forEach { checklistId -> val position = items.indexOfFirst { it.id == checklistId } val tempItem = items[position] items.removeAt(position) + movedPositions.add(position) items.add(0, tempItem) } - notifyDataSetChanged() + movedPositions.forEach { + notifyItemMoved(it, 0) + } listener?.saveChecklist() } private fun moveSelectedItemsToBottom() { activity.config.sorting = SORT_BY_CUSTOM + val movedPositions = mutableListOf() selectedKeys.forEach { checklistId -> val position = items.indexOfFirst { it.id == checklistId } val tempItem = items[position] items.removeAt(position) + movedPositions.add(position) items.add(items.size, tempItem) } - notifyDataSetChanged() + movedPositions.forEach { + notifyItemMoved(it, items.size - 1) + } listener?.saveChecklist() } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt index 1d0e83cf..04003ed7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt @@ -190,7 +190,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { } } - private fun saveNote(refreshIndex: Int = -1) { + private fun saveNote(refreshIndex: Int = -1, callback: () -> Unit = {}) { if (note == null) { return } @@ -215,6 +215,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { ensureBackgroundThread { saveNoteValue(note!!, note!!.value) context?.updateWidgets() + activity?.runOnUiThread(callback) } } } @@ -235,8 +236,8 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener { fun getChecklistItems() = Gson().toJson(items) - override fun saveChecklist() { - saveNote() + override fun saveChecklist(callback: () -> Unit) { + saveNote(callback = callback) } override fun refreshItems() { diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/ChecklistItemsListener.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/ChecklistItemsListener.kt index 04667b6b..01766222 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/ChecklistItemsListener.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/ChecklistItemsListener.kt @@ -3,5 +3,5 @@ package com.simplemobiletools.notes.pro.interfaces interface ChecklistItemsListener { fun refreshItems() - fun saveChecklist() + fun saveChecklist(callback: () -> Unit = {}) }