Skip to content

Commit

Permalink
Moving seems to be working
Browse files Browse the repository at this point in the history
  • Loading branch information
agronick committed Aug 15, 2022
1 parent 63c1b5f commit a0e0a69
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/com/agronick/launcher/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.core.view.GestureDetectorCompat
var TAG = "main"

class MainActivity : Activity(), GestureDetector.OnGestureListener {
private var wasScrolling = false
private lateinit var mDetector: GestureDetectorCompat
private lateinit var mainView: MainView

Expand Down Expand Up @@ -46,6 +47,9 @@ class MainActivity : Activity(), GestureDetector.OnGestureListener {
} else if (mainView.reorderer != null) {
mainView.handleLongPress(event)
return true
} else if (wasScrolling && event.action == MotionEvent.ACTION_UP) {
mainView.checkOverPanLimit()
return true
} else {
super.onTouchEvent(event)
}
Expand Down Expand Up @@ -115,9 +119,7 @@ class MainActivity : Activity(), GestureDetector.OnGestureListener {
mainView.offsetLeft -= distanceX
mainView.offsetTop -= distanceY
mainView.prepareInvalidate()
if (e2 != null && e2.action == MotionEvent.ACTION_UP) {
mainView.checkOverPanLimit()
}
wasScrolling = true
return true
}

Expand Down
64 changes: 44 additions & 20 deletions app/src/main/java/com/agronick/launcher/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import util.geometry.Vector2
import java.util.*

class MainView(context: Context, appList: List<PInfo>) : View(context) {
private var edgeTimer: Timer? = null
private var density: Float = context.resources.displayMetrics.density
var onPackageClick: ((PInfo) -> Unit)? = null

Expand All @@ -30,26 +31,61 @@ class MainView(context: Context, appList: List<PInfo>) : View(context) {
}.run()
}

var edgeTimer = Timer()
val resetEdge: () -> Unit = {
edgeTimer.cancel()
var reorderer: Reorderer? = null

fun resetReorderEdgeTimer() {
edgeTimer?.cancel()
}

fun reorderAtEdge(newOffsets: Vector2) {
resetReorderEdgeTimer()
edgeTimer = Timer()
edgeTimer?.schedule(object : TimerTask() {
override fun run() {
offsetLeft += newOffsets.x
offsetTop += newOffsets.y
val curReorderer = reorderer
if (curReorderer !== null) {
val appPos = curReorderer.getAppPos()
post {
curReorderer.onMove(
Vector2(
appPos.x - newOffsets.x,
appPos.y - newOffsets.y
)
)
}
}
prepareInvalidate()
}
}, 0, 33)
}
var reorderer: Reorderer? = null

fun handleLongPress(event: MotionEvent) {
val offset = getRelativePosition(Pair(event.x, event.y))
if (reorderer != null) {
if (event.action == MotionEvent.ACTION_UP) {
reorderer!!.onStopReorder(container.getAppAtPoint(Vector2(offset.x, offset.y)))
reorderer!!.onStopReorder(
when (container.getLimit(offsetLeft, offsetTop, canvasSize)) {
Pair(offsetLeft, offsetTop) -> container.getAppAtPoint(
Vector2(
offset.x,
offset.y
)
)
else -> null
}
)
reorderer = null
resetReorderEdgeTimer()
} else {
reorderer!!.onMove(offset)
val newOffsets =
reorderer!!.checkAtEdge(offset, container.lastCircle, container.appCircleSize)
reorderer!!.checkAtEdge(offset, container.lastCircle, density * 0.3f)
if (newOffsets != null) {
offsetLeft += newOffsets.x
offsetTop += newOffsets.y
reorderAtEdge(newOffsets.scale(density * 3))
} else {
resetReorderEdgeTimer()
}
prepareInvalidate()
}
Expand All @@ -64,18 +100,6 @@ class MainView(context: Context, appList: List<PInfo>) : View(context) {
}
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event != null) {
when (event.action) {
MotionEvent.ACTION_MOVE -> {
resetEdge()
return true
}
}
}
return super.onTouchEvent(event)
}

fun checkOverPanLimit() {
val limited = container.getLimit(offsetLeft, offsetTop, canvasSize)
val animators = mutableListOf<ValueAnimator>()
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/java/com/agronick/launcher/PreferenceManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package com.agronick.launcher
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import java.util.*
import kotlin.collections.ArrayList

object PreferenceManager {
private var preferences =
Launcher.appContext.getSharedPreferences("prefs", Context.MODE_PRIVATE)
private var editor = preferences.edit()
private var appOrder = preferences.getString("app_order", "")?.split(";")?.toTypedArray()!!
.toCollection(ArrayList())
private lateinit var appOrder: ArrayList<String>
private var sep = ";"

fun getSetDiam(value: Double): Double {
Expand All @@ -32,7 +33,7 @@ object PreferenceManager {
order = null
)
}.toMap().toMutableMap()
val prefList = preferences.getString("app_order", null) ?: return asMapp.values.toList()
val prefList = preferences.getString("app_order", "")
val blankPinfo = PInfo(
appname = null,
pname = null,
Expand All @@ -41,10 +42,19 @@ object PreferenceManager {
order = null
)
val matchedItems =
prefList.split(sep).toTypedArray().map { asMapp.remove(it) ?: blankPinfo }
prefList!!.split(sep).toTypedArray().map { asMapp.remove(it) ?: blankPinfo }
.toMutableList()
if (matchedItems.size == 1 && matchedItems.first().appname == null) {
matchedItems.clear()
}
matchedItems.addAll(asMapp.values)
matchedItems.mapIndexed { order, pInfo -> pInfo.order = order }
matchedItems.removeIf { it.pname?.startsWith("com.agronick.launcher") ?: false }
val orderedItems = LinkedList<String>()
matchedItems.forEachIndexed { order, pInfo ->
pInfo.order = order
orderedItems.add(pInfo.pname ?: "")
}
appOrder = orderedItems.toCollection(ArrayList())
return matchedItems
}

Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/agronick/launcher/Reorderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.agronick.launcher

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.util.Log
import androidx.core.animation.doOnEnd
import util.geometry.Circle
import util.geometry.Vector2
Expand Down Expand Up @@ -67,6 +66,7 @@ class Reorderer(
} else {
animateAppPosition(overApp, suppressedAppCopy.left, suppressedAppCopy.top)
animateAppPosition(app, overApp.left, overApp.top)
PreferenceManager.swap(app.pkgInfo, overApp.pkgInfo)
}
ValueAnimator.ofInt((suppressedAppCopy.size * 1.4).toInt(), suppressedAppCopy.size)
.apply {
Expand All @@ -82,11 +82,10 @@ class Reorderer(
}


fun checkAtEdge(offsetVector: Vector2, lastCircle: Circle?, radius: Int): Vector2? {
fun checkAtEdge(offsetVector: Vector2, lastCircle: Circle?, density: Float): Vector2? {
if (lastCircle == null) return null
val maxDistance = lastCircle.r
Log.d("offset", "${offsetVector.distance(lastCircle.c)} >= ${maxDistance}")
if (offsetVector.distance(lastCircle.c) >= maxDistance - radius) {
val maxDistance = lastCircle.r * density
if (offsetVector.distance(lastCircle.c) >= maxDistance) {
val angle = Math.toRadians(lastCircle.c.angleBetween(offsetVector)).toFloat()
return Vector2(kotlin.math.sin(angle) * -2, kotlin.math.cos(angle) * 2)
}
Expand Down

0 comments on commit a0e0a69

Please sign in to comment.