Skip to content

Commit

Permalink
feat: optimize the selection logic so that the action mode UI is not …
Browse files Browse the repository at this point in the history
…out of scope.
  • Loading branch information
plateaukao committed Oct 20, 2023
1 parent 6e5f69f commit 13e4357
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import java.io.File
import kotlin.math.abs
import kotlin.math.floor
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -1594,30 +1595,22 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
override fun onSwipeLeft() = gestureHandler.handle(config.multitouchLeft)
override fun onLongPressMove(motionEvent: MotionEvent) {
super.onLongPressMove(motionEvent)
actionModeMenuViewModel.updateClickedPoint(motionEvent.toPoint())
actionModeMenuViewModel.hide()
if (abs(motionEvent.x - actionModeMenuViewModel.clickedPoint.value.x) > ViewUnit.dpToPixel(this@BrowserActivity, 8) ||
abs(motionEvent.y - actionModeMenuViewModel.clickedPoint.value.y) > ViewUnit.dpToPixel(this@BrowserActivity, 8)) {
actionModeMenuViewModel.updateClickedPoint(motionEvent.toPoint())
actionModeMenuViewModel.hide()
}
Log.d("touch", "onLongPress")
}

override fun onMoveDone(motionEvent: MotionEvent) {
actionModeMenuViewModel.updateClickedPoint(motionEvent.toPoint())
if (abs(motionEvent.x - actionModeMenuViewModel.clickedPoint.value.x) > ViewUnit.dpToPixel(this@BrowserActivity, 8) ||
abs(motionEvent.y - actionModeMenuViewModel.clickedPoint.value.y) > ViewUnit.dpToPixel(this@BrowserActivity, 8)) {
actionModeMenuViewModel.updateClickedPoint(motionEvent.toPoint())
}
actionModeMenuViewModel.show()
Log.d("touch", "onMoveDone")
}

// override fun onTouchDown(motionEvent: MotionEvent) {
// Log.d("touch", "onTouchDown")
// if (actionModeMenuViewModel.isInActionMode()) {
// actionModeMenuViewModel.updateClickedPoint(motionEvent.toPoint())
// }
// }
//
// override fun onTouchUp(motionEvent: MotionEvent) {
// Log.d("touch", "onTouchUp")
// if (actionModeMenuViewModel.isInActionMode()) {
// actionModeMenuViewModel.updateClickedPoint(motionEvent.toPoint())
// actionModeMenuViewModel.show()
// }
// }
}.apply { lifecycle.addObserver(this) }

private fun updateSavedAlbumInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,40 @@ class ActionModeMenuViewModel : ViewModel(), KoinComponent {
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
actionModeView?.x = _clickedPoint.value.x.toFloat()
actionModeView?.y = _clickedPoint.value.y + ViewUnit.dpToPixel(context, 5)

actionModeView?.visibility = INVISIBLE
viewGroup.addView(actionModeView)

actionModeView?.post { checkBoundary() }
actionModeView?.post {
updatePosition(clickedPoint.value)
actionModeView?.visibility = VISIBLE
}

viewModelScope.launch {
clickedPoint.collect { anchorPoint ->
actionModeView?.x = anchorPoint.x.toFloat()
actionModeView?.y = (anchorPoint.y + ViewUnit.dpToPixel(context, 5))
actionModeView?.post { checkBoundary() }
}
clickedPoint.collect { updatePosition(it) }
}
}

private fun checkBoundary() {
private fun updatePosition(point: Point) {
val view = actionModeView ?: return
val properPoint = getProperPosition(point)
view.x = properPoint.x.toFloat()
view.y = properPoint.y + ViewUnit.dpToPixel(view.context, 5)
}

private fun getProperPosition(point: Point): Point {
val view = actionModeView ?: return Point(0, 0)
val parentWidth = (view.parent as View).width
val parentHeight = (view.parent as View).height

val width = view.width
val height = view.height
// Calculate the new position to ensure the view is within bounds
val padding = ViewUnit.dpToPixel(view.context, 10)
view.x =
if (view.x + view.width + padding > parentWidth) parentWidth - view.width - padding else view.x
view.y =
if (view.y + view.height + padding > parentHeight) parentHeight - view.height - padding else view.y
val x =
if (point.x + width + padding > parentWidth) parentWidth - width - padding else point.x
val y =
if (point.y + height + padding > parentHeight) parentHeight - height - padding else point.y

return Point(x.toInt(), y.toInt())
}

fun finish() {
Expand Down

0 comments on commit 13e4357

Please sign in to comment.