-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMessagesActivity4.kt
More file actions
239 lines (224 loc) · 10.6 KB
/
MessagesActivity4.kt
File metadata and controls
239 lines (224 loc) · 10.6 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.example.chattutorial
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Keyboard
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.unit.dp
import io.getstream.chat.android.compose.ui.components.composer.MessageInput
import io.getstream.chat.android.compose.ui.components.messageoptions.defaultMessageOptionsState
import io.getstream.chat.android.compose.ui.components.selectedmessage.SelectedMessageMenu
import io.getstream.chat.android.compose.ui.components.selectedmessage.SelectedReactionsMenu
import io.getstream.chat.android.compose.ui.messages.attachments.AttachmentsPicker
import io.getstream.chat.android.compose.ui.messages.composer.MessageComposer
import io.getstream.chat.android.compose.ui.messages.list.MessageList
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.compose.ui.theme.StreamShapes
import io.getstream.chat.android.compose.viewmodel.messages.AttachmentsPickerViewModel
import io.getstream.chat.android.compose.viewmodel.messages.MessageComposerViewModel
import io.getstream.chat.android.compose.viewmodel.messages.MessageListViewModel
import io.getstream.chat.android.compose.viewmodel.messages.MessagesViewModelFactory
import io.getstream.chat.android.ui.common.state.messages.MessageMode
import io.getstream.chat.android.ui.common.state.messages.list.SelectedMessageOptionsState
import io.getstream.chat.android.ui.common.state.messages.list.SelectedMessageReactionsState
class MessagesActivity4 : ComponentActivity() {
// Build the ViewModel factory
private val factory by lazy {
MessagesViewModelFactory(
context = this,
channelId = intent.getStringExtra(KEY_CHANNEL_ID) ?: "",
)
}
// Build the required ViewModels, using the 'factory'
private val listViewModel: MessageListViewModel by viewModels { factory }
private val attachmentsPickerViewModel: AttachmentsPickerViewModel by viewModels { factory }
private val composerViewModel: MessageComposerViewModel by viewModels { factory }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 1 - Load the ID of the selected channel
val channelId = intent.getStringExtra(KEY_CHANNEL_ID)
if (channelId == null) {
finish()
return
}
// 2 - Add the MessagesScreen to your UI
setContent {
ChatTheme(
shapes = StreamShapes
.defaultShapes()
.copy(
avatar = RoundedCornerShape(8.dp),
attachment = RoundedCornerShape(16.dp),
myMessageBubble = RoundedCornerShape(16.dp),
otherMessageBubble = RoundedCornerShape(16.dp),
inputField = RectangleShape
)
) {
MyCustomUi()
}
}
}
@Composable
fun MyCustomUi() {
// 1 - Load the data
val isShowingAttachments = attachmentsPickerViewModel.isShowingAttachments
val selectedMessageState = listViewModel.currentMessagesState.selectedMessageState
val user by listViewModel.user.collectAsState()
Box(modifier = Modifier.fillMaxSize()) { // 2 - Define the root
Scaffold(
modifier = Modifier.fillMaxSize(),
bottomBar = {
MyCustomComposer() // <--
}
) {
MessageList( // 4 - Build the MessageList and connect the actions
modifier = Modifier
.background(ChatTheme.colors.appBackground)
.padding(it)
.fillMaxSize(),
viewModel = listViewModel,
onThreadClick = { message ->
composerViewModel.setMessageMode(MessageMode.MessageThread(message))
listViewModel.openMessageThread(message)
}
)
}
// 5 - Show attachments when necessary
if (isShowingAttachments) {
AttachmentsPicker(
attachmentsPickerViewModel = attachmentsPickerViewModel,
modifier = Modifier
.align(Alignment.BottomCenter)
.height(350.dp),
onAttachmentsSelected = { attachments ->
attachmentsPickerViewModel.changeAttachmentState(false)
composerViewModel.addSelectedAttachments(attachments)
},
onDismiss = {
attachmentsPickerViewModel.changeAttachmentState(false)
attachmentsPickerViewModel.dismissAttachments()
},
onTabClick = { _, _ -> /* Not needed for this example */ }
)
}
// 6 - Show the overlay if we've selected a message
if (selectedMessageState != null) {
val selectedMessage = selectedMessageState.message
if (selectedMessageState is SelectedMessageOptionsState) {
SelectedMessageMenu(
modifier = Modifier
.align(Alignment.Center)
.padding(horizontal = 20.dp)
.wrapContentSize(),
shape = ChatTheme.shapes.attachment,
messageOptions = defaultMessageOptionsState(
selectedMessage,
user,
selectedMessageState.ownCapabilities
),
message = selectedMessage,
onMessageAction = { action ->
composerViewModel.performMessageAction(action)
listViewModel.performMessageAction(action)
},
onShowMoreReactionsSelected = {
listViewModel.selectExtendedReactions(selectedMessage)
},
onDismiss = { listViewModel.removeOverlay() },
ownCapabilities = selectedMessageState.ownCapabilities
)
} else if (selectedMessageState is SelectedMessageReactionsState) {
SelectedReactionsMenu(
modifier = Modifier
.align(Alignment.Center)
.padding(horizontal = 20.dp)
.wrapContentSize(),
shape = ChatTheme.shapes.attachment,
message = selectedMessage,
currentUser = user,
onMessageAction = { action ->
composerViewModel.performMessageAction(action)
listViewModel.performMessageAction(action)
},
onShowMoreReactionsSelected = {
listViewModel.selectExtendedReactions(selectedMessage)
},
onDismiss = { listViewModel.removeOverlay() },
ownCapabilities = selectedMessageState.ownCapabilities
)
}
}
}
}
@Composable
fun MyCustomComposer() {
MessageComposer( // 1 - Use our MessageComposer as the base component
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
viewModel = composerViewModel,
integrations = {}, // 2 - Remove integrations from the composer
input = { inputState ->// 3 - Add a custom message input
MessageInput(
modifier = Modifier
.fillMaxWidth()
.weight(7f)
.padding(start = 8.dp)
.align(Alignment.CenterVertically),
messageComposerState = inputState,
onValueChange = { composerViewModel.setMessageInput(it) },
onAttachmentRemoved = { composerViewModel.removeSelectedAttachment(it) },
label = { // 4 - Override the label to show a custom icon and a text
Row(
Modifier.wrapContentWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Default.Keyboard,
contentDescription = null,
tint = ChatTheme.colors.textLowEmphasis
)
Text(
modifier = Modifier.padding(start = 4.dp),
text = "Type something",
color = ChatTheme.colors.textLowEmphasis
)
}
}
)
}
)
}
// 3 - Create an intent to start this Activity, with a given channelId
companion object {
private const val KEY_CHANNEL_ID = "channelId"
fun getIntent(context: Context, channelId: String): Intent {
return Intent(context, MessagesActivity4::class.java).apply {
putExtra(KEY_CHANNEL_ID, channelId)
}
}
}
}