From 65ace16fbf2d90b74543d11001cf6cef0953f2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 21 Sep 2023 14:24:20 +0200 Subject: [PATCH] Ensure shifting happens only after folder has been properly saved This fixes #130 --- .../launcher/views/HomeScreenGrid.kt | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index 6b398f8..16f43a7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -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