refactor: 迁移至官方 PredictiveBackHandler,移除 navigationevent-compose 依赖
使用 androidx.activity.compose.PredictiveBackHandler (Flow<BackEventCompat> 模式) 替换 JetBrains NavigationBackHandler,提升设备兼容性。
This commit is contained in:
parent
6542362f6f
commit
6ef51a4879
@ -16,7 +16,6 @@ material3 = "1.10.0-alpha05"
|
||||
kotlinx-datetime = "0.8.0"
|
||||
tyme4kt = "1.4.5"
|
||||
sketch = "4.4.0"
|
||||
navigationevent = "1.0.1"
|
||||
|
||||
[libraries]
|
||||
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||
@ -35,7 +34,6 @@ tyme4kt = { module = "cn.6tail:tyme4kt", version.ref = "tyme4kt" }
|
||||
sketch-compose = { module = "io.github.panpf.sketch4:sketch-compose", version.ref = "sketch" }
|
||||
sketch-animated-gif = { module = "io.github.panpf.sketch4:sketch-animated-gif", version.ref = "sketch" }
|
||||
sketch-compose-resources = { module = "io.github.panpf.sketch4:sketch-compose-resources", version.ref = "sketch" }
|
||||
navigationevent-compose = { module = "org.jetbrains.androidx.navigationevent:navigationevent-compose", version.ref = "navigationevent" }
|
||||
|
||||
[plugins]
|
||||
androidApplication = { id = "com.android.application", version.ref = "agp" }
|
||||
|
||||
@ -49,7 +49,6 @@ kotlin {
|
||||
implementation(libs.sketch.compose)
|
||||
implementation(libs.sketch.animated.gif)
|
||||
implementation(libs.sketch.compose.resources)
|
||||
implementation(libs.navigationevent.compose)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(libs.kotlin.test)
|
||||
|
||||
@ -1,13 +1,12 @@
|
||||
package plus.rua.project
|
||||
|
||||
import android.os.Build
|
||||
import androidx.activity.BackEventCompat
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.PredictiveBackHandler
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.navigationevent.NavigationEventInfo
|
||||
import androidx.navigationevent.NavigationEventTransitionState
|
||||
import androidx.navigationevent.compose.NavigationBackHandler
|
||||
import androidx.navigationevent.compose.rememberNavigationEventState
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class AndroidPlatform : Platform {
|
||||
override val name: String = "Android ${Build.VERSION.SDK_INT}"
|
||||
@ -26,19 +25,15 @@ actual fun PredictiveBackHandler(
|
||||
onBack: () -> Unit,
|
||||
onCancel: () -> Unit
|
||||
) {
|
||||
val navState = rememberNavigationEventState(NavigationEventInfo.None)
|
||||
|
||||
NavigationBackHandler(
|
||||
state = navState,
|
||||
isBackEnabled = enabled,
|
||||
onBackCancelled = onCancel,
|
||||
onBackCompleted = onBack
|
||||
)
|
||||
|
||||
LaunchedEffect(navState.transitionState) {
|
||||
val ts = navState.transitionState
|
||||
if (ts is NavigationEventTransitionState.InProgress) {
|
||||
onProgress(ts.latestEvent.progress)
|
||||
// 官方 PredictiveBackHandler — Flow 模式:collect 完成=返回,CancellationException=取消
|
||||
PredictiveBackHandler(enabled = enabled) { progress: Flow<BackEventCompat> ->
|
||||
try {
|
||||
progress.collect { event ->
|
||||
onProgress(event.progress)
|
||||
}
|
||||
onBack()
|
||||
} catch (e: CancellationException) {
|
||||
onCancel()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user