-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathNavigation.kt
146 lines (135 loc) · 4.65 KB
/
Navigation.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.jetnews.ui
import android.os.Bundle
import androidx.annotation.MainThread
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.core.os.bundleOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.example.jetnews.ui.Screen.Article
import com.example.jetnews.ui.Screen.Home
import com.example.jetnews.ui.Screen.Interests
import com.example.jetnews.ui.ScreenName.ARTICLE
import com.example.jetnews.ui.ScreenName.HOME
import com.example.jetnews.ui.ScreenName.INTERESTS
import com.example.jetnews.utils.getMutableStateOf
/**
* Screen names (used for serialization)
*/
enum class ScreenName { HOME, INTERESTS, ARTICLE }
/**
* Class defining the screens we have in the app: home, article details and interests
*/
sealed class Screen(val id: ScreenName) {
object Home : Screen(HOME)
object Interests : Screen(INTERESTS)
data class Article(val postId: String) : Screen(ARTICLE)
}
/**
* Helpers for saving and loading a [Screen] object to a [Bundle].
*
* This allows us to persist navigation across process death, for example caused by a long video
* call.
*/
private const val SIS_SCREEN = "sis_screen"
private const val SIS_NAME = "screen_name"
private const val SIS_POST = "post"
/**
* Convert a screen to a bundle that can be stored in [SavedStateHandle]
*/
private fun Screen.toBundle(): Bundle {
return bundleOf(SIS_NAME to id.name).also {
// add extra keys for various types here
if (this is Article) {
it.putString(SIS_POST, postId)
}
}
}
/**
* Read a bundle stored by [Screen.toBundle] and return desired screen.
*
* @return the parsed [Screen]
* @throws IllegalArgumentException if the bundle could not be parsed
*/
private fun Bundle.toScreen(): Screen {
val screenName = ScreenName.valueOf(getStringOrThrow(SIS_NAME))
return when (screenName) {
HOME -> Home
INTERESTS -> Interests
ARTICLE -> {
val postId = getStringOrThrow(SIS_POST)
Article(postId)
}
}
}
/**
* Throw [IllegalArgumentException] if key is not in bundle.
*
* @see Bundle.getString
*/
private fun Bundle.getStringOrThrow(key: String) =
requireNotNull(getString(key)) { "Missing key '$key' in $this" }
/**
* This is expected to be replaced by the navigation component, but for now handle navigation
* manually.
*
* Instantiate this ViewModel at the scope that is fully-responsible for navigation, which in this
* application is [MainActivity].
*
* This app has simplified navigation; the back stack is always [Home] or [Home, dest] and more
* levels are not allowed. To use a similar pattern with a longer back stack, use a [StateList] to
* hold the back stack state.
*/
class NavigationViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() {
/**
* Hold the current screen in an observable, restored from savedStateHandle after process
* death.
*
* mutableStateOf is an observable similar to LiveData that's designed to be read by compose. It
* supports observability via property delegate syntax as shown here.
*/
var currentScreen: Screen by savedStateHandle.getMutableStateOf<Screen>(
key = SIS_SCREEN,
default = Home,
save = { it.toBundle() },
restore = { it.toScreen() }
)
private set // limit the writes to only inside this class.
/**
* Go back (always to [Home]).
*
* Returns true if this call caused user-visible navigation. Will always return false
* when [currentScreen] is [Home].
*/
@MainThread
fun onBack(): Boolean {
val wasHandled = currentScreen != Home
currentScreen = Home
return wasHandled
}
/**
* Navigate to requested [Screen].
*
* If the requested screen is not [Home], it will always create a back stack with one element:
* ([Home] -> [screen]). More back entries are not supported in this app.
*/
@MainThread
fun navigateTo(screen: Screen) {
currentScreen = screen
}
}