Back button: fixed destinations
This commit is contained in:
@@ -64,15 +64,15 @@ class MainActivity : ComponentActivity() {
|
||||
val cameraPermission = rememberCameraPermissionState()
|
||||
MyScanTheme {
|
||||
val navigation = Navigation(
|
||||
toHomeScreen = { viewModel.navigateTo(Screen.Home) },
|
||||
toCameraScreen = { viewModel.navigateTo(Screen.Camera) },
|
||||
toDocumentScreen = { viewModel.navigateTo(Screen.Document()) },
|
||||
toAboutScreen = { viewModel.navigateTo(Screen.About) },
|
||||
toLibrariesScreen = { viewModel.navigateTo(Screen.Libraries) },
|
||||
toHomeScreen = { viewModel.navigateTo(Screen.Main.Home) },
|
||||
toCameraScreen = { viewModel.navigateTo(Screen.Main.Camera) },
|
||||
toDocumentScreen = { viewModel.navigateTo(Screen.Main.Document()) },
|
||||
toAboutScreen = { viewModel.navigateTo(Screen.Overlay.About) },
|
||||
toLibrariesScreen = { viewModel.navigateTo(Screen.Overlay.Libraries) },
|
||||
back = { viewModel.navigateBack() }
|
||||
)
|
||||
when (val screen = currentScreen) {
|
||||
is Screen.Home -> {
|
||||
is Screen.Main.Home -> {
|
||||
val recentDocs by viewModel.recentDocuments.collectAsStateWithLifecycle()
|
||||
HomeScreen(
|
||||
cameraPermission = cameraPermission,
|
||||
@@ -83,17 +83,17 @@ class MainActivity : ComponentActivity() {
|
||||
onOpenPdf = { file -> openPdf(file.toUri()) }
|
||||
)
|
||||
}
|
||||
is Screen.Camera -> {
|
||||
is Screen.Main.Camera -> {
|
||||
CameraScreen(
|
||||
viewModel,
|
||||
navigation,
|
||||
liveAnalysisState,
|
||||
onImageAnalyzed = { image -> viewModel.liveAnalysis(image) },
|
||||
onFinalizePressed = { viewModel.navigateTo(Screen.Document()) },
|
||||
onFinalizePressed = navigation.toDocumentScreen,
|
||||
cameraPermission = cameraPermission
|
||||
)
|
||||
}
|
||||
is Screen.Document -> {
|
||||
is Screen.Main.Document -> {
|
||||
DocumentScreen (
|
||||
document = document,
|
||||
initialPage = screen.initialPage,
|
||||
@@ -109,14 +109,14 @@ class MainActivity : ComponentActivity() {
|
||||
),
|
||||
onStartNew = {
|
||||
viewModel.startNewDocument()
|
||||
viewModel.navigateTo(Screen.Home) },
|
||||
viewModel.navigateTo(Screen.Main.Home) },
|
||||
onDeleteImage = { id -> viewModel.deletePage(id) }
|
||||
)
|
||||
}
|
||||
is Screen.About -> {
|
||||
is Screen.Overlay.About -> {
|
||||
AboutScreen(onBack = navigation.back, onViewLibraries = navigation.toLibrariesScreen)
|
||||
}
|
||||
is Screen.Libraries -> {
|
||||
is Screen.Overlay.Libraries -> {
|
||||
LibrariesScreen(onBack = navigation.back)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,9 +73,9 @@ class MainViewModel(
|
||||
val liveAnalysisState: StateFlow<LiveAnalysisState> = _liveAnalysisState.asStateFlow()
|
||||
private var lastSuccessfulLiveAnalysisState: LiveAnalysisState? = null
|
||||
|
||||
private val _screenStack = MutableStateFlow<List<Screen>>(listOf(Screen.Home))
|
||||
val currentScreen: StateFlow<Screen> = _screenStack.map { it.last() }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, _screenStack.value.last())
|
||||
private val _navigationState = MutableStateFlow(NavigationState.initial())
|
||||
val currentScreen: StateFlow<Screen> = _navigationState.map { it.current }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, _navigationState.value.current)
|
||||
|
||||
private val _pageIds = MutableStateFlow(imageRepository.imageIds())
|
||||
val documentUiModel: StateFlow<DocumentUiModel> =
|
||||
@@ -159,12 +159,12 @@ class MainViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun navigateTo(screen: Screen) {
|
||||
_screenStack.update { it + screen }
|
||||
fun navigateTo(destination: Screen) {
|
||||
_navigationState.update { it.navigateTo(destination) }
|
||||
}
|
||||
|
||||
fun navigateBack() {
|
||||
_screenStack.update { stack -> if (stack.size > 1) stack.dropLast(1) else stack }
|
||||
_navigationState.update { stack -> stack.navigateBack() }
|
||||
}
|
||||
|
||||
fun onImageCaptured(imageProxy: ImageProxy?) {
|
||||
|
||||
@@ -15,11 +15,15 @@
|
||||
package org.mydomain.myscan
|
||||
|
||||
sealed class Screen {
|
||||
object Home : Screen()
|
||||
object Camera : Screen()
|
||||
data class Document(val initialPage: Int = 0) : Screen()
|
||||
object About : Screen()
|
||||
object Libraries : Screen()
|
||||
sealed class Main : Screen() {
|
||||
object Home : Main()
|
||||
object Camera : Main()
|
||||
data class Document(val initialPage: Int = 0) : Main()
|
||||
}
|
||||
sealed class Overlay : Screen() {
|
||||
object About : Overlay()
|
||||
object Libraries : Overlay()
|
||||
}
|
||||
}
|
||||
|
||||
data class Navigation(
|
||||
@@ -30,3 +34,30 @@ data class Navigation(
|
||||
val toLibrariesScreen: () -> Unit,
|
||||
val back: () -> Unit,
|
||||
)
|
||||
|
||||
@ConsistentCopyVisibility
|
||||
data class NavigationState private constructor(val stack: List<Screen>) {
|
||||
|
||||
companion object {
|
||||
fun initial() = NavigationState(listOf(Screen.Main.Home))
|
||||
}
|
||||
|
||||
val current: Screen get() = stack.last()
|
||||
|
||||
fun navigateTo(destination: Screen): NavigationState {
|
||||
return if (destination is Screen.Overlay) {
|
||||
copy(stack = stack + destination)
|
||||
} else {
|
||||
copy(stack = listOf(destination))
|
||||
}
|
||||
}
|
||||
|
||||
fun navigateBack(): NavigationState {
|
||||
return when (current) {
|
||||
is Screen.Main.Home -> this // Back handled by system
|
||||
is Screen.Main.Camera -> copy(stack = listOf(Screen.Main.Home))
|
||||
is Screen.Main.Document -> copy(stack = listOf(Screen.Main.Camera))
|
||||
is Screen.Overlay -> copy(stack = stack.dropLast(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ fun CameraScreen(
|
||||
pageListState =
|
||||
CommonPageListState(
|
||||
document = document,
|
||||
onPageClick = { index -> viewModel.navigateTo(Screen.Document(index)) },
|
||||
onPageClick = { index -> viewModel.navigateTo(Screen.Main.Document(index)) },
|
||||
listState = listState,
|
||||
onLastItemPosition = { offset -> thumbnailCoords.value = offset },
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user