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"
|
kotlinx-datetime = "0.8.0"
|
||||||
tyme4kt = "1.4.5"
|
tyme4kt = "1.4.5"
|
||||||
sketch = "4.4.0"
|
sketch = "4.4.0"
|
||||||
navigationevent = "1.0.1"
|
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
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-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-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" }
|
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]
|
[plugins]
|
||||||
androidApplication = { id = "com.android.application", version.ref = "agp" }
|
androidApplication = { id = "com.android.application", version.ref = "agp" }
|
||||||
|
|||||||
@ -49,7 +49,6 @@ kotlin {
|
|||||||
implementation(libs.sketch.compose)
|
implementation(libs.sketch.compose)
|
||||||
implementation(libs.sketch.animated.gif)
|
implementation(libs.sketch.animated.gif)
|
||||||
implementation(libs.sketch.compose.resources)
|
implementation(libs.sketch.compose.resources)
|
||||||
implementation(libs.navigationevent.compose)
|
|
||||||
}
|
}
|
||||||
commonTest.dependencies {
|
commonTest.dependencies {
|
||||||
implementation(libs.kotlin.test)
|
implementation(libs.kotlin.test)
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
package plus.rua.project
|
package plus.rua.project
|
||||||
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import androidx.activity.BackEventCompat
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
|
import androidx.activity.compose.PredictiveBackHandler
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import kotlinx.coroutines.CancellationException
|
||||||
import androidx.navigationevent.NavigationEventInfo
|
import kotlinx.coroutines.flow.Flow
|
||||||
import androidx.navigationevent.NavigationEventTransitionState
|
|
||||||
import androidx.navigationevent.compose.NavigationBackHandler
|
|
||||||
import androidx.navigationevent.compose.rememberNavigationEventState
|
|
||||||
|
|
||||||
class AndroidPlatform : Platform {
|
class AndroidPlatform : Platform {
|
||||||
override val name: String = "Android ${Build.VERSION.SDK_INT}"
|
override val name: String = "Android ${Build.VERSION.SDK_INT}"
|
||||||
@ -26,19 +25,15 @@ actual fun PredictiveBackHandler(
|
|||||||
onBack: () -> Unit,
|
onBack: () -> Unit,
|
||||||
onCancel: () -> Unit
|
onCancel: () -> Unit
|
||||||
) {
|
) {
|
||||||
val navState = rememberNavigationEventState(NavigationEventInfo.None)
|
// 官方 PredictiveBackHandler — Flow 模式:collect 完成=返回,CancellationException=取消
|
||||||
|
PredictiveBackHandler(enabled = enabled) { progress: Flow<BackEventCompat> ->
|
||||||
NavigationBackHandler(
|
try {
|
||||||
state = navState,
|
progress.collect { event ->
|
||||||
isBackEnabled = enabled,
|
onProgress(event.progress)
|
||||||
onBackCancelled = onCancel,
|
}
|
||||||
onBackCompleted = onBack
|
onBack()
|
||||||
)
|
} catch (e: CancellationException) {
|
||||||
|
onCancel()
|
||||||
LaunchedEffect(navState.transitionState) {
|
|
||||||
val ts = navState.transitionState
|
|
||||||
if (ts is NavigationEventTransitionState.InProgress) {
|
|
||||||
onProgress(ts.latestEvent.progress)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user