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.LaunchedEffect
|
||||||
import androidx.compose.runtime.State
|
import androidx.compose.runtime.State
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.compositionLocalOf
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.staticCompositionLocalOf
|
import androidx.compose.runtime.staticCompositionLocalOf
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
|
|
@ -41,6 +43,7 @@ import androidx.core.content.ContextCompat
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import androidx.lifecycle.flowWithLifecycle
|
import androidx.lifecycle.flowWithLifecycle
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.plusAssign
|
import androidx.navigation.plusAssign
|
||||||
import androidx.window.layout.FoldingFeature
|
import androidx.window.layout.FoldingFeature
|
||||||
import androidx.window.layout.WindowInfoTracker
|
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.defaults.RootNavGraphDefaultAnimations
|
||||||
import com.ramcosta.composedestinations.animations.rememberAnimatedNavHostEngine
|
import com.ramcosta.composedestinations.animations.rememberAnimatedNavHostEngine
|
||||||
import com.ramcosta.composedestinations.navigation.dependency
|
import com.ramcosta.composedestinations.navigation.dependency
|
||||||
|
import com.ramcosta.composedestinations.spec.DestinationSpec
|
||||||
|
import com.ramcosta.composedestinations.utils.currentDestinationAsState
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.channels.BufferOverflow
|
import kotlinx.coroutines.channels.BufferOverflow
|
||||||
|
|
@ -93,6 +98,9 @@ val LocalNotificationCountFlow =
|
||||||
staticCompositionLocalOf<Flow<Int>> { throw IllegalStateException("not allowed here!") }
|
staticCompositionLocalOf<Flow<Int>> { throw IllegalStateException("not allowed here!") }
|
||||||
val LocalDevicePosture =
|
val LocalDevicePosture =
|
||||||
staticCompositionLocalOf<State<DevicePosture>> { throw IllegalStateException("not allowed here!") }
|
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)
|
@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterialNavigationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
|
|
@ -294,18 +302,26 @@ class MainActivityV2 : BaseComposeActivity() {
|
||||||
skipHalfExpanded = true
|
skipHalfExpanded = true
|
||||||
)
|
)
|
||||||
navController.navigatorProvider += bottomSheetNavigator
|
navController.navigatorProvider += bottomSheetNavigator
|
||||||
ModalBottomSheetLayout(
|
|
||||||
bottomSheetNavigator = bottomSheetNavigator,
|
val currentDestination by navController.currentDestinationAsState()
|
||||||
sheetShape = RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp)
|
|
||||||
|
CompositionLocalProvider(
|
||||||
|
LocalNavController provides navController,
|
||||||
|
LocalDestination provides currentDestination
|
||||||
) {
|
) {
|
||||||
DestinationsNavHost(
|
ModalBottomSheetLayout(
|
||||||
navController = navController,
|
bottomSheetNavigator = bottomSheetNavigator,
|
||||||
navGraph = NavGraphs.root,
|
sheetShape = RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp)
|
||||||
engine = engine,
|
) {
|
||||||
dependenciesContainerBuilder = {
|
DestinationsNavHost(
|
||||||
dependency(MainPageDestination) { this@MainActivityV2 }
|
navController = navController,
|
||||||
}
|
navGraph = NavGraphs.root,
|
||||||
)
|
engine = engine,
|
||||||
|
dependenciesContainerBuilder = {
|
||||||
|
dependency(MainPageDestination) { this@MainActivityV2 }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package com.huanchengfly.tieba.post.ui.page.thread
|
package com.huanchengfly.tieba.post.ui.page.thread
|
||||||
|
|
||||||
import androidx.activity.compose.BackHandler
|
|
||||||
import androidx.compose.animation.animateColorAsState
|
import androidx.compose.animation.animateColorAsState
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.background
|
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.ProvideNavigator
|
||||||
import com.huanchengfly.tieba.post.ui.page.destinations.ForumPageDestination
|
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.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.Avatar
|
||||||
import com.huanchengfly.tieba.post.ui.widgets.compose.BackNavigationIcon
|
import com.huanchengfly.tieba.post.ui.widgets.compose.BackNavigationIcon
|
||||||
import com.huanchengfly.tieba.post.ui.widgets.compose.Button
|
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.LazyLoad
|
||||||
import com.huanchengfly.tieba.post.ui.widgets.compose.ListMenuItem
|
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.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.MyScaffold
|
||||||
import com.huanchengfly.tieba.post.ui.widgets.compose.PromptDialog
|
import com.huanchengfly.tieba.post.ui.widgets.compose.PromptDialog
|
||||||
import com.huanchengfly.tieba.post.ui.widgets.compose.Sizes
|
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()
|
closeBottomSheet()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -594,7 +598,10 @@ fun ThreadPage(
|
||||||
) {
|
) {
|
||||||
Text(text = stringResource(R.string.message_update_collect_mark, readFloorBeforeBack))
|
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
|
readFloorBeforeBack = getLastVisibilityPost()?.get { floor } ?: 0
|
||||||
if (readFloorBeforeBack != 0) {
|
if (readFloorBeforeBack != 0) {
|
||||||
updateCollectMarkDialogState.show()
|
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