@@ -245,6 +245,17 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
245245 }
246246 }
247247
248+ fun removeWidget (item : HomeScreenGridItem ) {
249+ ensureBackgroundThread {
250+ removeItemFromHomeScreen(item)
251+ post {
252+ removeView(widgetViews.firstOrNull { it.tag == item.widgetId })
253+ widgetViews.removeIf { it.tag == item.widgetId }
254+ }
255+ gridItems.removeIf { it.id == item.id }
256+ }
257+ }
258+
248259 private fun removeItemFromHomeScreen (item : HomeScreenGridItem ) {
249260 ensureBackgroundThread {
250261 if (item.id != null ) {
@@ -381,7 +392,9 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
381392 val viewX = widgetView.x.toInt()
382393 val viewY = widgetView.y.toInt()
383394 val frameRect = Rect (viewX, viewY, viewX + widgetView.width, viewY + widgetView.height)
384- val otherGridItems = gridItems.filterVisibleOnCurrentPageOnly().filter { it.widgetId != item.widgetId }.toMutableList() as ArrayList <HomeScreenGridItem >
395+ val otherGridItems = gridItems.filterVisibleOnCurrentPageOnly()
396+ .filter { ! it.outOfBounds() }
397+ .filter { it.widgetId != item.widgetId }.toMutableList() as ArrayList <HomeScreenGridItem >
385398 binding.resizeFrame.updateFrameCoords(frameRect, cellWidth, cellHeight, sideMargins, item, otherGridItems)
386399 binding.resizeFrame.beVisible()
387400 binding.resizeFrame.z = 1f // make sure the frame isnt behind the widget itself
@@ -399,6 +412,28 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
399412 cellsRect.bottom
400413 }
401414 updateWidgetPositionAndSize(widgetView, item)
415+
416+ val widgetTargetCells = ArrayList <Pair <Int , Int >>()
417+ for (xCell in item.left.. item.right) {
418+ for (yCell in item.top.. item.bottom) {
419+ widgetTargetCells.add(Pair (xCell, yCell))
420+ }
421+ }
422+
423+ gridItems.filterVisibleOnCurrentPageOnly().filter { it.id != item.id }.forEach { gridItem ->
424+ for (xCell in gridItem.left.. gridItem.right) {
425+ for (yCell in gridItem.getDockAdjustedTop(rowCount).. gridItem.getDockAdjustedBottom(rowCount)) {
426+ val cell = Pair (xCell, yCell)
427+ val isAnyCellOccupied = widgetTargetCells.contains(cell)
428+ if (isAnyCellOccupied) {
429+ if (gridItem.type == ITEM_TYPE_WIDGET && gridItem.outOfBounds()) {
430+ removeWidget(gridItem)
431+ }
432+ }
433+ }
434+ }
435+ }
436+
402437 ensureBackgroundThread {
403438 context.homeScreenGridItemsDB.updateItemPosition(item.left, item.top, item.right, item.bottom, item.page, false , null , item.id!! )
404439 }
@@ -450,7 +485,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
450485 val cell = Pair (xCell, yCell)
451486 val isAnyCellOccupied = wantedCell == cell
452487 if (isAnyCellOccupied) {
453- isDroppingPositionValid = false
488+ if (item.type == ITEM_TYPE_WIDGET && item.outOfBounds()) {
489+ removeWidget(item)
490+ } else {
491+ isDroppingPositionValid = false
492+ }
454493 return @forEach
455494 }
456495 }
@@ -533,14 +572,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
533572 potentialParent = item
534573 } else {
535574 if (item.type == ITEM_TYPE_WIDGET && item.outOfBounds()) {
536- ensureBackgroundThread {
537- removeItemFromHomeScreen(item)
538- post {
539- removeView(widgetViews.firstOrNull { it.tag == item.widgetId })
540- widgetViews.removeIf { it.tag == item.widgetId }
541- }
542- gridItems.removeIf { it.id == item.id }
543- }
575+ removeWidget(item)
544576 } else {
545577 isDroppingPositionValid = false
546578 }
@@ -771,8 +803,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
771803 val cell = Pair (xCell, yCell)
772804 val isAnyCellOccupied = widgetTargetCells.contains(cell)
773805 if (isAnyCellOccupied) {
774- areAllCellsEmpty = false
775- return @forEach
806+ if (item.type == ITEM_TYPE_WIDGET && item.outOfBounds()) {
807+ removeWidget(item)
808+ } else {
809+ areAllCellsEmpty = false
810+ return @forEach
811+ }
776812 }
777813 }
778814 }
0 commit comments