Skip to content

Commit e1d6dff

Browse files
v4.3.11
1 parent e64538d commit e1d6dff

23 files changed

+610
-366
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId = "com.cometchat.kotlinsampleapp"
1212
minSdk = 21
1313
targetSdk = 34
14-
versionCode = 2
15-
versionName = "4.3.6"
14+
versionCode = 3
15+
versionName = "4.3.11"
1616

1717
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1818

app/src/main/assets/SampleUsers.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"users": [
3+
{
4+
"uid": "superhero1",
5+
"name": "Iron Man",
6+
"avatar": "https://assets.cometchat.io/sampleapp/users/ironman.png"
7+
},
8+
{
9+
"uid": "superhero2",
10+
"name": "Captain America",
11+
"avatar": "https://assets.cometchat.io/sampleapp/users/captainamerica.png"
12+
},
13+
{
14+
"uid": "superhero3",
15+
"name": "Spiderman",
16+
"avatar": "https://assets.cometchat.io/sampleapp/users/spiderman.png"
17+
},
18+
{
19+
"uid": "superhero4",
20+
"name": "Wolverine",
21+
"avatar": "https://assets.cometchat.io/sampleapp/users/wolverine.png"
22+
},
23+
{
24+
"uid": "superhero5",
25+
"name": "Cyclops",
26+
"avatar": "https://assets.cometchat.io/sampleapp/users/cyclops.png"
27+
}
28+
]
29+
}

app/src/main/java/com/cometchat/kotlinsampleapp/AppUtils.kt

Lines changed: 142 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,74 +11,169 @@ import com.cometchat.chat.core.CometChat
1111
import com.cometchat.chat.exceptions.CometChatException
1212
import com.cometchat.chat.models.Group
1313
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
1423

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-
}
2324

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+
}
3035

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+
}
3442

35-
val defaultGroup: Group?
36-
get() = group
43+
override fun onError(e: CometChatException?) {}
44+
})
45+
}
3746

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+
}
4061

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
44132

45-
fun switchDarkMode() {
46-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
47-
}
133+
val defaultGroup: Group?
134+
get() = group
48135

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+
}
54152

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(
58155
ContextCompat.getColor(
59156
context!!,
60157
R.color.white
61158
)
62159
)
63-
)
64-
}
160+
}
65161

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(
69164
ContextCompat.getColor(
70165
context!!,
71166
R.color.black
72167
)
73168
)
74-
)
75-
}
169+
}
76170

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+
}
80174

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+
}
83178
}
84179
}

app/src/main/java/com/cometchat/kotlinsampleapp/Application.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.cometchat.chat.core.CometChat
77
import com.cometchat.chatuikit.calls.CometChatCallActivity
88
import com.cometchat.chatuikit.shared.resources.theme.CometChatTheme
99
import com.cometchat.chatuikit.shared.resources.theme.Palette
10-
import com.cometchat.kotlinsampleapp.AppUtils.isNightMode
10+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.isNightMode
1111

