Skip to content

Commit

Permalink
修复 intent 跳转条目详情
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Nov 16, 2024
1 parent a6fab13 commit 915b920
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
24 changes: 18 additions & 6 deletions app/android/src/main/kotlin/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.core.view.WindowCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import coil3.compose.LocalPlatformContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import me.him188.ani.app.data.models.preference.configIfEnabledOrNull
Expand Down Expand Up @@ -69,20 +70,31 @@ class MainActivity : AniComponentActivity() {
}
}

handleStartIntent(intent)
}

private fun handleStartIntent(intent: Intent) {
val data = intent.data ?: return
if (data.scheme != "ani") return
if (data.pathSegments.getOrNull(0) == "subjects") {
val id = data.pathSegments.getOrNull(1)?.toIntOrNull() ?: return
try {
aniNavigator.navigateSubjectDetails(id)
} catch (e: Exception) {
logger.error(e) { "Failed to navigate to subject details" }
if (data.host == "subjects") {
val id = data.pathSegments.getOrNull(0)?.toIntOrNull() ?: return
lifecycleScope.launch {
try {
if (!aniNavigator.isNavControllerReady()) {
aniNavigator.awaitNavController()
delay(1000) // 等待初始化好, 否则跳转可能无效
}
aniNavigator.navigateSubjectDetails(id)
} catch (e: Exception) {
logger.error(e) { "Failed to navigate to subject details" }
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleStartIntent(intent)

enableEdgeToEdge(
// 透明状态栏
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ interface AniNavigator {
controller: NavHostController,
)

fun isNavControllerReady(): Boolean

suspend fun awaitNavController(): NavHostController

val navigator: NavHostController
Expand Down Expand Up @@ -139,6 +141,8 @@ private class AniNavigatorImpl : AniNavigator {
this._navigator.complete(controller)
}

override fun isNavControllerReady(): Boolean = _navigator.isCompleted

override suspend fun awaitNavController(): NavHostController {
return _navigator.await()
}
Expand Down

0 comments on commit 915b920

Please sign in to comment.