Ensure shifting happens only after folder has been properly saved

This fixes #130
This commit is contained in:
Ensar Sarajčić 2023-09-21 14:24:20 +02:00
parent f8fccf9444
commit 65ace16fbf
1 changed files with 12 additions and 10 deletions

View File

@ -690,9 +690,20 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
draggedItem!!.activityInfo
)
fun finalizeFolderOrder(newItem: HomeScreenGridItem) {
if (newParentId != null && gridItems.any { it.parentId == newParentId && it.left == newItem.left }) {
gridItems.filter { it.parentId == newParentId && it.left >= newItem.left && it.id != newItem.id}.forEach {
it.left += 1
}
context.homeScreenGridItemsDB.shiftFolderItems(newParentId, newItem.left - 1, +1, newItem.id)
}
}
if (newHomeScreenGridItem.type == ITEM_TYPE_ICON) {
ensureBackgroundThread {
storeAndShowGridItem(newHomeScreenGridItem)
finalizeFolderOrder(newHomeScreenGridItem)
}
} else if (newHomeScreenGridItem.type == ITEM_TYPE_SHORTCUT) {
(context as? MainActivity)?.handleShorcutCreation(newHomeScreenGridItem.activityInfo!!) { shortcutId, label, icon ->
@ -702,19 +713,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
newHomeScreenGridItem.icon = icon.toBitmap()
newHomeScreenGridItem.drawable = icon
storeAndShowGridItem(newHomeScreenGridItem)
finalizeFolderOrder(newHomeScreenGridItem)
}
}
}
ensureBackgroundThread {
if (newParentId != null && gridItems.any { it.parentId == newParentId && it.left == finalXIndex }) {
gridItems.filter { it.parentId == newParentId && it.left >= finalXIndex }.forEach {
it.left += 1
}
context.homeScreenGridItemsDB.shiftFolderItems(newParentId, left - 1, +1)
}
}
}
draggedItem = null