@@ -11,74 +11,169 @@ import com.cometchat.chat.core.CometChat
11
11
import com.cometchat.chat.exceptions.CometChatException
12
12
import com.cometchat.chat.models.Group
13
13
import com.cometchat.chat.models.User
14
+ import com.cometchat.chatuikit.shared.resources.utils.Utils
15
+ import com.cometchat.kotlinsampleapp.constants.StringConstants
16
+ import okhttp3.Call
17
+ import okhttp3.Callback
18
+ import okhttp3.OkHttpClient
19
+ import okhttp3.Request
20
+ import okhttp3.Response
21
+ import org.json.JSONObject
22
+ import java.io.IOException
14
23
15
- object AppUtils {
16
- private var group: Group ? = null
17
- private var user: User ? = null
18
- fun fetchDefaultObjects () {
19
- CometChat .getGroup(" supergroup" , object : CometChat .CallbackListener <Group ?>() {
20
- override fun onSuccess (group_ : Group ? ) {
21
- group = group_
22
- }
23
24
24
- override fun onError (e : CometChatException ? ) {}
25
- })
26
- CometChat .getUser(" superhero5" , object : CometChat .CallbackListener <User ?>() {
27
- override fun onSuccess (user_ : User ? ) {
28
- user = user_
29
- }
25
+ class AppUtils {
26
+ companion object {
27
+ private var group: Group ? = null
28
+ private var user: User ? = null
29
+ private var userList: MutableList <User > = ArrayList ()
30
+ fun fetchDefaultObjects () {
31
+ CometChat .getGroup(" supergroup" , object : CometChat .CallbackListener <Group ?>() {
32
+ override fun onSuccess (group_ : Group ? ) {
33
+ group = group_
34
+ }
30
35
31
- override fun onError (e : CometChatException ? ) {}
32
- })
33
- }
36
+ override fun onError (e : CometChatException ? ) {}
37
+ })
38
+ CometChat .getUser(" superhero5" , object : CometChat .CallbackListener <User ?>() {
39
+ override fun onSuccess (user_ : User ? ) {
40
+ user = user_
41
+ }
34
42
35
- val defaultGroup: Group ?
36
- get() = group
43
+ override fun onError (e : CometChatException ? ) {}
44
+ })
45
+ }
37
46
38
- val defaultUser: User ?
39
- get() = user
47
+ fun fetchSampleUsers (listener : CometChat .CallbackListener <List <User >>) {
48
+ if (userList.isEmpty()) {
49
+ val request: Request =
50
+ Request .Builder ().url(StringConstants .SAMPLE_APP_USERS_URL )
51
+ .method(" GET" , null ).build()
52
+ val client = OkHttpClient ()
53
+ client.newCall(request).enqueue(object : Callback {
54
+ override fun onFailure (call : Call , e : IOException ) {
55
+ Utils .runOnMainThread {
56
+ listener.onError(
57
+ CometChatException (" 11" , e.message)
58
+ )
59
+ }
60
+ }
40
61
41
- fun switchLightMode () {
42
- AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
43
- }
62
+ override fun onResponse (call : Call , response : Response ) {
63
+ if (response.isSuccessful && response.body != null ) {
64
+ try {
65
+ userList = processSampleUserList(response.body!! .string())
66
+ } catch (e: IOException ) {
67
+ Utils .runOnMainThread {
68
+ listener.onError(
69
+ CometChatException (" 10" , e.message)
70
+ )
71
+ }
72
+ }
73
+ Utils .runOnMainThread {
74
+ listener.onSuccess(
75
+ userList
76
+ )
77
+ }
78
+ } else {
79
+ Utils .runOnMainThread {
80
+ listener.onError(
81
+ CometChatException (
82
+ " Unexpected code " ,
83
+ response.code.toString()
84
+ )
85
+ )
86
+ }
87
+ }
88
+ }
89
+ })
90
+ } else {
91
+ Utils .runOnMainThread {
92
+ listener.onSuccess(
93
+ userList
94
+ )
95
+ }
96
+ }
97
+ }
98
+ fun processSampleUserList (jsonString : String? ): MutableList <User > {
99
+ val users: MutableList <User > = java.util.ArrayList ()
100
+ try {
101
+ val jsonObject = JSONObject (jsonString)
102
+ val jsonArray = jsonObject.getJSONArray(StringConstants .KEY_USER )
103
+ for (i in 0 until jsonArray.length()) {
104
+ val userJson = jsonArray.getJSONObject(i)
105
+ val user = User ()
106
+ user.uid = userJson.getString(StringConstants .UID )
107
+ user.name = userJson.getString(StringConstants .NAME )
108
+ user.avatar = userJson.getString(StringConstants .AVATAR )
109
+ users.add(user)
110
+ }
111
+ } catch (ignore: java.lang.Exception ) {
112
+ }
113
+ return users
114
+ }
115
+ fun loadJSONFromAsset (context : Context ): String? {
116
+ var json: String? = null
117
+ try {
118
+ val `is ` = context.assets.open(" SampleUsers.json" )
119
+ val size = `is `.available()
120
+ val buffer = ByteArray (size)
121
+ `is `.read(buffer)
122
+ `is `.close()
123
+ json = String (buffer, charset(" UTF-8" ))
124
+ } catch (ex: IOException ) {
125
+ ex.printStackTrace()
126
+ return null
127
+ }
128
+ return json
129
+ }
130
+ val defaultUserList: List <User ?>
131
+ get() = userList
44
132
45
- fun switchDarkMode () {
46
- AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
47
- }
133
+ val defaultGroup: Group ?
134
+ get() = group
48
135
49
- fun isNightMode (context : Context ): Boolean {
50
- val currentNightMode =
51
- context.resources.configuration.uiMode and Configuration .UI_MODE_NIGHT_MASK
52
- return currentNightMode == Configuration .UI_MODE_NIGHT_YES
53
- }
136
+ val defaultUser: User ?
137
+ get() = user
138
+
139
+ fun switchLightMode () {
140
+ AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
141
+ }
142
+
143
+ fun switchDarkMode () {
144
+ AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
145
+ }
146
+
147
+ fun isNightMode (context : Context ): Boolean {
148
+ val currentNightMode =
149
+ context.resources.configuration.uiMode and Configuration .UI_MODE_NIGHT_MASK
150
+ return currentNightMode == Configuration .UI_MODE_NIGHT_YES
151
+ }
54
152
55
- fun changeIconTintToWhite (context : Context ? , imageView : ImageView ) {
56
- imageView.setImageTintList(
57
- ColorStateList .valueOf(
153
+ fun changeIconTintToWhite (context : Context ? , imageView : ImageView ) {
154
+ imageView.imageTintList = ColorStateList .valueOf(
58
155
ContextCompat .getColor(
59
156
context!! ,
60
157
R .color.white
61
158
)
62
159
)
63
- )
64
- }
160
+ }
65
161
66
- fun changeIconTintToBlack (context : Context ? , imageView : ImageView ) {
67
- imageView.setImageTintList(
68
- ColorStateList .valueOf(
162
+ fun changeIconTintToBlack (context : Context ? , imageView : ImageView ) {
163
+ imageView.imageTintList = ColorStateList .valueOf(
69
164
ContextCompat .getColor(
70
165
context!! ,
71
166
R .color.black
72
167
)
73
168
)
74
- )
75
- }
169
+ }
76
170
77
- fun changeTextColorToWhite (context : Context ? , textView : TextView ) {
78
- textView.setTextColor(ContextCompat .getColor(context!! , R .color.white))
79
- }
171
+ fun changeTextColorToWhite (context : Context ? , textView : TextView ) {
172
+ textView.setTextColor(ContextCompat .getColor(context!! , R .color.white))
173
+ }
80
174
81
- fun changeTextColorToBlack (context : Context ? , textView : TextView ) {
82
- textView.setTextColor(ContextCompat .getColor(context!! , R .color.black))
175
+ fun changeTextColorToBlack (context : Context ? , textView : TextView ) {
176
+ textView.setTextColor(ContextCompat .getColor(context!! , R .color.black))
177
+ }
83
178
}
84
179
}
0 commit comments