Merge pull request #687 from esensar/fix/checklist-glitch

Wait for DB updates before refreshing checklist items
This commit is contained in:
Tibor Kaputa 2023-09-20 14:10:35 +02:00 committed by GitHub
commit 32909a5336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 15 deletions

View File

@ -78,13 +78,9 @@ class ChecklistAdapter(
override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id == key } override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id == key }
override fun onActionModeCreated() { override fun onActionModeCreated() {}
notifyDataSetChanged()
}
override fun onActionModeDestroyed() { override fun onActionModeDestroyed() {}
notifyDataSetChanged()
}
override fun prepareActionMode(menu: Menu) { override fun prepareActionMode(menu: Menu) {
val selectedItems = getSelectedItems() val selectedItems = getSelectedItems()
@ -152,35 +148,44 @@ class ChecklistAdapter(
positions.sortDescending() positions.sortDescending()
removeSelectedItems(positions) removeSelectedItems(positions)
listener?.saveChecklist() listener?.saveChecklist {
if (items.isEmpty()) { if (items.isEmpty()) {
listener?.refreshItems() listener.refreshItems()
}
} }
} }
private fun moveSelectedItemsToTop() { private fun moveSelectedItemsToTop() {
activity.config.sorting = SORT_BY_CUSTOM activity.config.sorting = SORT_BY_CUSTOM
val movedPositions = mutableListOf<Int>()
selectedKeys.reversed().forEach { checklistId -> selectedKeys.reversed().forEach { checklistId ->
val position = items.indexOfFirst { it.id == checklistId } val position = items.indexOfFirst { it.id == checklistId }
val tempItem = items[position] val tempItem = items[position]
items.removeAt(position) items.removeAt(position)
movedPositions.add(position)
items.add(0, tempItem) items.add(0, tempItem)
} }
notifyDataSetChanged() movedPositions.forEach {
notifyItemMoved(it, 0)
}
listener?.saveChecklist() listener?.saveChecklist()
} }
private fun moveSelectedItemsToBottom() { private fun moveSelectedItemsToBottom() {
activity.config.sorting = SORT_BY_CUSTOM activity.config.sorting = SORT_BY_CUSTOM
val movedPositions = mutableListOf<Int>()
selectedKeys.forEach { checklistId -> selectedKeys.forEach { checklistId ->
val position = items.indexOfFirst { it.id == checklistId } val position = items.indexOfFirst { it.id == checklistId }
val tempItem = items[position] val tempItem = items[position]
items.removeAt(position) items.removeAt(position)
movedPositions.add(position)
items.add(items.size, tempItem) items.add(items.size, tempItem)
} }
notifyDataSetChanged() movedPositions.forEach {
notifyItemMoved(it, items.size - 1)
}
listener?.saveChecklist() listener?.saveChecklist()
} }

View File

@ -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) { if (note == null) {
return return
} }
@ -215,6 +215,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
ensureBackgroundThread { ensureBackgroundThread {
saveNoteValue(note!!, note!!.value) saveNoteValue(note!!, note!!.value)
context?.updateWidgets() context?.updateWidgets()
activity?.runOnUiThread(callback)
} }
} }
} }
@ -235,8 +236,8 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
fun getChecklistItems() = Gson().toJson(items) fun getChecklistItems() = Gson().toJson(items)
override fun saveChecklist() { override fun saveChecklist(callback: () -> Unit) {
saveNote() saveNote(callback = callback)
} }
override fun refreshItems() { override fun refreshItems() {

View File

@ -3,5 +3,5 @@ package com.simplemobiletools.notes.pro.interfaces
interface ChecklistItemsListener { interface ChecklistItemsListener {
fun refreshItems() fun refreshItems()
fun saveChecklist() fun saveChecklist(callback: () -> Unit = {})
} }