Remove empty home screen pages after moving or deleting items

This commit is contained in:
Ensar Sarajčić 2023-09-07 14:20:38 +02:00
parent 60a4c31a88
commit 63a61dfb2d
2 changed files with 45 additions and 1 deletions

View File

@ -47,6 +47,9 @@ interface HomeScreenGridItemsDao {
@Query("UPDATE home_screen_grid_items SET `left` = `left` + :shiftBy WHERE parent_id == :folderId AND `left` > :shiftFrom AND id != :excludingId")
fun shiftFolderItems(folderId: Long, shiftFrom: Int, shiftBy: Int, excludingId: Long? = null)
@Query("UPDATE home_screen_grid_items SET `page` = `page` + :shiftBy WHERE `page` > :shiftFrom")
fun shiftPage(shiftFrom: Int, shiftBy: Int)
@Transaction
fun deleteByPackageName(packageName: String) {
deleteItemByPackageName(packageName)

View File

@ -246,6 +246,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (item.type == ITEM_TYPE_WIDGET) {
appWidgetHost.deleteAppWidgetId(item.widgetId)
}
if (item.page != 0 && gridItems.none { it.page == item.page && it.id != item.id && it.parentId == null }) {
deletePage(item.page)
}
}
}
@ -441,6 +445,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (isDroppingPositionValid) {
draggedHomeGridItem?.apply {
val oldPage = page
left = xIndex
top = yIndex
right = xIndex
@ -450,6 +455,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
ensureBackgroundThread {
context.homeScreenGridItemsDB.updateItemPosition(left, top, right, bottom, page, docked, parentId, id!!)
if (page != oldPage && oldPage != 0) {
if (gridItems.none { it.page == oldPage && it.parentId == null }) {
deletePage(oldPage)
}
}
}
}
redrawIcons = true
@ -604,6 +615,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
draggedHomeGridItem.apply {
val oldParentId = parentId
val oldLeft = left
val oldPage = page
left = finalXIndex
top = yIndex
right = finalXIndex
@ -638,6 +650,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
context.homeScreenGridItemsDB.shiftFolderItems(newParentId, left - 1, +1, id)
}
if (page != oldPage && oldPage != 0) {
if (gridItems.none { it.page == oldPage && it.parentId == null }) {
deletePage(oldPage)
}
}
}
}
} else if (draggedItem != null) {
@ -734,6 +752,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (areAllCellsEmpty) {
val widgetItem = draggedItem!!.copy()
val oldPage = widgetItem.page
widgetItem.apply {
left = widgetRect.left
top = widgetRect.top
@ -776,7 +795,14 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
right = widgetItem.right
top = widgetItem.top
bottom = widgetItem.bottom
page = widgetItem.page
page = pager.getCurrentPage()
}
if (widgetItem.page != oldPage && oldPage != 0) {
if (gridItems.none { it.page == oldPage && it.parentId == null }) {
deletePage(oldPage)
}
}
}
}
@ -848,6 +874,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
// remove the drawable so that it gets refreshed on long press
item.drawable = null
// Delete existing windget if it has already been loaded to the list
gridItems.removeIf { it.id == item.id }
gridItems.add(item)
}
@ -1317,6 +1345,19 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
}
private fun deletePage(page: Int) {
gridItems.filter { it.page > page }.forEach {
it.page -= 1
}
context.homeScreenGridItemsDB.shiftPage(page, -1)
if (pager.isOutsideOfPageRange()) {
post {
prevPage()
}
}
}
private fun getMaxPage() = gridItems.filter { !it.docked && !it.outOfBounds() }.maxOfOrNull { it.page } ?: 0
fun nextPage(redraw: Boolean = false): Boolean {