Skip to content

Wait for DB updates before refreshing checklist items #687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -215,6 +215,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
ensureBackgroundThread {
saveNoteValue(note!!, note!!.value)
context?.updateWidgets()
activity?.runOnUiThread(callback)
}
}
}
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.simplemobiletools.notes.pro.interfaces
interface ChecklistItemsListener {
fun refreshItems()

fun saveChecklist()
fun saveChecklist(callback: () -> Unit = {})
}