mirror of
https://github.com/gkd-kit/gkd.git
synced 2024-11-15 19:22:26 +08:00
fix: 悬浮窗按钮无法点击
This commit is contained in:
parent
7f0d7f1c71
commit
ba2271df39
|
@ -0,0 +1,5 @@
|
|||
package li.songe.gkd.composition
|
||||
|
||||
interface CanConfigBubble {
|
||||
fun configBubble(f: ConfigBubbleHook)
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package li.songe.gkd.composition
|
||||
|
||||
interface CanSetupBubble {
|
||||
fun setupBubble(f:SetupBubbleHook): Unit
|
||||
}
|
|
@ -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<ConfigBubbleHook>() }
|
||||
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")
|
||||
return result
|
||||
}
|
||||
|
||||
private val setupBubbleHooks by lazy { linkedSetOf<SetupBubbleHook>() }
|
||||
override fun setupBubble(f: SetupBubbleHook) {
|
||||
setupBubbleHooks.add(f)
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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
|
||||
typealias ConfigBubbleHook = ((BubbleBuilder) -> Unit) -> Unit
|
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue
Block a user