From ba2271df398f9baf0c24e61dacfb5f6f9aeec2f0 Mon Sep 17 00:00:00 2001 From: lisonge Date: Sun, 1 Oct 2023 19:47:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=82=AC=E6=B5=AE=E7=AA=97=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E6=97=A0=E6=B3=95=E7=82=B9=E5=87=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../songe/gkd/composition/CanConfigBubble.kt | 5 ++ .../songe/gkd/composition/CanSetupBubble.kt | 5 -- .../gkd/composition/CompositionFbService.kt | 31 +++---- .../li/songe/gkd/composition/Typealias.kt | 4 +- .../li/songe/gkd/debug/FloatingService.kt | 82 ++++++++++++------- settings.gradle.kts | 2 +- 6 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 app/src/main/java/li/songe/gkd/composition/CanConfigBubble.kt delete mode 100644 app/src/main/java/li/songe/gkd/composition/CanSetupBubble.kt diff --git a/app/src/main/java/li/songe/gkd/composition/CanConfigBubble.kt b/app/src/main/java/li/songe/gkd/composition/CanConfigBubble.kt new file mode 100644 index 000000000..8b2e1ad98 --- /dev/null +++ b/app/src/main/java/li/songe/gkd/composition/CanConfigBubble.kt @@ -0,0 +1,5 @@ +package li.songe.gkd.composition + +interface CanConfigBubble { + fun configBubble(f: ConfigBubbleHook) +} \ No newline at end of file diff --git a/app/src/main/java/li/songe/gkd/composition/CanSetupBubble.kt b/app/src/main/java/li/songe/gkd/composition/CanSetupBubble.kt deleted file mode 100644 index 12fb9341d..000000000 --- a/app/src/main/java/li/songe/gkd/composition/CanSetupBubble.kt +++ /dev/null @@ -1,5 +0,0 @@ -package li.songe.gkd.composition - -interface CanSetupBubble { - fun setupBubble(f:SetupBubbleHook): Unit -} \ No newline at end of file diff --git a/app/src/main/java/li/songe/gkd/composition/CompositionFbService.kt b/app/src/main/java/li/songe/gkd/composition/CompositionFbService.kt index f4bd1e94c..3ac96717f 100644 --- a/app/src/main/java/li/songe/gkd/composition/CompositionFbService.kt +++ b/app/src/main/java/li/songe/gkd/composition/CompositionFbService.kt @@ -1,11 +1,15 @@ package li.songe.gkd.composition -import com.torrydo.floatingbubbleview.FloatingBubble -import com.torrydo.floatingbubbleview.FloatingBubbleService +import com.torrydo.floatingbubbleview.service.expandable.BubbleBuilder +import com.torrydo.floatingbubbleview.service.expandable.ExpandableBubbleService open class CompositionFbService( private val block: CompositionFbService.() -> Unit, -) : FloatingBubbleService(), CanOnDestroy, CanSetupBubble { +) : ExpandableBubbleService(), CanOnDestroy, CanConfigBubble { + + + override fun configExpandedBubble() = null + override fun onCreate() { block() super.onCreate() @@ -18,21 +22,20 @@ open class CompositionFbService( destroyHooks.forEach { f -> f() } } - override fun setupBubble(action: FloatingBubble.Action): FloatingBubble.Builder { - var result: FloatingBubble.Builder? = null - setupBubbleHooks.forEach { f -> - f(action) { + private val configBubbleHooks by lazy { linkedSetOf() } + override fun configBubble(f: ConfigBubbleHook) { + configBubbleHooks.add(f) + } + + override fun configBubble(): BubbleBuilder? { + var result: BubbleBuilder? = null + configBubbleHooks.forEach { f -> + f { result = it } } - return result ?: throw error("setupBubble result must be resolved") - } - - private val setupBubbleHooks by lazy { linkedSetOf() } - override fun setupBubble(f: SetupBubbleHook) { - setupBubbleHooks.add(f) + return result } - } \ No newline at end of file diff --git a/app/src/main/java/li/songe/gkd/composition/Typealias.kt b/app/src/main/java/li/songe/gkd/composition/Typealias.kt index 13484a8fc..aca3fbca1 100644 --- a/app/src/main/java/li/songe/gkd/composition/Typealias.kt +++ b/app/src/main/java/li/songe/gkd/composition/Typealias.kt @@ -2,10 +2,10 @@ package li.songe.gkd.composition import android.content.Context import android.content.Intent -import com.torrydo.floatingbubbleview.FloatingBubble +import com.torrydo.floatingbubbleview.service.expandable.BubbleBuilder typealias StartCommandHook = (intent: Intent?, flags: Int, startId: Int) -> Unit typealias onReceiveType = (Context?, Intent?) -> Unit -typealias SetupBubbleHook = (FloatingBubble.Action, (FloatingBubble.Builder) -> Unit) -> Unit \ No newline at end of file +typealias ConfigBubbleHook = ((BubbleBuilder) -> Unit) -> Unit \ No newline at end of file diff --git a/app/src/main/java/li/songe/gkd/debug/FloatingService.kt b/app/src/main/java/li/songe/gkd/debug/FloatingService.kt index f621db2d1..189d8cb68 100644 --- a/app/src/main/java/li/songe/gkd/debug/FloatingService.kt +++ b/app/src/main/java/li/songe/gkd/debug/FloatingService.kt @@ -1,64 +1,88 @@ package li.songe.gkd.debug -import android.app.Notification import android.content.Context import android.content.Intent -import androidx.compose.foundation.clickable -import androidx.compose.foundation.interaction.MutableInteractionSource +import android.view.ViewConfiguration import androidx.compose.foundation.layout.size import androidx.compose.material.Icon -import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp -import androidx.core.app.NotificationCompat import com.blankj.utilcode.util.ServiceUtils import com.blankj.utilcode.util.ToastUtils -import com.torrydo.floatingbubbleview.FloatingBubble +import com.torrydo.floatingbubbleview.FloatingBubbleListener +import com.torrydo.floatingbubbleview.service.expandable.BubbleBuilder import kotlinx.coroutines.Dispatchers import li.songe.gkd.app import li.songe.gkd.appScope import li.songe.gkd.composition.CompositionExt.useLifeCycleLog import li.songe.gkd.composition.CompositionFbService +import li.songe.gkd.data.Tuple3 +import li.songe.gkd.notif.createNotif import li.songe.gkd.notif.floatingChannel import li.songe.gkd.notif.floatingNotif import li.songe.gkd.util.SafeR import li.songe.gkd.util.launchTry +import kotlin.math.sqrt class FloatingService : CompositionFbService({ useLifeCycleLog() - setupBubble { _, resolve -> - val builder = FloatingBubble.Builder(this).bubble { - Icon(painter = painterResource(SafeR.ic_capture), + + configBubble { resolve -> + val builder = BubbleBuilder(this).bubbleCompose { + Icon( + painter = painterResource(SafeR.ic_capture), contentDescription = "capture", - modifier = Modifier - .clickable(indication = null, - interactionSource = remember { MutableInteractionSource() }) { - appScope.launchTry(Dispatchers.IO) { - SnapshotExt.captureSnapshot() - ToastUtils.showShort("快照成功") - } + modifier = Modifier.size(40.dp), + tint = Color.Red + ) + }.enableAnimateToEdge(false) + + // https://github.com/gkd-kit/gkd/issues/62 + // https://github.com/gkd-kit/gkd/issues/61 + val defaultFingerData = Tuple3(0L, 0f, 0f) + var fingerDownData = defaultFingerData + val maxDistanceOffset = 50 + builder.addFloatingBubbleListener(object : FloatingBubbleListener { + override fun onFingerDown(x: Float, y: Float) { + fingerDownData = Tuple3(System.currentTimeMillis(), x, y) + } + + override fun onFingerMove(x: Float, y: Float) { + if (fingerDownData === defaultFingerData) { + return + } + val dx = fingerDownData.t1 - x + val dy = fingerDownData.t2 - y + val distance = sqrt(dx * dx + dy * dy) + if (distance > maxDistanceOffset) { + // reset + fingerDownData = defaultFingerData + } + } + + override fun onFingerUp(x: Float, y: Float) { + if (System.currentTimeMillis() - fingerDownData.t0 < ViewConfiguration.getTapTimeout()) { + // is onClick + appScope.launchTry(Dispatchers.IO) { + SnapshotExt.captureSnapshot() + ToastUtils.showShort("快照成功") } - .size(40.dp), - tint = Color.Red) - }.enableCloseBubble(false).enableAnimateToEdge(false) + } + } + }) resolve(builder) } }) { - - override fun initialNotification(): Notification { - return NotificationCompat.Builder(this, floatingChannel.id).setOngoing(true) - .setSmallIcon(SafeR.ic_launcher).setContentTitle(floatingNotif.title) - .setContentText(floatingNotif.text).setPriority(NotificationCompat.PRIORITY_DEFAULT) - .setCategory(Notification.CATEGORY_SERVICE).build() + override fun onCreate() { + super.onCreate() + minimize() } - override fun notificationId() = floatingNotif.id - - override fun createNotificationChannel(channelId: String, channelName: String) { -// by app init + override fun startNotificationForeground() { + createNotif(this, floatingChannel.id, floatingNotif) } companion object { diff --git a/settings.gradle.kts b/settings.gradle.kts index 8311b5c2f..bd54d916a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -94,7 +94,7 @@ dependencyResolutionManagement { // https://github.com/journeyapps/zxing-android-embedded library("others.zxing.android.embedded", "com.journeyapps:zxing-android-embedded:4.3.0") // https://github.com/TorryDo/Floating-Bubble-View - library("others.floating.bubble.view", "io.github.torrydo:floating-bubble-view:0.5.6") + library("others.floating.bubble.view", "io.github.torrydo:floating-bubble-view:0.6.1") library("androidx.appcompat", "androidx.appcompat:appcompat:1.6.1") library("androidx.core.ktx", "androidx.core:core-ktx:1.12.0")