Change button behaviour to match usr expectations

This commit is contained in:
Haafingar 2020-05-27 17:40:08 +10:00 committed by GitHub
parent a6b9012aec
commit 1b9908377c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -209,23 +209,26 @@ class NewConversationButtonSetView : RelativeLayout {
// region Interaction // region Interaction
override fun onTouchEvent(event: MotionEvent): Boolean { override fun onTouchEvent(event: MotionEvent): Boolean {
val touch = PointF(event.x, event.y) val touch = PointF(event.x, event.y)
val expandedButton = expandedButton
val allButtons = listOf( mainButton, sessionButton, closedGroupButton, openGroupButton ) val allButtons = listOf( mainButton, sessionButton, closedGroupButton, openGroupButton )
val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton ) val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton )
if (allButtons.none { it.contains(touch) }) { return false } if (allButtons.none { it.contains(touch) }) { return false }
when (event.action) { when (event.action) {
MotionEvent.ACTION_DOWN -> { MotionEvent.ACTION_DOWN -> {
if (isExpanded) {
if (sessionButton.contains(touch)) { delegate?.createNewPrivateChat(); collapse()}
else if (closedGroupButton.contains(touch)) { delegate?.createNewClosedGroup(); collapse() }
else if (openGroupButton.contains(touch)) { delegate?.joinOpenGroup(); collapse() }
else if (mainButton.contains(touch)) { collapse() }
} else {
isExpanded = !isExpanded
expand()
}
val vibrator = context.getSystemService(VIBRATOR_SERVICE) as Vibrator val vibrator = context.getSystemService(VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(50, DEFAULT_AMPLITUDE)) vibrator.vibrate(VibrationEffect.createOneShot(50, DEFAULT_AMPLITUDE))
} else { } else {
vibrator.vibrate(50) vibrator.vibrate(50)
} }
if (!isExpanded && mainButton.contains(touch)) {
expand()
} else if (buttonsExcludingMainButton.none { it.contains(touch) }) {
collapse()
}
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
mainButton.x = touch.x - mainButton.expandedSize / 2 mainButton.x = touch.x - mainButton.expandedSize / 2
@ -249,22 +252,27 @@ class NewConversationButtonSetView : RelativeLayout {
} }
} }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
if (previousAction == MotionEvent.ACTION_MOVE || isExpanded) { val distanceFromRestPosition = touch.distanceTo(buttonRestPosition)
if (distanceFromRestPosition > (minDragDistance + mainButton.collapsedSize / 2)) {
if (sessionButton.contains(touch) || touch.isAbove(sessionButton, dragMargin)) { delegate?.createNewPrivateChat(); collapse()}
else if (closedGroupButton.contains(touch) || touch.isRightOf(closedGroupButton, dragMargin)) { delegate?.createNewClosedGroup(); collapse() }
else if (openGroupButton.contains(touch) || touch.isLeftOf(openGroupButton, dragMargin)) { delegate?.joinOpenGroup(); collapse() }
else {
isExpanded = !isExpanded
if (!isExpanded) { collapse() }
}
} else {
val currentPosition = PointF(mainButton.x, mainButton.y)
mainButton.animatePositionChange(currentPosition, buttonRestPosition)
val endAlpha = 1.0f
mainButton.animateAlphaChange(mainButton.alpha, endAlpha)
expandedButton?.collapse() expandedButton?.collapse()
this.expandedButton = null this.expandedButton = null
collapse()
val distanceFromRestPosition = touch.distanceTo(buttonRestPosition)
if (event.action == MotionEvent.ACTION_UP && distanceFromRestPosition > (minDragDistance + mainButton.collapsedSize / 2)) {
if (sessionButton.contains(touch) || touch.isAbove(sessionButton, dragMargin)) { delegate?.createNewPrivateChat() }
else if (closedGroupButton.contains(touch) || touch.isRightOf(closedGroupButton, dragMargin)) { delegate?.createNewClosedGroup() }
else if (openGroupButton.contains(touch) || touch.isLeftOf(openGroupButton, dragMargin)) { delegate?.joinOpenGroup() }
}
} }
} }
} }
previousAction = event.action previousAction = event.action
return true return true
}
private fun expand() { private fun expand() {
val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton ) val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton )
@ -295,4 +303,4 @@ interface NewConversationButtonSetViewDelegate {
fun createNewPrivateChat() fun createNewPrivateChat()
fun createNewClosedGroup() fun createNewClosedGroup()
} }
// endregion // endregion