diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/MyBubbleLayout.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/MyBubbleLayout.kt index e80b040..268619e 100644 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/MyBubbleLayout.kt +++ b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/MyBubbleLayout.kt @@ -2,6 +2,7 @@ package com.torrydo.floatingbubbleview import android.content.Context import android.util.AttributeSet +import android.view.KeyEvent import android.view.MotionEvent import android.widget.LinearLayout @@ -16,6 +17,19 @@ internal open class MyBubbleLayout( internal var ignoreChildEvent: (MotionEvent) -> Boolean = { false } internal var doOnTouchEvent: (MotionEvent) -> Unit = {} + private var onDispatchKeyEvent: ((KeyEvent) -> Boolean?)? = null + + fun setOnDispatchKeyEvent(callback: ((KeyEvent) -> Boolean?)){ + onDispatchKeyEvent = callback + } + + override fun dispatchKeyEvent(event: KeyEvent?): Boolean { + if (onDispatchKeyEvent != null && event != null) { + return onDispatchKeyEvent!!(event) ?: super.dispatchKeyEvent(event) + } + return super.dispatchKeyEvent(event) + } + override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean { val b = ev?.let{ doOnTouchEvent(it) diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/MyFloatingComposeView.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/MyFloatingComposeView.kt deleted file mode 100644 index 70c4f68..0000000 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/MyFloatingComposeView.kt +++ /dev/null @@ -1,56 +0,0 @@ -package com.torrydo.floatingbubbleview - -import android.content.Context -import android.util.AttributeSet -import android.view.KeyEvent -import androidx.annotation.Discouraged -import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateOf -import androidx.compose.ui.platform.AbstractComposeView - -// maybe removed in the near future -@Discouraged("may be removed in the near future") -internal class MyFloatingComposeView( - context: Context, - attrs: AttributeSet? = null, - defStyleAttr: Int = 0 -) : AbstractComposeView(context, attrs, defStyleAttr) { - - companion object { - internal var keyEventHandler: ((KeyEvent?) -> Boolean?)? = null - } - - private val content = mutableStateOf<(@Composable () -> Unit)?>(null) - - - @Suppress("RedundantVisibilityModifier") - protected override var shouldCreateCompositionOnAttachedToWindow: Boolean = false - - @Composable - override fun Content() { - content.value?.invoke() - } - - override fun getAccessibilityClassName(): CharSequence { - return javaClass.name - } - - /** - * Set the Jetpack Compose UI content for this view. - * Initial composition will occur when the view becomes attached to a window or when - * [createComposition] is called, whichever comes first. - */ - fun setContent(content: @Composable () -> Unit) { - shouldCreateCompositionOnAttachedToWindow = true - this.content.value = content - if (isAttachedToWindow) { - createComposition() - } - } - - override fun dispatchKeyEvent(event: KeyEvent?): Boolean { - val b = keyEventHandler?.invoke(event) - return b ?: super.dispatchKeyEvent(event) - } - -} \ No newline at end of file diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt index 79a1910..359bede 100644 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt +++ b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt @@ -4,9 +4,7 @@ import android.annotation.SuppressLint import android.content.Context import android.graphics.Point import android.graphics.PointF -import android.util.Log import android.view.* -import android.widget.FrameLayout import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringForce import com.torrydo.floatingbubbleview.AnimHelper @@ -28,12 +26,9 @@ class FloatingBubble( onDispatchKeyEvent: ((KeyEvent) -> Boolean?)? = null ) : Bubble( context = context, - root = object : MyBubbleLayout(context) { - override fun dispatchKeyEvent(event: KeyEvent): Boolean { - if(onDispatchKeyEvent != null){ - return onDispatchKeyEvent(event) ?: super.dispatchKeyEvent(event) - } - return super.dispatchKeyEvent(event) + root = LayoutInflater.from(context).inflate(R.layout.bubble, null).apply { + if (onDispatchKeyEvent != null) { + (this as MyBubbleLayout).setOnDispatchKeyEvent(onDispatchKeyEvent) } }, containCompose = containCompose @@ -154,33 +149,23 @@ class FloatingBubble( endY = y, event = object : AnimHelper.Event { override fun onUpdatePoint(x: Float, y: Float) { - layoutParams.x = x.toInt() layoutParams.y = y.toInt() // builder.listener?.onMove(x.toFloat(), y.toFloat()) // don't call this line, it'll spam multiple MotionEvent.OnActionMove update() - } }, stiffness = stiffness, ) } - private fun getX() = context.resources.configuration.smallestScreenWidthDp - - // private val MAX_XY_MOVE = 80f + private val MAX_XY_MOVE = 1f private var ignoreClick: Boolean = false @SuppressLint("ClickableViewAccessibility") private fun customTouch() { - val dpi = getX() - val max_xy_move = dpi / 22 -// Log.d("<>", "dpi | xy: ${dpi} - ${max_xy_move}"); -// Log.d("<>", "sdfasfasdf ${context.resources.configuration}: "); - - fun handleMovement(event: MotionEvent) { when (event.action) { MotionEvent.ACTION_DOWN -> { @@ -220,7 +205,9 @@ class FloatingBubble( } MotionEvent.ACTION_MOVE -> { - ignoreClick = abs(event.rawX - rawPointOnDown.x) > max_xy_move || abs(event.rawY - rawPointOnDown.y) > max_xy_move + if (abs(event.rawX - rawPointOnDown.x) > MAX_XY_MOVE || abs(event.rawY - rawPointOnDown.y) > MAX_XY_MOVE) { + ignoreClick = true + } } } diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/helper/ComposeHelper.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/helper/ComposeHelper.kt deleted file mode 100644 index ff03dcf..0000000 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/helper/ComposeHelper.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.torrydo.floatingbubbleview.helper - -import android.view.KeyEvent -import androidx.annotation.Discouraged -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import com.torrydo.floatingbubbleview.MyFloatingComposeView - -/** - * override "dispatchKeyEvent" of the floating composable - * - * */ -@Deprecated("removed") -@Composable -internal fun OverrideDispatchKeyEvent(handler: (KeyEvent?) -> Boolean?) { - DisposableEffect(Unit){ - MyFloatingComposeView.keyEventHandler = handler - onDispose { -// FloatingComposeView.keyEventHandler = null - } - } -} \ No newline at end of file diff --git a/README.md b/README.md index 48207b4..74e8efc 100644 --- a/README.md +++ b/README.md @@ -361,18 +361,25 @@ class MyServiceKt : ExpandableBubbleService() { .expandedCompose { ExpandedCompose() } + // handle key code .onDispatchKeyEvent { if(it.keyCode == KeyEvent.KEYCODE_BACK){ minimize() } null } + // set start location in dp .startLocation(0, 0) + // allow expanded bubble can be draggable or not .draggable(true) + // fade animation by default .style(null) + // .fillMaxWidth(true) + // animate to the left/right side when release, trfalseue by default .enableAnimateToEdge(true) - .dimAmount(0.9f) + // set background dimmer + .dimAmount(0.6f) } } ``` diff --git a/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt b/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt index c5e8187..61f995b 100644 --- a/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt +++ b/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt @@ -36,12 +36,12 @@ class MyServiceKt : ExpandableBubbleService() { return BubbleBuilder(this) // set bubble view -// .bubbleView(imgView) + .bubbleView(imgView) // or our sweetie, Jetpack Compose - .bubbleCompose { - BubbleCompose(expand = { expand() }) - } +// .bubbleCompose { +// BubbleCompose(expand = { expand() }) +// } // .forceDragging(false) // set style for the bubble, fade animation by default diff --git a/gradle.properties b/gradle.properties index 1a532eb..4c9f6c5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -27,8 +27,8 @@ RELEASE_SIGNING_ENABLED=true GROUP=io.github.torrydo POM_ARTIFACT_ID=floating-bubble-view -VERSION_NAME=0.6.2 -#prev: 0.6.1 +VERSION_NAME=0.6.3 +#prev: 0.6.2 POM_NAME=FloatingBubbleView POM_PACKAGING=aar