11package com.example.chattutorial
22
3+ import android.content.ClipboardManager
34import android.content.Context
45import android.content.Intent
56import android.os.Bundle
67import androidx.activity.compose.setContent
78import androidx.appcompat.app.AppCompatActivity
8- import io.getstream.chat.android.compose.ui.messages.MessagesScreen
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
928import 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
1035
1136class MessagesActivity : AppCompatActivity () {
1237
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+
1355 override fun onCreate (savedInstanceState : Bundle ? ) {
1456 super .onCreate(savedInstanceState)
1557 // 1 - Load the ID of the selected channel
@@ -22,16 +64,121 @@ class MessagesActivity : AppCompatActivity() {
2264
2365 // 2 - Add the MessagesScreen to your UI
2466 setContent {
25- ChatTheme {
26- MessagesScreen (
27- channelId = channelId,
28- messageLimit = 30 ,
29- onBackPressed = { finish() }
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() }
30135 )
31136 }
32137 }
33138 }
34139
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+
35182 // 3 - Create an intent to start this Activity, with a given channelId
36183 companion object {
37184 private const val KEY_CHANNEL_ID = " channelId"
@@ -42,4 +189,4 @@ class MessagesActivity : AppCompatActivity() {
42189 }
43190 }
44191 }
45- }
192+ }
0 commit comments