From 9fba3b56d6ba9e7c7246b7a33d19f9b9fe7a3396 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 21 Sep 2022 23:25:17 +0200 Subject: [PATCH] =?UTF-8?q?implementing=20initial=20drag=20&=C2=A0drop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interfaces/HomeScreenGridItemsDao.kt | 3 +++ .../launcher/models/HomeScreenGridItem.kt | 6 ++--- .../launcher/views/HomeScreenGrid.kt | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/interfaces/HomeScreenGridItemsDao.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/interfaces/HomeScreenGridItemsDao.kt index 46247e7..9db56dc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/interfaces/HomeScreenGridItemsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/interfaces/HomeScreenGridItemsDao.kt @@ -20,6 +20,9 @@ interface HomeScreenGridItemsDao { @Query("UPDATE home_screen_grid_items SET title = :title WHERE package_name = :packageName") 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) + @Query("DELETE FROM home_screen_grid_items WHERE package_name = :packageName") fun deleteItem(packageName: String) } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt index 3057657..64fc40c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt @@ -10,9 +10,9 @@ import androidx.room.PrimaryKey data class HomeScreenGridItem( @PrimaryKey(autoGenerate = true) var id: Long?, @ColumnInfo(name = "left") var left: Int, - @ColumnInfo(name = "top") val top: Int, - @ColumnInfo(name = "right") val right: Int, - @ColumnInfo(name = "bottom") val bottom: Int, + @ColumnInfo(name = "top") var top: Int, + @ColumnInfo(name = "right") var right: Int, + @ColumnInfo(name = "bottom") var bottom: Int, @ColumnInfo(name = "package_name") val packageName: String, @ColumnInfo(name = "title") val title: String ) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index 482123f..6faa82b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -69,7 +69,32 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie invalidate() } + // figure out at which cell was the item dropped, if it is empty fun itemDraggingStopped(x: Int, y: Int) { + val center = gridCenters.minBy { Math.abs(it.first - x) + Math.abs(it.second - y) } + + // convert stuff like 102x192 to grid cells like 0x1 + rowXCoords.forEachIndexed { xIndex, xCell -> + rowYCoords.forEachIndexed { yIndex, yCell -> + if (xCell + rowWidth / 2 == center.first && yCell + rowHeight / 2 == center.second) { + // check if the destination grid item is empty + val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex } + if (targetGridItem == null) { + gridItems.firstOrNull { it.id == draggedItem?.id }?.apply { + left = xIndex + top = yIndex + right = xIndex + 1 + bottom = yIndex + 1 + + ensureBackgroundThread { + context.homeScreenGridItemsDB.updateAppPosition(left, top, right, bottom, id!!) + } + } + } + } + } + } + draggedItem = null invalidate() }