Skip to content

Commit 79c118d

Browse files
Merge pull request #32 from Vonage-Community/android-chat
Android chat App
2 parents 52838ae + c24bb9a commit 79c118d

38 files changed

+1149
-0
lines changed

messaging-kotlin/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

messaging-kotlin/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

messaging-kotlin/app/build.gradle

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
namespace 'com.vonage.tutorial.chat'
8+
compileSdk 34
9+
10+
defaultConfig {
11+
applicationId "com.vonage.tutorial.chat"
12+
minSdk 24
13+
targetSdk 34
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables {
19+
useSupportLibrary true
20+
}
21+
}
22+
23+
buildTypes {
24+
release {
25+
minifyEnabled false
26+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
27+
}
28+
}
29+
compileOptions {
30+
sourceCompatibility JavaVersion.VERSION_1_8
31+
targetCompatibility JavaVersion.VERSION_1_8
32+
}
33+
kotlinOptions {
34+
jvmTarget = '1.8'
35+
}
36+
buildFeatures {
37+
compose true
38+
}
39+
composeOptions {
40+
kotlinCompilerExtensionVersion '1.4.3'
41+
}
42+
packaging {
43+
resources {
44+
excludes += '/META-INF/{AL2.0,LGPL2.1}'
45+
}
46+
}
47+
}
48+
49+
dependencies {
50+
51+
implementation 'com.vonage:client-sdk-chat:1.3.0'
52+
53+
implementation 'androidx.core:core-ktx:1.12.0'
54+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
55+
implementation 'androidx.activity:activity-compose:1.8.1'
56+
implementation platform('androidx.compose:compose-bom:2023.03.00')
57+
implementation 'androidx.compose.ui:ui'
58+
implementation 'androidx.compose.ui:ui-graphics'
59+
implementation 'androidx.compose.ui:ui-tooling-preview'
60+
implementation 'androidx.compose.material3:material3'
61+
implementation platform('androidx.compose:compose-bom:2023.03.00')
62+
63+
testImplementation 'junit:junit:4.13.2'
64+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
65+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
66+
androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00')
67+
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
68+
androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00')
69+
debugImplementation 'androidx.compose.ui:ui-tooling'
70+
debugImplementation 'androidx.compose.ui:ui-test-manifest'
71+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.vonage.tutorial.chat
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.vonage.tutorial.chat", appContext.packageName)
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:dataExtractionRules="@xml/data_extraction_rules"
10+
android:fullBackupContent="@xml/backup_rules"
11+
android:icon="@mipmap/ic_launcher"
12+
android:label="@string/app_name"
13+
android:roundIcon="@mipmap/ic_launcher_round"
14+
android:supportsRtl="true"
15+
android:theme="@style/Theme.Chat"
16+
tools:targetApi="31">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true"
20+
android:label="@string/app_name"
21+
android:theme="@style/Theme.Chat">
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN" />
24+
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
</application>
29+
30+
</manifest>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.vonage.tutorial.chat
2+
3+
import android.app.Application
4+
import androidx.compose.runtime.getValue
5+
import androidx.compose.runtime.mutableStateListOf
6+
import androidx.compose.runtime.mutableStateOf
7+
import androidx.compose.runtime.setValue
8+
import androidx.compose.runtime.snapshots.SnapshotStateList
9+
import androidx.compose.runtime.toMutableStateList
10+
import androidx.lifecycle.AndroidViewModel
11+
import com.vonage.android_core.VGError
12+
import com.vonage.chat.ChatClient
13+
import com.vonage.clientcore.core.api.models.EmbeddedInfo
14+
import com.vonage.clientcore.core.api.models.GetConversationEventsParameters
15+
import com.vonage.clientcore.core.api.models.MemberJoinedConversationEvent
16+
import com.vonage.clientcore.core.api.models.MemberLeftConversationEvent
17+
import com.vonage.clientcore.core.api.models.MessageTextEvent
18+
import com.vonage.clientcore.core.api.models.PersistentConversationEvent
19+
import com.vonage.clientcore.core.api.models.PresentingOrder
20+
21+
class ChatViewModel(application: Application) : AndroidViewModel(application = application) {
22+
private val aliceJwt = "" //Set to ALICE JWT
23+
private val bobJwt = "" //Set to BOB JWT
24+
private val conversationID = "" //Set to created conversation ID that has Bob and Alice as members
25+
26+
var isLoggedIn by mutableStateOf(false)
27+
var isError by mutableStateOf(false)
28+
var error = ""
29+
private var memberID = "" //logged in users member ID for this conversation
30+
31+
var events : SnapshotStateList<PersistentConversationEvent> = mutableStateListOf()
32+
33+
private var client = ChatClient(getApplication<Application>().applicationContext)
34+
35+
fun login(username: String) {
36+
val jwt = if(username == "Alice") aliceJwt else bobJwt
37+
client.createSession(jwt) { err, sessionId ->
38+
when {
39+
err != null -> {
40+
isError = true
41+
error = err.localizedMessage?.toString() ?: ""
42+
}
43+
else -> {
44+
client.setOnConversationEventListener {
45+
events.add(it as PersistentConversationEvent)
46+
}
47+
isLoggedIn = true
48+
}
49+
}
50+
}
51+
}
52+
53+
suspend fun getMemberIDIfNeeded(){
54+
if(memberID.isNotEmpty()) return else getMemberID()
55+
}
56+
57+
private suspend fun getMemberID() {
58+
try {
59+
val member = client.getConversationMember(conversationID, "me")
60+
memberID = member.id
61+
}
62+
catch (e: VGError) {
63+
//User not yet a member of the conversation
64+
memberID = client.joinConversation(conversationID)
65+
}
66+
catch (err:Error) {
67+
isError = true
68+
error = err.localizedMessage?.toString() ?: ""
69+
}
70+
}
71+
72+
suspend fun getConversationEvents(){
73+
val params = GetConversationEventsParameters(PresentingOrder.ASC, 100)
74+
try {
75+
val eventsPage = client.getConversationEvents(conversationID, params)
76+
events.clear()
77+
events.addAll(eventsPage.events.toMutableStateList())
78+
}
79+
catch (err:Error) {
80+
isError = true
81+
error = err.localizedMessage?.toString() ?: ""
82+
}
83+
}
84+
85+
suspend fun sendMessage(message: String){
86+
try {
87+
client.sendMessageTextEvent(conversationID, message)
88+
} catch (err:Error) {
89+
isError = true
90+
error = err.localizedMessage?.toString() ?: ""
91+
}
92+
}
93+
94+
fun generateDisplayedText(event: PersistentConversationEvent): Pair<String, Boolean>{
95+
var from = "System"
96+
return when(event){
97+
is MemberJoinedConversationEvent -> {
98+
val from = event.body.user.name
99+
"$from joined" to false
100+
}
101+
is MemberLeftConversationEvent -> {
102+
val from = event.body.user.name
103+
"$from left" to false
104+
}
105+
is MessageTextEvent -> {
106+
var isUser = false
107+
val userInfo = event.from as EmbeddedInfo
108+
isUser = userInfo.memberId == memberID
109+
from = if(isUser) "" else "${userInfo.user.name}: "
110+
"$from ${event.body.text}" to isUser
111+
}
112+
else -> {
113+
"" to false
114+
}
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)