check if all wanted cells for placing an item are empty

This commit is contained in:
tibbi 2022-09-26 23:08:56 +02:00
parent 54fd9eed50
commit 7b52bf2b30
2 changed files with 64 additions and 11 deletions

View file

@ -164,7 +164,8 @@ class MainActivity : SimpleActivity(), FlingListener {
mLongPressedIcon = null mLongPressedIcon = null
(widgets_fragment as WidgetsFragment).ignoreTouches = false (widgets_fragment as WidgetsFragment).ignoreTouches = false
(all_apps_fragment as AllAppsFragment).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 (!mIgnoreUpEvent) {
if (all_apps_fragment.y < mScreenHeight * 0.5) { if (all_apps_fragment.y < mScreenHeight * 0.5) {
showFragment(all_apps_fragment) showFragment(all_apps_fragment)

View file

@ -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 // figure out at which cell was the item dropped, if it is empty
fun itemDraggingStopped(x: Int, y: Int) { fun itemDraggingStopped() {
if (draggedItem == null) { if (draggedItem == null) {
return return
} }
if (draggedItem!!.type == ITEM_TYPE_ICON) { if (draggedItem!!.type == ITEM_TYPE_ICON) {
addAppIcon(x, y) addAppIcon()
} else if (draggedItem!!.type == ITEM_TYPE_WIDGET) { } else if (draggedItem!!.type == ITEM_TYPE_WIDGET) {
addWidget(x, y) addWidget()
} }
} }
private fun addAppIcon(x: Int, y: Int) { private fun addAppIcon() {
val center = gridCenters.minBy { Math.abs(it.first - x + sideMargins.left) + Math.abs(it.second - y + sideMargins.top) } val center = gridCenters.minBy {
Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top)
}
var redrawIcons = false var redrawIcons = false
val gridCells = getClosestGridCells(center) val gridCells = getClosestGridCells(center)
if (gridCells != null) { if (gridCells != null) {
val xIndex = gridCells.first val xIndex = gridCells.first
val yIndex = gridCells.second val yIndex = gridCells.second
// check if the destination grid item is empty
val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex } // check if the destination cell is empty
if (targetGridItem == null) { 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 } val draggedHomeGridItem = gridItems.firstOrNull { it.id == draggedItem?.id }
// we are moving an existing home screen item from one place to another // 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<Pair<Int, Int>>()
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 draggedItem = null
draggedItemCurrentCoords = Pair(-1, -1) draggedItemCurrentCoords = Pair(-1, -1)
invalidate() invalidate()