adding a helper function for getting the closest grid cell

This commit is contained in:
tibbi 2022-09-23 11:44:10 +02:00
parent 115b60c52e
commit 5d85b9d7b4
2 changed files with 50 additions and 39 deletions

View File

@ -43,7 +43,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:e93092b380' implementation 'com.github.SimpleMobileTools:Simple-Commons:b826e8e020'
kapt "androidx.room:room-compiler:2.4.3" kapt "androidx.room:room-compiler:2.4.3"
implementation "androidx.room:room-runtime:2.4.3" implementation "androidx.room:room-runtime:2.4.3"

View File

@ -86,49 +86,47 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
val center = gridCenters.minBy { Math.abs(it.first - x) + Math.abs(it.second - y) } val center = gridCenters.minBy { Math.abs(it.first - x) + Math.abs(it.second - y) }
var redrawIcons = false var redrawIcons = false
// convert stuff like 102x192 to grid cells like 0x1 val gridCells = getClosestGridCells(center)
rowXCoords.forEachIndexed { xIndex, xCell -> if (gridCells != null) {
rowYCoords.forEachIndexed { yIndex, yCell -> val xIndex = gridCells.first
if (xCell + rowWidth / 2 == center.first && yCell + rowHeight / 2 == center.second) { val yIndex = gridCells.second
// check if the destination grid item is empty // check if the destination grid item is empty
val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex } val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex }
if (targetGridItem == null) { if (targetGridItem == null) {
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
if (draggedHomeGridItem != null) { if (draggedHomeGridItem != null) {
draggedHomeGridItem.apply { draggedHomeGridItem.apply {
left = xIndex left = xIndex
top = yIndex top = yIndex
right = xIndex + 1 right = xIndex + 1
bottom = yIndex + 1 bottom = yIndex + 1
ensureBackgroundThread { ensureBackgroundThread {
context.homeScreenGridItemsDB.updateAppPosition(left, top, right, bottom, id!!) context.homeScreenGridItemsDB.updateAppPosition(left, top, right, bottom, id!!)
}
}
redrawIcons = true
} else if (draggedItem != null) {
// we are dragging a new item at the home screen from the All Apps fragment
val newHomeScreenGridItem =
HomeScreenGridItem(null, xIndex, yIndex, xIndex + 1, yIndex + 1, draggedItem!!.packageName, draggedItem!!.title)
ensureBackgroundThread {
val newId = context.homeScreenGridItemsDB.insert(newHomeScreenGridItem)
newHomeScreenGridItem.id = newId
gridItems.add(newHomeScreenGridItem)
val drawable = context.getDrawableForPackageName(newHomeScreenGridItem.packageName)
if (drawable != null) {
gridItemDrawables[newHomeScreenGridItem.packageName] = drawable
}
invalidate()
}
} }
} else { }
redrawIcons = true redrawIcons = true
} else if (draggedItem != null) {
// we are dragging a new item at the home screen from the All Apps fragment
val newHomeScreenGridItem =
HomeScreenGridItem(null, xIndex, yIndex, xIndex + 1, yIndex + 1, draggedItem!!.packageName, draggedItem!!.title)
ensureBackgroundThread {
val newId = context.homeScreenGridItemsDB.insert(newHomeScreenGridItem)
newHomeScreenGridItem.id = newId
gridItems.add(newHomeScreenGridItem)
val drawable = context.getDrawableForPackageName(newHomeScreenGridItem.packageName)
if (drawable != null) {
gridItemDrawables[newHomeScreenGridItem.packageName] = drawable
}
invalidate()
} }
} }
} else {
redrawIcons = true
} }
} }
@ -138,6 +136,19 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
} }
} }
// convert stuff like 102x192 to grid cells like 0x1
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
rowXCoords.forEachIndexed { xIndex, xCell ->
rowYCoords.forEachIndexed { yIndex, yCell ->
if (xCell + rowWidth / 2 == center.first && yCell + rowHeight / 2 == center.second) {
return Pair(xIndex, yIndex)
}
}
}
return null
}
@SuppressLint("DrawAllocation") @SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)