-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIKitManager.kt
154 lines (140 loc) · 7.22 KB
/
UIKitManager.kt
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
package com.hyphenate.chatdemo.uikit
import android.content.Context
import android.content.Intent
import com.hyphenate.chatdemo.DemoHelper
import com.hyphenate.chatdemo.common.extensions.internal.toProfile
import com.hyphenate.chatdemo.ui.chat.ChatActivity
import com.hyphenate.chatdemo.ui.contact.ChatContactCheckActivity
import com.hyphenate.chatdemo.ui.contact.ChatContactDetailActivity
import com.hyphenate.chatdemo.ui.group.ChatGroupDetailActivity
import com.hyphenate.chatdemo.ui.group.ChatCreateGroupActivity
import com.hyphenate.chatdemo.repository.ProfileInfoRepository
import com.hyphenate.chatdemo.ui.contact.ChatNewRequestActivity
import com.hyphenate.easeui.ChatUIKitClient
import com.hyphenate.easeui.common.ChatClient
import com.hyphenate.easeui.common.ChatException
import com.hyphenate.easeui.common.ChatLog
import com.hyphenate.easeui.common.ChatMessage
import com.hyphenate.easeui.common.ChatUserInfoType
import com.hyphenate.easeui.common.extensions.toProfile
import com.hyphenate.easeui.common.impl.OnValueSuccess
import com.hyphenate.easeui.feature.chat.activities.UIKitChatActivity
import com.hyphenate.easeui.feature.contact.ChatUIKitContactCheckActivity
import com.hyphenate.easeui.feature.contact.ChatUIKitContactDetailsActivity
import com.hyphenate.easeui.feature.group.ChatUIKitCreateGroupActivity
import com.hyphenate.easeui.feature.group.ChatUIKitGroupDetailActivity
import com.hyphenate.easeui.feature.invitation.ChatUIKitNewRequestsActivity
import com.hyphenate.easeui.model.ChatUIKitGroupProfile
import com.hyphenate.easeui.model.ChatUIKitProfile
import com.hyphenate.easeui.provider.ChatUIKitCustomActivityRoute
import com.hyphenate.easeui.provider.ChatUIKitGroupProfileProvider
import com.hyphenate.easeui.provider.ChatUIKitSettingsProvider
import com.hyphenate.easeui.provider.ChatUIKitUserProfileProvider
import com.hyphenate.easeui.widget.ChatUIKitImageView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
object UIKitManager {
fun addUIKitSettings(context: Context) {
addProviders(context)
setUIKitConfigs(context)
}
fun addProviders(context: Context) {
ChatUIKitClient.setUserProfileProvider(object : ChatUIKitUserProfileProvider {
override fun getUser(userId: String?): ChatUIKitProfile? {
return DemoHelper.getInstance().getDataModel().getAllContacts()[userId]?.toProfile()
}
override fun fetchUsers(
userIds: List<String>,
onValueSuccess: OnValueSuccess<List<ChatUIKitProfile>>
) {
// fetch users from server and call call onValueSuccess.onSuccess(users) after successfully getting users
CoroutineScope(Dispatchers.IO).launch {
if (userIds.isEmpty()) {
onValueSuccess(mutableListOf())
return@launch
}
try {
val users = ProfileInfoRepository().getUserInfoAttribute(userIds, mutableListOf(ChatUserInfoType.NICKNAME, ChatUserInfoType.AVATAR_URL))
val callbackList = users.values.map { it.toProfile() }.map {
DemoHelper.getInstance().getDataModel().getUser(it.id)?.remark?.let { remark->
it.remark = remark
}
it
}
if (callbackList.isNotEmpty()) {
DemoHelper.getInstance().getDataModel().insertUsers(callbackList)
DemoHelper.getInstance().getDataModel().updateUsersTimes(callbackList)
ChatUIKitClient.updateUsersInfo(callbackList)
}
onValueSuccess(callbackList)
}catch (e:ChatException){
ChatLog.e("fetchUsers", "fetchUsers error: ${e.description}")
}
}
}
})
.setGroupProfileProvider(object : ChatUIKitGroupProfileProvider {
override fun getGroup(id: String?): ChatUIKitGroupProfile? {
ChatClient.getInstance().groupManager().getGroup(id)?.let {
return ChatUIKitGroupProfile(it.groupId, it.groupName, it.extension)
}
return null
}
override fun fetchGroups(
groupIds: List<String>,
onValueSuccess: OnValueSuccess<List<ChatUIKitGroupProfile>>
) {
}
})
.setSettingsProvider(object : ChatUIKitSettingsProvider {
override fun isMsgNotifyAllowed(message: ChatMessage?): Boolean {
return true
}
override fun isMsgSoundAllowed(message: ChatMessage?): Boolean {
return false
}
override fun isMsgVibrateAllowed(message: ChatMessage?): Boolean {
return false
}
override val isSpeakerOpened: Boolean
get() = true
})
.setCustomActivityRoute(object : ChatUIKitCustomActivityRoute {
override fun getActivityRoute(intent: Intent): Intent? {
intent.component?.className?.let {
when(it) {
UIKitChatActivity::class.java.name -> {
intent.setClass(context, ChatActivity::class.java)
}
ChatUIKitGroupDetailActivity::class.java.name -> {
intent.setClass(context, ChatGroupDetailActivity::class.java)
}
ChatUIKitContactDetailsActivity::class.java.name -> {
intent.setClass(context, ChatContactDetailActivity::class.java)
}
ChatUIKitCreateGroupActivity::class.java.name -> {
intent.setClass(context, ChatCreateGroupActivity::class.java)
}
ChatUIKitContactCheckActivity::class.java.name ->{
intent.setClass(context, ChatContactCheckActivity::class.java)
}
ChatUIKitNewRequestsActivity::class.java.name ->{
intent.setClass(context, ChatNewRequestActivity::class.java)
}
else -> {
return intent
}
}
}
return intent
}
})
}
fun setUIKitConfigs(context: Context) {
ChatUIKitClient.getConfig()?.avatarConfig?.let {
it.avatarShape = ChatUIKitImageView.ShapeType.RECTANGLE
it.avatarRadius = context.resources.getDimensionPixelSize(com.hyphenate.easeui.R.dimen.ease_corner_extra_small)
}
}
}