1212
class Application : Application() {
1313

@@ -31,7 +31,7 @@ class Application : Application() {
3131
private fun addCallListener() {
3232
val LISTENER_ID = System.currentTimeMillis().toString() + ""
3333
CometChat.addCallListener(LISTENER_ID, object : CometChat.CallListener() {
34-
override fun onIncomingCallReceived(call: Call?) {
34+
override fun onIncomingCallReceived(call: Call) {
3535
CometChatCallActivity.launchIncomingCallScreen(applicationContext, call, null)
3636
}
3737

app/src/main/java/com/cometchat/kotlinsampleapp/activity/ComponentListActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import android.widget.TextView
99
import androidx.appcompat.app.AppCompatActivity
1010
import androidx.core.content.ContextCompat
1111
import com.cometchat.chatuikit.shared.resources.utils.Utils
12-
import com.cometchat.kotlinsampleapp.AppUtils.changeIconTintToBlack
13-
import com.cometchat.kotlinsampleapp.AppUtils.changeIconTintToWhite
14-
import com.cometchat.kotlinsampleapp.AppUtils.isNightMode
12+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeIconTintToBlack
13+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeIconTintToWhite
14+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.isNightMode
1515
import com.cometchat.kotlinsampleapp.R
1616
import com.cometchat.kotlinsampleapp.constants.StringConstants
1717
import com.cometchat.kotlinsampleapp.databinding.ActivityComponentListBinding

app/src/main/java/com/cometchat/kotlinsampleapp/activity/CreateUserActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import com.cometchat.chat.core.CometChat
1414
import com.cometchat.chat.exceptions.CometChatException
1515
import com.cometchat.chat.models.User
1616
import com.cometchat.chatuikit.shared.cometchatuikit.CometChatUIKit
17-
import com.cometchat.kotlinsampleapp.AppUtils.changeTextColorToBlack
18-
import com.cometchat.kotlinsampleapp.AppUtils.changeTextColorToWhite
19-
import com.cometchat.kotlinsampleapp.AppUtils.fetchDefaultObjects
20-
import com.cometchat.kotlinsampleapp.AppUtils.isNightMode
17+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeTextColorToBlack
18+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeTextColorToWhite
19+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.fetchDefaultObjects
20+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.isNightMode
2121
import com.cometchat.kotlinsampleapp.R
2222
import com.cometchat.kotlinsampleapp.databinding.ActivityCreateUserBinding
2323
import com.google.android.material.snackbar.Snackbar

app/src/main/java/com/cometchat/kotlinsampleapp/activity/HomeActivity.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import com.cometchat.chatuikit.shared.cometchatuikit.CometChatUIKit
1414
import com.cometchat.chatuikit.shared.resources.theme.CometChatTheme
1515
import com.cometchat.chatuikit.shared.resources.theme.Palette
1616
import com.cometchat.chatuikit.shared.resources.utils.Utils
17-
import com.cometchat.kotlinsampleapp.AppUtils.changeIconTintToBlack
18-
import com.cometchat.kotlinsampleapp.AppUtils.changeIconTintToWhite
19-
import com.cometchat.kotlinsampleapp.AppUtils.isNightMode
20-
import com.cometchat.kotlinsampleapp.AppUtils.switchDarkMode
21-
import com.cometchat.kotlinsampleapp.AppUtils.switchLightMode
17+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeIconTintToBlack
18+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeIconTintToWhite
19+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.isNightMode
20+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.switchDarkMode
21+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.switchLightMode
2222
import com.cometchat.kotlinsampleapp.R
2323
import com.cometchat.kotlinsampleapp.constants.StringConstants
2424
import com.cometchat.kotlinsampleapp.databinding.ActivityHomeBinding

app/src/main/java/com/cometchat/kotlinsampleapp/activity/LoginActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import com.cometchat.chat.core.CometChat
1616
import com.cometchat.chat.exceptions.CometChatException
1717
import com.cometchat.chat.models.User
1818
import com.cometchat.chatuikit.shared.cometchatuikit.CometChatUIKit
19-
import com.cometchat.kotlinsampleapp.AppUtils.changeTextColorToBlack
20-
import com.cometchat.kotlinsampleapp.AppUtils.changeTextColorToWhite
21-
import com.cometchat.kotlinsampleapp.AppUtils.fetchDefaultObjects
22-
import com.cometchat.kotlinsampleapp.AppUtils.isNightMode
19+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeTextColorToBlack
20+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.changeTextColorToWhite
21+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.fetchDefaultObjects
22+
import com.cometchat.kotlinsampleapp.AppUtils.Companion.isNightMode
2323
import com.cometchat.kotlinsampleapp.R
2424
import com.cometchat.kotlinsampleapp.databinding.ActivityLoginBinding
2525
import com.google.android.material.textfield.TextInputEditText

0 commit comments

Comments
 (0)