Allow user to tap as well

This commit is contained in:
gmbnt 2020-03-16 15:02:40 +11:00
parent 83feb26225
commit f94bf6301b

View File

@ -27,6 +27,8 @@ import org.thoughtcrime.securesms.loki.toPx
class NewConversationButtonSetView : RelativeLayout { class NewConversationButtonSetView : RelativeLayout {
private var expandedButton: Button? = null private var expandedButton: Button? = null
private var previousAction: Int? = null
private var isExpanded = false
var delegate: NewConversationButtonSetViewDelegate? = null var delegate: NewConversationButtonSetViewDelegate? = null
// region Convenience // region Convenience
@ -205,9 +207,7 @@ class NewConversationButtonSetView : RelativeLayout {
// region Interaction // region Interaction
override fun onTouchEvent(event: MotionEvent): Boolean { override fun onTouchEvent(event: MotionEvent): Boolean {
val touchX = event.x val touch = PointF(event.x, event.y)
val touchY = event.y
val touch = PointF(touchX, touchY)
val expandedButton = expandedButton val expandedButton = expandedButton
val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton ) val buttonsExcludingMainButton = listOf( sessionButton, closedGroupButton, openGroupButton )
when (event.action) { when (event.action) {
@ -218,14 +218,15 @@ class NewConversationButtonSetView : RelativeLayout {
} else { } else {
vibrator.vibrate(50) vibrator.vibrate(50)
} }
sessionButton.animatePositionChange(buttonRestPosition, sessionButtonExpandedPosition) if (!isExpanded && mainButton.contains(touch)) {
closedGroupButton.animatePositionChange(buttonRestPosition, closedGroupButtonExpandedPosition) expand()
openGroupButton.animatePositionChange(buttonRestPosition, openGroupButtonExpandedPosition) } else if (buttonsExcludingMainButton.none { it.contains(touch) }) {
buttonsExcludingMainButton.forEach { it.animateAlphaChange(0.0f, 1.0f) } collapse()
}
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
mainButton.x = touchX - mainButton.expandedSize / 2 mainButton.x = touch.x - mainButton.expandedSize / 2
mainButton.y = touchY - mainButton.expandedSize / 2 mainButton.y = touch.y - mainButton.expandedSize / 2
mainButton.alpha = 1 - (PointF(mainButton.x, mainButton.y).distanceTo(buttonRestPosition) / maxDragDistance) mainButton.alpha = 1 - (PointF(mainButton.x, mainButton.y).distanceTo(buttonRestPosition) / maxDragDistance)
val buttonToExpand = buttonsExcludingMainButton.firstOrNull { button -> val buttonToExpand = buttonsExcludingMainButton.firstOrNull { button ->
var hasUserDraggedBeyondButton = false var hasUserDraggedBeyondButton = false
@ -245,23 +246,40 @@ class NewConversationButtonSetView : RelativeLayout {
} }
} }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
if (previousAction == MotionEvent.ACTION_MOVE || isExpanded) {
expandedButton?.collapse() expandedButton?.collapse()
this.expandedButton = null this.expandedButton = null
val allButtons = listOf( mainButton ) + buttonsExcludingMainButton collapse()
if (event.action == MotionEvent.ACTION_UP) {
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 )
sessionButton.animatePositionChange(buttonRestPosition, sessionButtonExpandedPosition)
closedGroupButton.animatePositionChange(buttonRestPosition, closedGroupButtonExpandedPosition)
openGroupButton.animatePositionChange(buttonRestPosition, openGroupButtonExpandedPosition)
buttonsExcludingMainButton.forEach { it.animateAlphaChange(0.0f, 1.0f) }
postDelayed({ isExpanded = true }, Button.animationDuration)
}
private fun collapse() {
val allButtons = listOf( mainButton, sessionButton, closedGroupButton, openGroupButton )
allButtons.forEach { allButtons.forEach {
val currentPosition = PointF(it.x, it.y) val currentPosition = PointF(it.x, it.y)
it.animatePositionChange(currentPosition, buttonRestPosition) it.animatePositionChange(currentPosition, buttonRestPosition)
val endAlpha = if (it == mainButton) 1.0f else 0.0f val endAlpha = if (it == mainButton) 1.0f else 0.0f
it.animateAlphaChange(it.alpha, endAlpha) it.animateAlphaChange(it.alpha, endAlpha)
} }
if (event.action == MotionEvent.ACTION_UP) { postDelayed({ isExpanded = false }, Button.animationDuration)
if (openGroupButton.contains(touch) || touch.isLeftOf(openGroupButton, dragMargin)) { delegate?.joinOpenGroup() }
else if (sessionButton.contains(touch) || touch.isAbove(sessionButton, dragMargin)) { delegate?.createNewPrivateChat() }
else if (closedGroupButton.contains(touch) || touch.isRightOf(closedGroupButton, dragMargin)) { delegate?.createNewClosedGroup() }
}
}
}
return true
} }
// endregion // endregion
} }