From 7b52bf2b3023e607619f8473cf890d442d92d142 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 26 Sep 2022 23:08:56 +0200 Subject: [PATCH] check if all wanted cells for placing an item are empty --- .../launcher/activities/MainActivity.kt | 3 +- .../launcher/views/HomeScreenGrid.kt | 72 ++++++++++++++++--- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index ad1d52e..722700b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -164,7 +164,8 @@ class MainActivity : SimpleActivity(), FlingListener { mLongPressedIcon = null (widgets_fragment as WidgetsFragment).ignoreTouches = false (all_apps_fragment as AllAppsFragment).ignoreTouches = false - home_screen_grid.itemDraggingStopped(event.x.toInt(), event.y.toInt()) + home_screen_grid.itemDraggingStopped() + if (!mIgnoreUpEvent) { if (all_apps_fragment.y < mScreenHeight * 0.5) { showFragment(all_apps_fragment) 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 9ecff2e..680e306 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -110,29 +110,46 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie } // figure out at which cell was the item dropped, if it is empty - fun itemDraggingStopped(x: Int, y: Int) { + fun itemDraggingStopped() { if (draggedItem == null) { return } if (draggedItem!!.type == ITEM_TYPE_ICON) { - addAppIcon(x, y) + addAppIcon() } else if (draggedItem!!.type == ITEM_TYPE_WIDGET) { - addWidget(x, y) + addWidget() } } - private fun addAppIcon(x: Int, y: Int) { - val center = gridCenters.minBy { Math.abs(it.first - x + sideMargins.left) + Math.abs(it.second - y + sideMargins.top) } - var redrawIcons = false + private fun addAppIcon() { + val center = gridCenters.minBy { + Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) + } + var redrawIcons = false val gridCells = getClosestGridCells(center) if (gridCells != null) { val xIndex = gridCells.first val yIndex = gridCells.second - // check if the destination grid item is empty - val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex } - if (targetGridItem == null) { + + // check if the destination cell is empty + var areAllCellsEmpty = true + val wantedCell = Pair(xIndex, yIndex) + gridItems.forEach { item -> + for (xCell in item.left until item.right) { + for (yCell in item.top until item.bottom) { + val cell = Pair(xCell, yCell) + val isAnyCellOccupied = wantedCell == cell + if (isAnyCellOccupied) { + areAllCellsEmpty = false + return@forEach + } + } + } + } + + if (areAllCellsEmpty) { val draggedHomeGridItem = gridItems.firstOrNull { it.id == draggedItem?.id } // we are moving an existing home screen item from one place to another @@ -184,7 +201,42 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie } } - private fun addWidget(x: Int, y: Int) { + private fun addWidget() { + val center = gridCenters.minBy { + Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) + } + + val gridCells = getClosestGridCells(center) + if (gridCells != null) { + val widgetRect = getWidgetOccupiedRect(gridCells) + val widgetTargetCells = ArrayList>() + for (xCell in widgetRect.left until widgetRect.right) { + for (yCell in widgetRect.top until widgetRect.bottom) { + widgetTargetCells.add(Pair(xCell, yCell)) + } + } + + var areAllCellsEmpty = true + gridItems.forEach { item -> + for (xCell in item.left until item.right) { + for (yCell in item.top until item.bottom) { + val cell = Pair(xCell, yCell) + val isAnyCellOccupied = widgetTargetCells.contains(cell) + if (isAnyCellOccupied) { + areAllCellsEmpty = false + return@forEach + } + } + } + } + + if (areAllCellsEmpty) { + + } else { + performHapticFeedback() + } + } + draggedItem = null draggedItemCurrentCoords = Pair(-1, -1) invalidate()