Skip to content

Commit

Permalink
[fix]修复快速navController.popBackStack()两次,无内容显示的问题(google/accompanist#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Mar 13, 2023
1 parent d00a278 commit 5ffd236
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
30 changes: 30 additions & 0 deletions app/src/main/java/com/skyd/nightscreen/ext/NavExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.skyd.nightscreen.ext

import android.os.Bundle
import androidx.core.net.toUri
import androidx.lifecycle.Lifecycle
import androidx.navigation.*

fun NavController.navigate(
route: String,
args: Bundle,
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
) {
val routeLink = NavDeepLinkRequest
.Builder
.fromUri(NavDestination.createRoute(route).toUri())
.build()

val deepLinkMatch = graph.matchDeepLink(routeLink)
if (deepLinkMatch != null) {
val destination = deepLinkMatch.destination
val id = destination.id
navigate(id, args, navOptions, navigatorExtras)
} else {
navigate(route, navOptions, navigatorExtras)
}
}

fun NavBackStackEntry.lifecycleIsResumed() =
this.getLifecycle().currentState == Lifecycle.State.RESUMED
14 changes: 13 additions & 1 deletion app/src/main/java/com/skyd/nightscreen/ui/component/NSTopBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.skyd.nightscreen.R
import com.skyd.nightscreen.ext.lifecycleIsResumed
import com.skyd.nightscreen.ui.local.LocalNavController

enum class NSTopBarStyle {
Small, Large
Expand All @@ -24,7 +26,7 @@ enum class NSTopBarStyle {
fun NSTopBar(
style: NSTopBarStyle = NSTopBarStyle.Small,
title: @Composable () -> Unit,
navigationIcon: @Composable () -> Unit = {},
navigationIcon: @Composable () -> Unit = { BackIcon() },
actions: @Composable RowScope.() -> Unit = {},
scrollBehavior: TopAppBarScrollBehavior? = null,
) {
Expand Down Expand Up @@ -90,6 +92,16 @@ fun TopBarIcon(
}
}

@Composable
fun BackIcon() {
val navController = LocalNavController.current
BackIcon {
if (navController.currentBackStackEntry?.lifecycleIsResumed() == true) {
navController.popBackStack()
}
}
}

@Composable
fun BackIcon(onClick: () -> Unit = {}) {
TopBarIcon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.skyd.nightscreen.R
import com.skyd.nightscreen.config.Const
import com.skyd.nightscreen.ui.component.BackIcon
import com.skyd.nightscreen.ui.component.Centered
import com.skyd.nightscreen.ui.component.NSTopBar
import com.skyd.nightscreen.ui.component.NSTopBarStyle
Expand All @@ -47,7 +46,6 @@ const val ABOUT_SCREEN_ROUTE = "aboutScreen"
fun AboutScreen() {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
val context = LocalContext.current
val navController = LocalNavController.current
Scaffold(
topBar = {
NSTopBar(
Expand All @@ -56,11 +54,6 @@ fun AboutScreen() {
Text(text = stringResource(R.string.about))
},
scrollBehavior = scrollBehavior,
navigationIcon = {
BackIcon {
navController.popBackStack()
}
}
)
}
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ import androidx.compose.ui.unit.dp
import com.skyd.nightscreen.R
import com.skyd.nightscreen.bean.LicenseBean
import com.skyd.nightscreen.ext.plus
import com.skyd.nightscreen.ui.component.BackIcon
import com.skyd.nightscreen.ui.component.NSTopBar
import com.skyd.nightscreen.ui.component.NSTopBarStyle
import com.skyd.nightscreen.ui.local.LocalNavController
import com.skyd.nightscreen.util.CommonUtil.openBrowser

const val LICENSE_SCREEN_ROUTE = "licenseScreen"

@Composable
fun LicenseScreen() {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
val navController = LocalNavController.current
Scaffold(
topBar = {
NSTopBar(
Expand All @@ -35,11 +32,6 @@ fun LicenseScreen() {
Text(text = stringResource(R.string.license))
},
scrollBehavior = scrollBehavior,
navigationIcon = {
BackIcon {
navController.popBackStack()
}
}
)
}
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import com.skyd.nightscreen.R
import com.skyd.nightscreen.ui.component.*
import com.skyd.nightscreen.ui.component.dialog.AlphaDialog
import com.skyd.nightscreen.ui.component.dialog.ColorDialog
import com.skyd.nightscreen.ui.local.LocalNavController

const val SETTINGS_SCREEN_ROUTE = "settingsScreen"

@Composable
fun SettingsScreen() {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
val navController = LocalNavController.current
Scaffold(
topBar = {
NSTopBar(
Expand All @@ -36,11 +34,6 @@ fun SettingsScreen() {
Text(text = stringResource(R.string.settings))
},
scrollBehavior = scrollBehavior,
navigationIcon = {
BackIcon {
navController.popBackStack()
}
}
)
}
) { innerPadding ->
Expand Down

0 comments on commit 5ffd236

Please sign in to comment.