|
| 1 | +/* |
| 2 | + * Nextcloud Talk - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Enrique López-Mañas <eenriquelopez@gmail.com> |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package com.nextcloud.talk.ui |
| 8 | + |
| 9 | +import android.content.Context |
| 10 | +import android.util.AttributeSet |
| 11 | +import android.view.MotionEvent |
| 12 | +import android.view.View |
| 13 | +import android.widget.FrameLayout |
| 14 | +import androidx.customview.widget.ViewDragHelper |
| 15 | +import kotlin.math.abs |
| 16 | + |
| 17 | +class SwipeToCloseLayout @JvmOverloads constructor( |
| 18 | + context: Context, |
| 19 | + attrs: AttributeSet? = null, |
| 20 | + defStyleAttr: Int = 0 |
| 21 | +) : FrameLayout(context, attrs, defStyleAttr) { |
| 22 | + |
| 23 | + private var dragHelper: ViewDragHelper |
| 24 | + private var swipeListener: OnSwipeToCloseListener? = null |
| 25 | + |
| 26 | + interface OnSwipeToCloseListener { |
| 27 | + fun onSwipeToClose() |
| 28 | + } |
| 29 | + |
| 30 | + init { |
| 31 | + dragHelper = ViewDragHelper.create(this, 1.0f, DragCallback()) |
| 32 | + } |
| 33 | + |
| 34 | + fun setOnSwipeToCloseListener(listener: OnSwipeToCloseListener) { |
| 35 | + this.swipeListener = listener |
| 36 | + } |
| 37 | + |
| 38 | + override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { |
| 39 | + val action = ev.actionMasked |
| 40 | + if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { |
| 41 | + dragHelper.cancel() |
| 42 | + return false |
| 43 | + } |
| 44 | + return dragHelper.shouldInterceptTouchEvent(ev) |
| 45 | + } |
| 46 | + |
| 47 | + override fun onTouchEvent(ev: MotionEvent): Boolean { |
| 48 | + dragHelper.processTouchEvent(ev) |
| 49 | + return true |
| 50 | + } |
| 51 | + |
| 52 | + private inner class DragCallback : ViewDragHelper.Callback() { |
| 53 | + override fun tryCaptureView(child: View, pointerId: Int): Boolean { |
| 54 | + return true // Capture any child view |
| 55 | + } |
| 56 | + |
| 57 | + override fun getViewVerticalDragRange(child: View): Int { |
| 58 | + return height |
| 59 | + } |
| 60 | + |
| 61 | + override fun clampViewPositionVertical(child: View, therapeutic: Int, dy: Int): Int { |
| 62 | + return therapeutic |
| 63 | + } |
| 64 | + |
| 65 | + override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) { |
| 66 | + val totalDragDistance = abs(releasedChild.top) |
| 67 | + if (totalDragDistance > height * DRAG_THRESHOLD || abs(yvel) > dragHelper.minVelocity) { |
| 68 | + swipeListener?.onSwipeToClose() |
| 69 | + } else { |
| 70 | + dragHelper.settleCapturedViewAt(0, 0) |
| 71 | + invalidate() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + override fun onViewPositionChanged(changedView: View, left: Int, top: Int, dx: Int, dy: Int) { |
| 76 | + val progress = 1f - (abs(top).toFloat() / height) |
| 77 | + alpha = progress.coerceIn(MIN_ALPHA, MAX_ALPHA) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override fun computeScroll() { |
| 82 | + if (dragHelper.continueSettling(true)) { |
| 83 | + postInvalidateOnAnimation() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + companion object { |
| 88 | + private const val DRAG_THRESHOLD = 0.3f |
| 89 | + private const val MIN_ALPHA = 0.5f |
| 90 | + private const val MAX_ALPHA = 1.0f |
| 91 | + } |
| 92 | +} |
0 commit comments