pref(BackHandler): 导航到其他页面时当前页不处理返回
This commit is contained in:
parent
89219be456
commit
5d74a478b5
|
|
@ -31,6 +31,8 @@ import androidx.compose.runtime.CompositionLocalProvider
|
|||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.compositionLocalOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.staticCompositionLocalOf
|
||||
import androidx.compose.ui.Alignment
|
||||
|
|
@ -41,6 +43,7 @@ import androidx.core.content.ContextCompat
|
|||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.lifecycle.flowWithLifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.plusAssign
|
||||
import androidx.window.layout.FoldingFeature
|
||||
import androidx.window.layout.WindowInfoTracker
|
||||
|
|
@ -75,6 +78,8 @@ import com.ramcosta.composedestinations.DestinationsNavHost
|
|||
import com.ramcosta.composedestinations.animations.defaults.RootNavGraphDefaultAnimations
|
||||
import com.ramcosta.composedestinations.animations.rememberAnimatedNavHostEngine
|
||||
import com.ramcosta.composedestinations.navigation.dependency
|
||||
import com.ramcosta.composedestinations.spec.DestinationSpec
|
||||
import com.ramcosta.composedestinations.utils.currentDestinationAsState
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
|
|
@ -93,6 +98,9 @@ val LocalNotificationCountFlow =
|
|||
staticCompositionLocalOf<Flow<Int>> { throw IllegalStateException("not allowed here!") }
|
||||
val LocalDevicePosture =
|
||||
staticCompositionLocalOf<State<DevicePosture>> { throw IllegalStateException("not allowed here!") }
|
||||
val LocalNavController =
|
||||
staticCompositionLocalOf<NavController> { throw IllegalStateException("not allowed here!") }
|
||||
val LocalDestination = compositionLocalOf<DestinationSpec<*>?> { null }
|
||||
|
||||
@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterialNavigationApi::class)
|
||||
@Composable
|
||||
|
|
@ -294,18 +302,26 @@ class MainActivityV2 : BaseComposeActivity() {
|
|||
skipHalfExpanded = true
|
||||
)
|
||||
navController.navigatorProvider += bottomSheetNavigator
|
||||
ModalBottomSheetLayout(
|
||||
bottomSheetNavigator = bottomSheetNavigator,
|
||||
sheetShape = RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp)
|
||||
|
||||
val currentDestination by navController.currentDestinationAsState()
|
||||
|
||||
CompositionLocalProvider(
|
||||
LocalNavController provides navController,
|
||||
LocalDestination provides currentDestination
|
||||
) {
|
||||
DestinationsNavHost(
|
||||
navController = navController,
|
||||
navGraph = NavGraphs.root,
|
||||
engine = engine,
|
||||
dependenciesContainerBuilder = {
|
||||
dependency(MainPageDestination) { this@MainActivityV2 }
|
||||
}
|
||||
)
|
||||
ModalBottomSheetLayout(
|
||||
bottomSheetNavigator = bottomSheetNavigator,
|
||||
sheetShape = RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp)
|
||||
) {
|
||||
DestinationsNavHost(
|
||||
navController = navController,
|
||||
navGraph = NavGraphs.root,
|
||||
engine = engine,
|
||||
dependenciesContainerBuilder = {
|
||||
dependency(MainPageDestination) { this@MainActivityV2 }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package com.huanchengfly.tieba.post.ui.page.thread
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.animation.animateColorAsState
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.background
|
||||
|
|
@ -114,6 +113,7 @@ import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
|
|||
import com.huanchengfly.tieba.post.ui.page.ProvideNavigator
|
||||
import com.huanchengfly.tieba.post.ui.page.destinations.ForumPageDestination
|
||||
import com.huanchengfly.tieba.post.ui.page.destinations.SubPostsSheetPageDestination
|
||||
import com.huanchengfly.tieba.post.ui.page.destinations.ThreadPageDestination
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.Avatar
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.BackNavigationIcon
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.Button
|
||||
|
|
@ -124,6 +124,7 @@ import com.huanchengfly.tieba.post.ui.widgets.compose.HorizontalDivider
|
|||
import com.huanchengfly.tieba.post.ui.widgets.compose.LazyLoad
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.ListMenuItem
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.LoadMoreLayout
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.MyBackHandler
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.MyScaffold
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.PromptDialog
|
||||
import com.huanchengfly.tieba.post.ui.widgets.compose.Sizes
|
||||
|
|
@ -545,7 +546,10 @@ fun ThreadPage(
|
|||
}
|
||||
}
|
||||
|
||||
BackHandler(enabled = bottomSheetState.isVisible) {
|
||||
MyBackHandler(
|
||||
enabled = bottomSheetState.isVisible,
|
||||
currentScreen = ThreadPageDestination
|
||||
) {
|
||||
closeBottomSheet()
|
||||
}
|
||||
|
||||
|
|
@ -594,7 +598,10 @@ fun ThreadPage(
|
|||
) {
|
||||
Text(text = stringResource(R.string.message_update_collect_mark, readFloorBeforeBack))
|
||||
}
|
||||
BackHandler(enabled = isCollected && !bottomSheetState.isVisible) {
|
||||
MyBackHandler(
|
||||
enabled = isCollected && !bottomSheetState.isVisible,
|
||||
currentScreen = ThreadPageDestination
|
||||
) {
|
||||
readFloorBeforeBack = getLastVisibilityPost()?.get { floor } ?: 0
|
||||
if (readFloorBeforeBack != 0) {
|
||||
updateCollectMarkDialogState.show()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package com.huanchengfly.tieba.post.ui.widgets.compose
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.runtime.Composable
|
||||
import com.huanchengfly.tieba.post.LocalDestination
|
||||
import com.ramcosta.composedestinations.spec.DestinationSpec
|
||||
|
||||
@Composable
|
||||
fun MyBackHandler(
|
||||
enabled: Boolean,
|
||||
currentScreen: DestinationSpec<*>? = null,
|
||||
onBack: () -> Unit,
|
||||
) {
|
||||
val currentDestination = LocalDestination.current
|
||||
|
||||
val shouldEnable =
|
||||
enabled && (currentScreen == null || currentDestination?.baseRoute == currentScreen.baseRoute)
|
||||
|
||||
BackHandler(enabled = shouldEnable, onBack = onBack)
|
||||
}
|
||||
Loading…
Reference in New Issue