Properly clean up folder items after removing item from folder

This commit is contained in:
Ensar Sarajčić 2023-08-18 15:57:32 +02:00
parent dfe9b04f71
commit 10603be76b
2 changed files with 42 additions and 32 deletions

View File

@ -44,6 +44,9 @@ interface HomeScreenGridItemsDao {
@Query("DELETE FROM home_screen_grid_items WHERE parent_id IN (SELECT id FROM home_screen_grid_items WHERE package_name = :packageName)") @Query("DELETE FROM home_screen_grid_items WHERE parent_id IN (SELECT id FROM home_screen_grid_items WHERE package_name = :packageName)")
fun deleteItemsByParentPackageName(packageName: String) fun deleteItemsByParentPackageName(packageName: String)
@Query("UPDATE home_screen_grid_items SET `left` = `left` + :shiftBy WHERE parent_id == :folderId AND `left` >= :shiftFrom")
fun shiftFolderItems(folderId: Long, shiftFrom: Int, shiftBy: Int)
@Transaction @Transaction
fun deleteByPackageName(packageName: String) { fun deleteByPackageName(packageName: String) {
deleteItemByPackageName(packageName) deleteItemByPackageName(packageName)

View File

@ -551,6 +551,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
if (draggedHomeGridItem != null) { if (draggedHomeGridItem != null) {
draggedHomeGridItem.apply { draggedHomeGridItem.apply {
val oldParentId = parentId val oldParentId = parentId
val oldLeft = left
left = finalXIndex left = finalXIndex
top = yIndex top = yIndex
right = finalXIndex right = finalXIndex
@ -571,6 +572,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
context.homeScreenGridItemsDB.updateItemPosition(left, top, right, bottom, page, docked, newParentId, id!!) context.homeScreenGridItemsDB.updateItemPosition(left, top, right, bottom, page, docked, newParentId, id!!)
if (deleteOldParent && oldParentId != null) { if (deleteOldParent && oldParentId != null) {
context.homeScreenGridItemsDB.deleteById(oldParentId) context.homeScreenGridItemsDB.deleteById(oldParentId)
} else if (oldParentId != null) {
gridItems.filter { it.parentId == oldParentId && it.left >= oldLeft }.forEach {
it.left -= 1
}
context.homeScreenGridItemsDB.shiftFolderItems(oldParentId, oldLeft, -1)
} }
} }
} }
@ -984,6 +990,38 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
canvas.drawCircle(currentIndicatorPosition + pageIndicatorRadius, pageIndicatorY, pageIndicatorRadius, currentPageIndicatorPaint) canvas.drawCircle(currentIndicatorPosition + pageIndicatorRadius, pageIndicatorY, pageIndicatorRadius, currentPageIndicatorPaint)
} }
val folder = currentlyOpenFolder
if (folder != null) {
val items = folder.getFolderItems()
val folderRect = folder.getFolderRect()
val folderItemsRect = folder.getFolderItemsRect()
canvas.drawRoundRect(folderRect, roundedCornerRadius, roundedCornerRadius, folderBackgroundPaint)
val textX = folderRect.left + folderPadding
val textY = folderRect.top + folderPadding + folderTitleTextPaint.textSize
val staticLayout = StaticLayout.Builder
.obtain(folder.title, 0, folder.title.length, folderTitleTextPaint, (folderRect.width() - 2 * folderPadding).toInt())
.setMaxLines(1)
.setEllipsize(TextUtils.TruncateAt.END)
.setAlignment(Layout.Alignment.ALIGN_CENTER)
.build()
canvas.save()
canvas.translate(textX, textY)
staticLayout.draw(canvas)
canvas.restore()
items.forEach { item ->
val (row, column) = item.getPositionInFolder(folder)
handleGridItemDrawing(
item,
(folderItemsRect.left + column * cellWidth).roundToInt(),
(folderItemsRect.top + row * cellHeight).roundToInt()
)
}
}
if (draggedItem != null && draggedItemCurrentCoords.first != -1 && draggedItemCurrentCoords.second != -1) { if (draggedItem != null && draggedItemCurrentCoords.first != -1 && draggedItemCurrentCoords.second != -1) {
if (draggedItem!!.type == ITEM_TYPE_ICON || draggedItem!!.type == ITEM_TYPE_SHORTCUT || draggedItem!!.type == ITEM_TYPE_FOLDER) { if (draggedItem!!.type == ITEM_TYPE_ICON || draggedItem!!.type == ITEM_TYPE_SHORTCUT || draggedItem!!.type == ITEM_TYPE_FOLDER) {
// draw a circle under the current cell // draw a circle under the current cell
@ -1042,38 +1080,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
} }
val folder = currentlyOpenFolder
if (folder != null) {
val items = folder.getFolderItems()
val folderRect = folder.getFolderRect()
val folderItemsRect = folder.getFolderItemsRect()
canvas.drawRoundRect(folderRect, roundedCornerRadius, roundedCornerRadius, folderBackgroundPaint)
val textX = folderRect.left + folderPadding
val textY = folderRect.top + folderPadding + folderTitleTextPaint.textSize
val staticLayout = StaticLayout.Builder
.obtain(folder.title, 0, folder.title.length, folderTitleTextPaint, (folderRect.width() - 2 * folderPadding).toInt())
.setMaxLines(1)
.setEllipsize(TextUtils.TruncateAt.END)
.setAlignment(Layout.Alignment.ALIGN_CENTER)
.build()
canvas.save()
canvas.translate(textX, textY)
staticLayout.draw(canvas)
canvas.restore()
items.forEach { item ->
val (row, column) = item.getPositionInFolder(folder)
handleGridItemDrawing(
item,
(folderItemsRect.left + column * cellWidth).roundToInt(),
(folderItemsRect.top + row * cellHeight).roundToInt()
)
}
}
isFirstDraw = false isFirstDraw = false
} }
@ -1110,6 +1116,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
widgetViews.forEach { widgetViews.forEach {
it.ignoreTouches = true it.ignoreTouches = true
} }
closeFolder(true)
} }
fun fragmentCollapsed() { fun fragmentCollapsed() {