Change button behaviour to match usr expectations

pull/202/head
Haafingar 4 years ago committed by GitHub
parent a6b9012aec
commit 1b9908377c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -209,23 +209,26 @@ class NewConversationButtonSetView : RelativeLayout {
// region Interaction
override fun onTouchEvent(event: MotionEvent): Boolean {
val touch = PointF(event.x, event.y)
val expandedButton = expandedButton
val allButtons = listOf( mainButton, sessionButton, closedGroupButton, openGroupButton )
val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton )
if (allButtons.none { it.contains(touch) }) { return false }
when (event.action) {
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
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(50, DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(50)
}
if (!isExpanded && mainButton.contains(touch)) {
expand()
} else if (buttonsExcludingMainButton.none { it.contains(touch) }) {
collapse()
}
}
MotionEvent.ACTION_MOVE -> {
mainButton.x = touch.x - mainButton.expandedSize / 2
@ -249,22 +252,27 @@ class NewConversationButtonSetView : RelativeLayout {
}
}
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()
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
return true
}
private fun expand() {
val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton )
@ -295,4 +303,4 @@ interface NewConversationButtonSetViewDelegate {
fun createNewPrivateChat()
fun createNewClosedGroup()
}
// endregion
// endregion

Loading…
Cancel
Save