Repository navigation
Expand file tree
/
Copy pathJetchatApp.kt
More file actions
115 lines (110 loc) · 4.9 KB
/
Copy pathJetchatApp.kt
File metadata and controls
115 lines (110 loc) · 4.9 KB
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
/*
* Copyright 2026 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.compose.jetchat
import androidx.compose.material3.DrawerValue.Closed
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.runtime.rememberSaveableStateHolderNavEntryDecorator
import androidx.navigation3.ui.NavDisplay
import com.example.compose.jetchat.components.JetchatDrawer
import com.example.compose.jetchat.conversation.ConversationContent
import com.example.compose.jetchat.conversation.ConversationViewModel
import com.example.compose.jetchat.navigation.Conversation
import com.example.compose.jetchat.navigation.Profile
import com.example.compose.jetchat.navigation.openConversation
import com.example.compose.jetchat.navigation.openProfile
import com.example.compose.jetchat.profile.ProfileError
import com.example.compose.jetchat.profile.ProfileScreen
import com.example.compose.jetchat.profile.ProfileViewModel
import kotlinx.coroutines.launch
/**
* Root composable of the app: the navigation drawer around a Navigation 3 [NavDisplay].
*
* The back stack is the single source of truth for navigation. The drawer derives its selection
* from it and sends navigation events back to it, following unidirectional data flow.
*/
@Composable
fun JetchatApp() {
// The back stack survives configuration changes and process death
val backStack = rememberNavBackStack(Conversation("composers"))
val drawerState = rememberDrawerState(initialValue = Closed)
val scope = rememberCoroutineScope()
JetchatDrawer(
// Derived from the back stack, so it can't get out of sync with the displayed screen
selectedKey = backStack.last(),
onNavigate = { key ->
when (key) {
is Conversation -> backStack.openConversation(key.channel)
is Profile -> backStack.openProfile(key.userId)
}
scope.launch {
drawerState.close()
}
},
drawerState = drawerState,
) {
NavDisplay(
backStack = backStack,
onBack = { backStack.removeLastOrNull() },
// Scope the ViewModels to their entry. The ViewModelStoreNavEntryDecorator
// requires the default SaveableStateHolderNavEntryDecorator, which saves the
// state of each entry.
entryDecorators = listOf(
rememberSaveableStateHolderNavEntryDecorator(),
rememberViewModelStoreNavEntryDecorator(),
),
entryProvider = entryProvider {
entry<Conversation> { key ->
// The channel comes from the key, so pass it when the ViewModel of this
// entry is created
val viewModel = viewModel { ConversationViewModel(channel = key.channel) }
ConversationContent(
uiState = viewModel.uiState,
navigateToProfile = { userId -> backStack.openProfile(userId) },
onNavIconPressed = {
scope.launch {
drawerState.open()
}
},
onMessageLikeToggled = viewModel::toggleLike,
)
}
entry<Profile> { key ->
// The user id comes from the key, so set it when the ViewModel of this
// entry is created
val viewModel = viewModel {
ProfileViewModel().apply { setUserId(key.userId) }
}
val userData by viewModel.userData.observeAsState()
if (userData == null) {
ProfileError()
} else {
ProfileScreen(
userData = userData!!,
)
}
}
},
)
}
}