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 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<Int>()
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<Int>()
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()
}

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) {
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() {

View File

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