-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonnelMessageFragment.txt
412 lines (347 loc) · 18.7 KB
/
PersonnelMessageFragment.txt
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package com.uqam.mentallys.view.ui.message
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import com.azure.android.communication.chat.ChatClientBuilder
import com.azure.android.communication.chat.ChatThreadAsyncClient
import com.azure.android.communication.chat.ChatThreadClientBuilder
import com.azure.android.communication.chat.models.*
import com.azure.android.communication.common.CommunicationTokenCredential
import com.azure.android.communication.common.CommunicationUserIdentifier
import com.azure.android.core.http.policy.UserAgentPolicy
import com.jakewharton.threetenabp.AndroidThreeTen
import com.jakewharton.threetenabp.AndroidThreeTen.*
import com.squareup.picasso.Picasso
import com.uqam.mentallys.R
import com.uqam.mentallys.databinding.FragmentPersonnelMessageBinding
import com.uqam.mentallys.model.resource.Activity
import dagger.hilt.android.AndroidEntryPoint
import java.util.*
/**
* A simple [Fragment] subclass.
* Use the [PersonnelChatFragment.newInstance] factory method to
* create an instance of this fragment.
*/
@AndroidEntryPoint
class PersonnelMessageFragment : Fragment(R.layout.fragment_personnel_message) {
/*private lateinit var chatThreadAsyncClient: ChatThreadAsyncClient
private lateinit var chatUI: ConstraintLayout
private lateinit var setupUI: ConstraintLayout
private lateinit var chatAsyncClient: ChatAsyncClient
private lateinit var id: String
private lateinit var token: String*/
//private lateinit var threadId: String
private val _fetchResourcesResponse: MutableLiveData<String> = MutableLiveData()
val fetchResourcesResponse: LiveData<String>
get() = _fetchResourcesResponse
private lateinit var name: String
private lateinit var url: String
private val applicationID = "ACS Chat Join"
private val sdkName = "azure-communication-com.azure.android.communication.chat"
private val sdkVersion = "1.0.0"
/*private lateinit var messagesList: MessagesList
private lateinit var messageInput: MessageInput
private lateinit var adapter: MessagesListAdapter<Message>*/
private var endpoint: String? = "https://mentallys-communicationservices-dev.communication.azure.com/"
private val firstUserId = "8:acs:ce1e4b81-dc3a-42c0-9a71-7ff148d57eb3_00000014-4b4b-df74-92fd-8b3a0d00f49a"
private val secondUserId = "8:acs:ce1e4b81-dc3a-42c0-9a71-7ff148d57eb3_00000014-4b4d-38b3-edbe-a43a0d004037"
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lIjoiY2hhdCIsInJlc291cmNlSWQiOiJjZTFlNGI4MS1kYzNhLTQyYzAtOWE3MS03ZmYxNDhkNTdlYjMiLCJpYXQiOjE2NjUwMjgyOTd9.qe6BHFTJtIOXc1TMtZZ8TxIRpn_h74Ve7Geoq7DCGtfiDWVEOcEDpTSpmVXWcsiXx8pzR9PoP06sMZUtG6nGuSZU_Qa71HQTRwmFPVg4PjkfHG4B9ztSOz7BnB5npE6MsQ90RMrkMZuyiQy5AEDK_pEJYHdhLB5ubUOPwk2No7x-xNUyyLFlYbVtIJ2jO6qi8Q78F2ElIyNlmDNyPOxZIrdM8RsQWH3qg9s3VqooyFngPqynME3p9ZuVhN1AMDXM0L3sdP4Hne3sSCYjzZHDMyS9WWUEBQQtsO0dpzibq5rN_3EhJL5W7vcJvRFuwGGhmat-Bpk__wh9bETQwVBtAg"
private var threadId1 = "<thread_id>"
private var threadId2 = "<thread_id>"
private var chatMessageId1 = "<chat_message_id>"
private var chatMessageId2 = "<chat_message_id>"
// private val sdkVersion = "<chat_sdk_version>"
private val APPLICATION_ID = "com.uqam.mentallys"
private val SDK_NAME = "azure-communication-com.azure.android.communication.chat"
private val TAG = "Chat Quickstart App"
private var chatThreadAsyncClient1: ChatThreadAsyncClient? = null
private var chatThreadAsyncClient2: ChatThreadAsyncClient? = null
private val viewModel: SharedChatUserInfo by viewModels({requireParentFragment()})
private lateinit var binding: FragmentPersonnelMessageBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentPersonnelMessageBinding.inflate(layoutInflater)
val selectedUserInfo = viewModel.getUserInfo()
binding.personName.text = selectedUserInfo?.fullUserName
binding.firstUser1.setOnClickListener{
sendMessageToUser2()
}
binding.secondUser2.setOnClickListener{
sendMessageToUser1()
}
Picasso.get()
.load(selectedUserInfo?.userImage)
.resize(300, 300)
.centerCrop()
.into(binding.imageChatPerson)
observeEventTypesViewModel()
Start1()
Start2()
return binding.root
}
private fun log(msg: String) {
Log.i(TAG, msg)
Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
}
private fun observeEventTypesViewModel() {
// viewModel.resource.observe(viewLifecycleOwner){ eventTypes ->
// resourceAdapter.submitList(eventTypes)*/
fetchResourcesResponse.observe(viewLifecycleOwner){
var it = it
binding.firstUser.text = it
}
}
public fun Start1() {
try {
// <CREATE A CHAT CLIENT>
val chatAsyncClient1 = ChatClientBuilder()
.endpoint(endpoint)
.credential(CommunicationTokenCredential(firstUserAccessToken))
.addPolicy(UserAgentPolicy(APPLICATION_ID, SDK_NAME, sdkVersion))
.buildAsyncClient()
// <CREATE A CHAT THREAD>
// A list of ChatParticipant to start the thread with.
val participants: MutableList<ChatParticipant> = ArrayList()
// The display name for the thread participant.
val displayName = "initial participant"
participants.add(
ChatParticipant()
.setCommunicationIdentifier(CommunicationUserIdentifier(firstUserId))
.setDisplayName(displayName)
)
// The topic for the thread.
val topic = "General"
// Optional, set a repeat request ID.
val repeatabilityRequestID = ""
// Options to pass to the create method.
val createChatThreadOptions = CreateChatThreadOptions()
.setTopic(topic)
.setParticipants(participants)
.setIdempotencyToken(repeatabilityRequestID)
val createChatThreadResult =
chatAsyncClient1.createChatThread(createChatThreadOptions).get()
val chatThreadProperties = createChatThreadResult.chatThreadProperties
threadId1 = chatThreadProperties.id
// <CREATE A CHAT THREAD CLIENT>
chatThreadAsyncClient1 = ChatThreadClientBuilder()
.endpoint(endpoint)
.credential(CommunicationTokenCredential(firstUserAccessToken))
.addPolicy(UserAgentPolicy(APPLICATION_ID, SDK_NAME, sdkVersion))
.chatThreadId(threadId1)
.buildAsyncClient()
// <RECEIVE CHAT MESSAGES>
// Start real time notification
chatAsyncClient1.startRealtimeNotifications(
firstUserAccessToken,
context
)
// Register a listener for chatMessageReceived event
// Register a listener for chatMessageReceived event
chatAsyncClient1.addEventHandler(ChatEventType.CHAT_MESSAGE_RECEIVED) { payload: ChatEvent? ->
val chatMessageReceivedEvent = payload as ChatMessageReceivedEvent?
activity?.runOnUiThread{
binding.firstUser.text = chatMessageReceivedEvent?.content.toString()
}
}
// Register a lchatMessageReceivedEvent?.content.toString()istener for chatMessageReceived event
/* chatAsyncClient1.addEventHandler(
ChatEventType.CHAT_MESSAGE_RECEIVED
) { payload: ChatEvent? ->
val chatMessageReceivedEvent =
payload as ChatMessageReceivedEvent?
if (chatMessageReceivedEvent != null) {
val authorID =
(chatMessageReceivedEvent.sender as CommunicationUserIdentifier).id.toString()
_fetchResourcesResponse.value = chatMessageReceivedEvent.content.toString()
}
}
}*/
// <ADD A USER>
// The display name for the thread participant.
val secondUserDisplayName = "a new participant"
val participant = ChatParticipant()
.setCommunicationIdentifier(CommunicationUserIdentifier(secondUserId))
.setDisplayName(secondUserDisplayName)
chatThreadAsyncClient1?.addParticipant(participant)
// <LIST USERS>
// The maximum number of participants to be returned per page, optional.
/* var maxPageSize = 10
// Skips participants up to a specified position in response.
var skip = 0
// Options to pass to the list method.
val listParticipantsOptions = ListParticipantsOptions()
.setMaxPageSize(maxPageSize)
.setSkip(skip)
val participantsPagedAsyncStream =
chatThreadAsyncClient.listParticipants(listParticipantsOptions, RequestContext.NONE)
participantsPagedAsyncStream.forEach { chatParticipant: ChatParticipant? -> }
// <REMOVE A USER>
// Using the unique ID of the participant.
chatThreadAsyncClient.removeParticipant(CommunicationUserIdentifier(secondUserId)).get()
// <<SEND A TYPING NOTIFICATION>>
chatThreadAsyncClient.sendTypingNotification().get()
// <<SEND A READ RECEIPT>>
chatThreadAsyncClient.sendReadReceipt(chatMessageId).get()
// <<LIST READ RECEIPTS>>
// The maximum number of participants to be returned per page, optional.
maxPageSize = 10
// Skips participants up to a specified position in response.
skip = 0
// Options to pass to the list method.
val listReadReceiptOptions = ListReadReceiptOptions()
.setMaxPageSize(maxPageSize)
.setSkip(skip)
val readReceiptsPagedAsyncStream =
chatThreadAsyncClient.listReadReceipts(listReadReceiptOptions, RequestContext.NONE)
readReceiptsPagedAsyncStream.forEach { readReceipt: ChatMessageReadReceipt? -> }*/
} catch (e: Exception) {
println("Quickstart failed: " + e.message)
}
}
public fun sendMessageToUser2(){
// <SEND A MESSAGE>
// The chat message content, required.
val content = binding.firstUserMessage.text.toString()
// The display name of the sender, if null (i.e. not specified), an empty name will be set.
val senderDisplayName = "An important person"
val chatMessageOptions = SendChatMessageOptions()
.setType(ChatMessageType.TEXT)
.setContent(content)
.setSenderDisplayName(senderDisplayName)
// A string is the response returned from sending a message, it is an id, which is the unique ID of the message.
chatMessageId1 = chatThreadAsyncClient1?.sendMessage(chatMessageOptions)?.get()?.id.toString()
}
public fun sendMessageToUser1(){
// <SEND A MESSAGE>
// The chat message content, required.
val content = binding.secondUserMessage.text.toString()
// The display name of the sender, if null (i.e. not specified), an empty name will be set.
val senderDisplayName = "An important person"
val chatMessageOptions = SendChatMessageOptions()
.setType(ChatMessageType.TEXT)
.setContent(content)
.setSenderDisplayName(senderDisplayName)
// A string is the response returned from sending a message, it is an id, which is the unique ID of the message.
chatMessageId2 =
chatThreadAsyncClient2?.sendMessage(chatMessageOptions)?.get()?.id.toString()
}
public fun Start2() {
try {
// <CREATE A CHAT CLIENT>
val chatAsyncClient2 = ChatClientBuilder()
.endpoint(endpoint)
.credential(CommunicationTokenCredential(secondUserAccessToken))
.addPolicy(UserAgentPolicy(APPLICATION_ID, SDK_NAME, sdkVersion))
.buildAsyncClient()
// <CREATE A CHAT THREAD>
// A list of ChatParticipant to start the thread with.
val participants: MutableList<ChatParticipant> = ArrayList()
// The display name for the thread participant.
val displayName = "initial participant"
participants.add(
ChatParticipant()
.setCommunicationIdentifier(CommunicationUserIdentifier(secondUserId))
.setDisplayName(displayName)
)
// The topic for the thread.
val topic = "General"
// Optional, set a repeat request ID.
val repeatabilityRequestID = ""
// Options to pass to the create method.
val createChatThreadOptions = CreateChatThreadOptions()
.setTopic(topic)
.setParticipants(participants)
.setIdempotencyToken(repeatabilityRequestID)
val createChatThreadResult =
chatAsyncClient2.createChatThread(createChatThreadOptions).get()
val chatThreadProperties = createChatThreadResult.chatThreadProperties
threadId2 = chatThreadProperties.id
// <CREATE A CHAT THREAD CLIENT>
chatThreadAsyncClient2 = ChatThreadClientBuilder()
.endpoint(endpoint)
.credential(CommunicationTokenCredential(secondUserAccessToken))
.addPolicy(UserAgentPolicy(APPLICATION_ID, SDK_NAME, sdkVersion))
.chatThreadId(threadId2)
.buildAsyncClient()
// <RECEIVE CHAT MESSAGES>
// Start real time notification
chatAsyncClient2.startRealtimeNotifications(
secondUserAccessToken,
context
)
chatAsyncClient2.addEventHandler(ChatEventType.CHAT_MESSAGE_RECEIVED) { payload: ChatEvent? ->
val chatMessageReceivedEvent = payload as ChatMessageReceivedEvent?
activity?.runOnUiThread{
binding.firstUser.text = chatMessageReceivedEvent?.content.toString()
}
}
// Register a listener for chatMessageReceived event
/* chatAsyncClient2.addEventHandler(
ChatEventType.CHAT_MESSAGE_RECEIVED
) { payload: ChatEvent? ->
val chatMessageReceivedEvent =
payload as ChatMessageReceivedEvent?
if (chatMessageReceivedEvent != null) {
val authorID =
(chatMessageReceivedEvent.sender as CommunicationUserIdentifier).id.toString()
// binding.firstUser.text = chatMessageReceivedEvent.content.toString()
}
}*/
// <ADD A USER>
// The display name for the thread participant.
val secondUserDisplayName = "a new participant"
val participant = ChatParticipant()
.setCommunicationIdentifier(CommunicationUserIdentifier(firstUserId))
.setDisplayName(secondUserDisplayName)
chatThreadAsyncClient2?.addParticipant(participant)
// <LIST USERS>
// The maximum number of participants to be returned per page, optional.
/* var maxPageSize = 10
// Skips participants up to a specified position in response.
var skip = 0
// Options to pass to the list method.
val listParticipantsOptions = ListParticipantsOptions()
.setMaxPageSize(maxPageSize)
.setSkip(skip)
val participantsPagedAsyncStream =
chatThreadAsyncClient.listParticipants(listParticipantsOptions, RequestContext.NONE)
participantsPagedAsyncStream.forEach { chatParticipant: ChatParticipant? -> }
// <REMOVE A USER>
// Using the unique ID of the participant.
chatThreadAsyncClient.removeParticipant(CommunicationUserIdentifier(secondUserId)).get()
// <<SEND A TYPING NOTIFICATION>>
chatThreadAsyncClient.sendTypingNotification().get()
// <<SEND A READ RECEIPT>>
chatThreadAsyncClient.sendReadReceipt(chatMessageId).get()
// <<LIST READ RECEIPTS>>
// The maximum number of participants to be returned per page, optional.
maxPageSize = 10
// Skips participants up to a specified position in response.
skip = 0
// Options to pass to the list method.
val listReadReceiptOptions = ListReadReceiptOptions()
.setMaxPageSize(maxPageSize)
.setSkip(skip)
val readReceiptsPagedAsyncStream =
chatThreadAsyncClient.listReadReceipts(listReadReceiptOptions, RequestContext.NONE)
readReceiptsPagedAsyncStream.forEach { readReceipt: ChatMessageReadReceipt? -> }*/
} catch (e: Exception) {
println("Quickstart failed: " + e.message)
}
}
}