Skip to content

Commit 9162881

Browse files
authored
Merge pull request #20 from GetStream/use_stateflow
Use clientState StateFlow in tutorial
2 parents a6682ac + 7b86b5c commit 9162881

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

app/src/main/java/com/example/chattutorial/MainActivity.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.example.chattutorial
22

33
import android.os.Bundle
4-
import android.widget.Toast
54
import androidx.activity.ComponentActivity
65
import androidx.activity.compose.setContent
6+
import androidx.compose.material.Text
7+
import androidx.compose.runtime.collectAsState
8+
import androidx.compose.runtime.getValue
79
import androidx.compose.ui.res.stringResource
810
import io.getstream.chat.android.client.ChatClient
911
import io.getstream.chat.android.client.logger.ChatLogLevel
1012
import io.getstream.chat.android.compose.ui.channels.ChannelsScreen
1113
import io.getstream.chat.android.compose.ui.theme.ChatTheme
14+
import io.getstream.chat.android.models.InitializationState
1215
import io.getstream.chat.android.models.User
1316
import io.getstream.chat.android.offline.plugin.factory.StreamOfflinePluginFactory
1417
import io.getstream.chat.android.state.plugin.config.StatePluginConfig
@@ -47,11 +50,15 @@ class MainActivity : ComponentActivity() {
4750
client.connectUser(
4851
user = user,
4952
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoidHV0b3JpYWwtZHJvaWQifQ.WwfBzU1GZr0brt_fXnqKdKhz3oj0rbDUm2DqJO_SS5U"
50-
).enqueue {
51-
if (it.isSuccess) {
52-
// 4 - Set up the Channels Screen UI
53-
setContent {
54-
ChatTheme {
53+
).enqueue()
54+
55+
setContent {
56+
// Observe the client connection state
57+
val clientInitialisationState by client.clientState.initializationState.collectAsState()
58+
59+
ChatTheme {
60+
when (clientInitialisationState) {
61+
InitializationState.COMPLETE -> {
5562
ChannelsScreen(
5663
title = stringResource(id = R.string.app_name),
5764
isShowingSearch = true,
@@ -61,9 +68,13 @@ class MainActivity : ComponentActivity() {
6168
onBackPressed = { finish() }
6269
)
6370
}
71+
InitializationState.INITIALIZING -> {
72+
Text(text = "Initialising...")
73+
}
74+
InitializationState.NOT_INITIALIZED -> {
75+
Text(text = "Not initialized...")
76+
}
6477
}
65-
} else {
66-
Toast.makeText(this, "something went wrong!", Toast.LENGTH_SHORT).show()
6778
}
6879
}
6980
}

0 commit comments

Comments
 (0)