Add option to move checklist item all the way top or bottom #370

This commit is contained in:
tanvirahmod
2021-01-21 16:33:38 +06:00
parent a64eabde8e
commit 3fa9bebcb2
41 changed files with 107 additions and 0 deletions

View File

@ -60,6 +60,8 @@ class ChecklistAdapter(activity: BaseSimpleActivity, var items: ArrayList<Checkl
}
when (id) {
R.id.cab_move_to_top -> moveSelectedItems(true)
R.id.cab_move_to_bottom -> moveSelectedItems(false)
R.id.cab_rename -> renameChecklistItem()
R.id.cab_delete -> deleteSelection()
}
@ -145,6 +147,21 @@ class ChecklistAdapter(activity: BaseSimpleActivity, var items: ArrayList<Checkl
}
}
private fun moveSelectedItems(isMoveToTop: Boolean) {
selectedKeys.withIndex()
.forEach { keys ->
val position = items.indexOfFirst { it.id == keys.value }
val tempItem = items[position]
items.remove(tempItem)
if (isMoveToTop)
items.add(0, tempItem)
else
items.add(items.size, tempItem)
}
notifyDataSetChanged()
listener?.saveChecklist()
}
private fun getItemWithKey(key: Int): ChecklistItem? = items.firstOrNull { it.id == key }
private fun getSelectedItems() = items.filter { selectedKeys.contains(it.id) } as ArrayList<ChecklistItem>