Skip to content

Commit

Permalink
Fix URL Navigate SubjectDetailsPage (#1748)
Browse files Browse the repository at this point in the history
Fix
  • Loading branch information
WoLeo-Z authored and Him188 committed Mar 1, 2025
1 parent 9bdabe3 commit 704f462
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion app/android/src/main/kotlin/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package me.him188.ani.android.activity

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.SystemBarStyle
Expand All @@ -17,6 +18,7 @@ import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.CompositionLocalProvider
import androidx.core.view.WindowCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.him188.ani.app.domain.session.SessionManager
import me.him188.ani.app.navigation.AniNavigator
Expand All @@ -29,6 +31,7 @@ import me.him188.ani.app.ui.foundation.widgets.LocalToaster
import me.him188.ani.app.ui.foundation.widgets.Toaster
import me.him188.ani.app.ui.main.AniApp
import me.him188.ani.app.ui.main.AniAppContent
import me.him188.ani.utils.logging.error
import me.him188.ani.utils.logging.logger
import org.koin.android.ext.android.inject

Expand All @@ -39,8 +42,34 @@ class MainActivity : AniComponentActivity() {
private val logger = logger<MainActivity>()
private val aniNavigator = AniNavigator()

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)

handleStartIntent(intent)
}

private fun handleStartIntent(intent: Intent) {
val data = intent.data ?: return
if (data.scheme != "ani") return
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, placeholder = null)
} catch (e: Exception) {
logger.error(e) { "Failed to navigate to subject details" }
}
}
}
}

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

enableEdgeToEdge(
// 透明状态栏
Expand All @@ -67,7 +96,7 @@ class MainActivity : AniComponentActivity() {
setContent {
AniApp {
SystemBarColorEffect()

CompositionLocalProvider(
LocalToaster provides toaster,
LocalPlatformWindow provides rememberPlatformWindow(this),
Expand Down

0 comments on commit 704f462

Please # to comment.