11package com.example.chattutorial
22
3- import android.content.ClipboardManager
43import android.content.Context
54import android.content.Intent
65import android.os.Bundle
76import androidx.activity.compose.setContent
87import androidx.appcompat.app.AppCompatActivity
9- import androidx.compose.foundation.layout.*
10- import androidx.compose.foundation.shape.RoundedCornerShape
11- import androidx.compose.material.Icon
12- import androidx.compose.material.Scaffold
13- import androidx.compose.material.Text
14- import androidx.compose.material.icons.Icons
15- import androidx.compose.runtime.Composable
16- import androidx.compose.ui.Alignment
17- import androidx.compose.ui.Modifier
18- import androidx.compose.ui.graphics.RectangleShape
19- import androidx.compose.ui.unit.dp
20- import io.getstream.chat.android.client.ChatClient
21- import io.getstream.chat.android.compose.state.messages.Thread
22- import io.getstream.chat.android.compose.ui.messages.attachments.AttachmentsPicker
23- import io.getstream.chat.android.compose.ui.messages.composer.MessageComposer
24- import io.getstream.chat.android.compose.ui.messages.composer.components.MessageInput
25- import io.getstream.chat.android.compose.ui.messages.list.MessageList
26- import io.getstream.chat.android.compose.ui.messages.overlay.SelectedMessageOverlay
27- import io.getstream.chat.android.compose.ui.messages.overlay.defaultMessageOptions
8+ import io.getstream.chat.android.compose.ui.messages.MessagesScreen
289import io.getstream.chat.android.compose.ui.theme.ChatTheme
29- import io.getstream.chat.android.compose.ui.theme.StreamShapes
30- import io.getstream.chat.android.compose.viewmodel.messages.AttachmentsPickerViewModel
31- import io.getstream.chat.android.compose.viewmodel.messages.MessageComposerViewModel
32- import io.getstream.chat.android.compose.viewmodel.messages.MessageListViewModel
33- import io.getstream.chat.android.compose.viewmodel.messages.MessagesViewModelFactory
34- import io.getstream.chat.android.offline.ChatDomain
3510
3611class MessagesActivity : AppCompatActivity () {
3712
38- // Build the ViewModel factory
39- private val factory by lazy {
40- MessagesViewModelFactory (
41- this ,
42- getSystemService(CLIPBOARD_SERVICE ) as ClipboardManager ,
43- ChatClient .instance(),
44- ChatDomain .instance(),
45- intent.getStringExtra(KEY_CHANNEL_ID ) ? : " " ,
46- 30
47- )
48- }
49-
50- // Build the required ViewModels, using the 'factory'
51- private val listViewModel: MessageListViewModel by viewModels { factory }
52- private val attachmentsPickerViewModel: AttachmentsPickerViewModel by viewModels { factory }
53- private val composerViewModel: MessageComposerViewModel by viewModels { factory }
54-
5513 override fun onCreate (savedInstanceState : Bundle ? ) {
5614 super .onCreate(savedInstanceState)
5715 // 1 - Load the ID of the selected channel
@@ -64,121 +22,16 @@ class MessagesActivity : AppCompatActivity() {
6422
6523 // 2 - Add the MessagesScreen to your UI
6624 setContent {
67- ChatTheme (
68- shapes = StreamShapes (
69- avatar = RoundedCornerShape (8 .dp),
70- attachment = RoundedCornerShape (16 .dp),
71- myMessageBubble = RoundedCornerShape (16 .dp),
72- otherMessageBubble = RoundedCornerShape (16 .dp),
73- inputField = RectangleShape
74- )
75- ) {
76- MyCustomUi ()
77- }
78- }
79- }
80-
81- @Composable
82- fun MyCustomUi () {
83- // load the data
84- val isShowingAttachments = attachmentsPickerViewModel.isShowingAttachments
85- val selectedMessage = listViewModel.currentMessagesState.selectedMessage
86- val user by listViewModel.user.collectAsState()
87-
88- Box (modifier = Modifier .fillMaxSize()) {
89- Scaffold (
90- modifier = Modifier .fillMaxSize(),
91- bottomBar = {
92- MyCustomComposer ()
93- }
94- ) {
95- MessageList ( // build the MessageList and connect the actions
96- modifier = Modifier
97- .padding(it)
98- .fillMaxSize(),
99- viewModel = listViewModel,
100- onThreadClick = { message ->
101- composerViewModel.setMessageMode(Thread (message))
102- listViewModel.openMessageThread(message)
103- }
104- )
105- }
106-
107- // show attachments picker when necessary
108- if (isShowingAttachments) {
109- AttachmentsPicker (
110- attachmentsPickerViewModel = attachmentsPickerViewModel,
111- modifier = Modifier
112- .align(Alignment .BottomCenter )
113- .height(350 .dp),
114- onAttachmentsSelected = { attachments ->
115- attachmentsPickerViewModel.changeAttachmentState(false )
116- composerViewModel.addSelectedAttachments(attachments)
117- },
118- onDismiss = {
119- attachmentsPickerViewModel.changeAttachmentState(false )
120- attachmentsPickerViewModel.dismissAttachments()
121- }
122- )
123- }
124-
125- // Show the overlay if we've selected a message
126- if (selectedMessage != null ) {
127- SelectedMessageOverlay (
128- messageOptions = defaultMessageOptions(selectedMessage, user, listViewModel.isInThread),
129- message = selectedMessage,
130- onMessageAction = { action ->
131- composerViewModel.performMessageAction(action)
132- listViewModel.performMessageAction(action)
133- },
134- onDismiss = { listViewModel.removeOverlay() }
25+ ChatTheme {
26+ MessagesScreen (
27+ channelId = channelId,
28+ messageLimit = 30 ,
29+ onBackPressed = { finish() }
13530 )
13631 }
13732 }
13833 }
13934
140- @Composable
141- fun MyCustomComposer () {
142- MessageComposer (
143- modifier = Modifier
144- .fillMaxWidth()
145- .wrapContentHeight(),
146- viewModel = composerViewModel,
147- integrations = {},
148- input = {
149- MessageInput (
150- modifier = Modifier
151- .fillMaxWidth()
152- .weight(7f )
153- .padding(start = 8 .dp),
154- value = composerViewModel.input,
155- attachments = composerViewModel.selectedAttachments,
156- activeAction = composerViewModel.activeAction,
157- onValueChange = { composerViewModel.setMessageInput(it) },
158- onAttachmentRemoved = { composerViewModel.removeSelectedAttachment(it) },
159- label = {
160- Row (
161- Modifier .wrapContentWidth(),
162- verticalAlignment = Alignment .CenterVertically
163- ) {
164- Icon (
165- imageVector = Icons .Default .Keyboard ,
166- contentDescription = null ,
167- tint = ChatTheme .colors.textLowEmphasis
168- )
169-
170- Text (
171- modifier = Modifier .padding(start = 4 .dp),
172- text = " Type something" ,
173- color = ChatTheme .colors.textLowEmphasis
174- )
175- }
176- }
177- )
178- }
179- )
180- }
181-
18235 // 3 - Create an intent to start this Activity, with a given channelId
18336 companion object {
18437 private const val KEY_CHANNEL_ID = " channelId"
@@ -189,4 +42,4 @@ class MessagesActivity : AppCompatActivity() {
18942 }
19043 }
19144 }
192- }
45+ }
0 commit comments