handle widget dragging

This commit is contained in:
tibbi 2022-10-01 15:25:54 +02:00
parent d4b4edd6eb
commit 65d13c31b1
2 changed files with 43 additions and 9 deletions

View File

@ -21,7 +21,7 @@ interface HomeScreenGridItemsDao {
fun updateAppTitle(title: String, packageName: String)
@Query("UPDATE home_screen_grid_items SET `left` = :left, `top` = :top, `right` = :right, `bottom` = :bottom WHERE id = :id")
fun updateAppPosition(left: Int, top: Int, right: Int, bottom: Int, id: Long)
fun updateItemPosition(left: Int, top: Int, right: Int, bottom: Int, id: Long)
@Query("DELETE FROM home_screen_grid_items WHERE id = :id")
fun deleteById(id: Long)

View File

@ -8,6 +8,7 @@ import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
@ -82,8 +83,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
ensureBackgroundThread {
gridItems = context.homeScreenGridItemsDB.getAllItems() as ArrayList<HomeScreenGridItem>
gridItems.forEach { item ->
if (item.type == ITEM_TYPE_ICON) {
item.drawable = context.getDrawableForPackageName(item.packageName)
}
}
redrawGrid()
}
@ -187,7 +190,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
bottom = yIndex + 1
ensureBackgroundThread {
context.homeScreenGridItemsDB.updateAppPosition(left, top, right, bottom, id!!)
context.homeScreenGridItemsDB.updateItemPosition(left, top, right, bottom, id!!)
}
}
redrawIcons = true
@ -268,11 +271,28 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
ensureBackgroundThread {
// store the new widget at creating it, else just move the existing one
if (widgetItem.id == null) {
val itemId = context.homeScreenGridItemsDB.insert(widgetItem)
widgetItem.id = itemId
post {
bindWidget(widgetItem, false)
}
} else {
context.homeScreenGridItemsDB.updateItemPosition(widgetItem.left, widgetItem.top, widgetItem.right, widgetItem.bottom, widgetItem.id!!)
val widgetView = widgetViews.firstOrNull { it.tag == widgetItem.widgetId }
if (widgetView != null) {
widgetView.x = calculateWidgetX(widgetItem.left)
widgetView.y = calculateWidgetY(widgetItem.top)
}
gridItems.firstOrNull { it.id == widgetItem.id }?.apply {
left = widgetItem.left
right = widgetItem.right
top = widgetItem.top
bottom = widgetItem.bottom
}
}
}
} else {
performHapticFeedback()
@ -319,16 +339,30 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
widgetView.longPressListener = { x, y ->
val yOffset = resources.getDimension(R.dimen.home_long_press_anchor_offset_y)
(context as? MainActivity)?.showHomeIconMenu(x, widgetView.y - yOffset, item, false)
val gridItem = gridItems.firstOrNull { it.widgetId == appWidgetId }
if (gridItem != null) {
widgetView.buildDrawingCache()
gridItem.drawable = BitmapDrawable(widgetView.drawingCache)
}
}
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
widgetView.x = calculateWidgetX(item.left)
widgetView.y = calculateWidgetY(item.top)
val widgetWidth = item.widthCells * rowWidth
val widgetHeight = item.heightCells * rowHeight
addView(widgetView, widgetWidth, widgetHeight)
widgetViews.add(widgetView)
// remove the drawable so that it gets refreshed on long press
item.drawable = null
gridItems.add(item)
}
private fun calculateWidgetX(leftCell: Int) = leftCell * rowWidth + sideMargins.left.toFloat()
private fun calculateWidgetY(topCell: Int) = topCell * rowHeight + sideMargins.top.toFloat()
// convert stuff like 102x192 to grid cells like 0x1
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
rowXCoords.forEachIndexed { xIndex, xCell ->