allow dragging app icons from All Fragments to home screen
This commit is contained in:
parent
61801ebcb2
commit
06738b595c
|
@ -12,7 +12,7 @@ interface HomeScreenGridItemsDao {
|
||||||
fun getAllItems(): List<HomeScreenGridItem>
|
fun getAllItems(): List<HomeScreenGridItem>
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun insert(item: HomeScreenGridItem)
|
fun insert(item: HomeScreenGridItem): Long
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun insertAll(items: List<HomeScreenGridItem>)
|
fun insertAll(items: List<HomeScreenGridItem>)
|
||||||
|
|
|
@ -71,7 +71,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
|
||||||
|
|
||||||
// figure out at which cell was the item dropped, if it is empty
|
// figure out at which cell was the item dropped, if it is empty
|
||||||
fun itemDraggingStopped(x: Int, y: Int) {
|
fun itemDraggingStopped(x: Int, y: Int) {
|
||||||
|
if (draggedItem == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
// convert stuff like 102x192 to grid cells like 0x1
|
// convert stuff like 102x192 to grid cells like 0x1
|
||||||
rowXCoords.forEachIndexed { xIndex, xCell ->
|
rowXCoords.forEachIndexed { xIndex, xCell ->
|
||||||
|
@ -80,7 +85,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
|
||||||
// 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) {
|
||||||
gridItems.firstOrNull { it.id == draggedItem?.id }?.apply {
|
val draggedHomeGridItem = gridItems.firstOrNull { it.id == draggedItem?.id }
|
||||||
|
|
||||||
|
// we are moving an existing home screen item from one place to another
|
||||||
|
if (draggedHomeGridItem != null) {
|
||||||
|
draggedHomeGridItem.apply {
|
||||||
left = xIndex
|
left = xIndex
|
||||||
top = yIndex
|
top = yIndex
|
||||||
right = xIndex + 1
|
right = xIndex + 1
|
||||||
|
@ -90,14 +99,34 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
|
||||||
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
draggedItem = null
|
draggedItem = null
|
||||||
|
if (redrawIcons) {
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("DrawAllocation")
|
@SuppressLint("DrawAllocation")
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
|
|
Loading…
Reference in New Issue