Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".sample.SampleApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.getstream.android.core.sample

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
Expand All @@ -40,27 +39,17 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import io.getstream.android.core.api.StreamClient
import io.getstream.android.core.api.authentication.StreamTokenProvider
import io.getstream.android.core.api.model.connection.StreamConnectionState
import io.getstream.android.core.api.model.connection.recovery.Recovery
import io.getstream.android.core.api.model.value.StreamApiKey
import io.getstream.android.core.api.model.value.StreamHttpClientInfoHeader
import io.getstream.android.core.api.model.value.StreamToken
import io.getstream.android.core.api.model.value.StreamUserId
import io.getstream.android.core.api.model.value.StreamWsUrl
import io.getstream.android.core.api.socket.listeners.StreamClientListener
import io.getstream.android.core.api.subscribe.StreamSubscription
import io.getstream.android.core.api.subscribe.StreamSubscriptionManager
import io.getstream.android.core.sample.client.createStreamClient
import io.getstream.android.core.sample.ui.ConnectionStateCard
import io.getstream.android.core.sample.ui.theme.StreamandroidcoreTheme
import kotlinx.coroutines.launch

class SampleActivity : ComponentActivity(), StreamClientListener {

val userId = StreamUserId.fromString("petar")
var streamClient: StreamClient? = null

var handle: StreamSubscription? = null

override fun onRecovery(recovery: Recovery) {
Expand All @@ -75,43 +64,13 @@ class SampleActivity : ComponentActivity(), StreamClientListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val streamClient2 =
createStreamClient(
context = this.applicationContext,
scope = lifecycleScope,
apiKey = StreamApiKey.fromString("pd67s34fzpgw"),
userId = userId,
wsUrl =
StreamWsUrl.fromString(
"wss://chat-edge-frankfurt-ce1.stream-io-api.com/api/v2/connect"
),
clientInfoHeader =
StreamHttpClientInfoHeader.create(
product = "android-core",
productVersion = "1.0.0",
os = "Android",
apiLevel = Build.VERSION.SDK_INT,
deviceModel = "Pixel 7 Pro",
app = "Stream Android Core Sample",
appVersion = "1.0.0",
),
tokenProvider =
object : StreamTokenProvider {
override suspend fun loadToken(userId: StreamUserId): StreamToken {
return StreamToken.fromString(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoicGV0YXIifQ.mZFi4iSblaIoyo9JDdcxIkGkwI-tuApeSBawxpz42rs"
)
}
},
)
streamClient = streamClient2
val streamClient = SampleApp.instance.streamClient
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) { streamClient?.connect() }
repeatOnLifecycle(Lifecycle.State.CREATED) { streamClient.connect() }
}

if (handle == null) {
handle =
streamClient2
streamClient
.subscribe(
this,
options =
Expand All @@ -136,16 +95,16 @@ class SampleActivity : ComponentActivity(), StreamClientListener {
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Greeting(name = "Android")
ClientInfo(streamClient = streamClient2)
val state = streamClient?.connectionState?.collectAsStateWithLifecycle()
ClientInfo(streamClient = streamClient)
val state = streamClient.connectionState?.collectAsStateWithLifecycle()
val buttonState =
when (state?.value) {
is StreamConnectionState.Connected -> {
Triple(
"Disconnect",
true,
{
lifecycleScope.launch { streamClient?.disconnect() }
lifecycleScope.launch { streamClient.disconnect() }
Unit
},
)
Expand Down Expand Up @@ -174,6 +133,11 @@ class SampleActivity : ComponentActivity(), StreamClientListener {
}
}
}

override fun onDestroy() {
super.onDestroy()
handle?.cancel()
}
}

@Composable
Expand Down
91 changes: 91 additions & 0 deletions app/src/main/java/io/getstream/android/core/sample/SampleApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.android.core.sample

import android.annotation.SuppressLint
import android.app.Application
import android.os.Build
import io.getstream.android.core.api.StreamClient
import io.getstream.android.core.api.authentication.StreamTokenProvider
import io.getstream.android.core.api.model.config.StreamClientSerializationConfig
import io.getstream.android.core.api.model.value.StreamApiKey
import io.getstream.android.core.api.model.value.StreamHttpClientInfoHeader
import io.getstream.android.core.api.model.value.StreamToken
import io.getstream.android.core.api.model.value.StreamUserId
import io.getstream.android.core.api.model.value.StreamWsUrl
import io.getstream.android.core.api.serialization.StreamEventSerialization
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob

class SampleApp : Application() {

lateinit var streamClient: StreamClient
private val userId = StreamUserId.fromString("sample-user")
private val token =
StreamToken.fromString(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoicGV0YXIifQ.mZFi4iSblaIoyo9JDdcxIkGkwI-tuApeSBawxpz42rs"
)
private val coroutinesScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

companion object {
lateinit var instance: SampleApp
}

@SuppressLint("NotKeepingInstance")
override fun onCreate() {
super.onCreate()
instance = this
streamClient =
StreamClient(
context = this.applicationContext,
scope = coroutinesScope,
apiKey = StreamApiKey.fromString("pd67s34fzpgw"),
userId = userId,
products = listOf("feeds", "chat", "video"),
wsUrl =
StreamWsUrl.fromString(
"wss://chat-edge-frankfurt-ce1.stream-io-api.com/api/v2/connect"
),
clientInfoHeader =
StreamHttpClientInfoHeader.create(
product = "android-core",
productVersion = "1.1.0",
os = "Android",
apiLevel = Build.VERSION.SDK_INT,
deviceModel = "Pixel 7 Pro",
app = "Stream Android Core Sample",
appVersion = "1.0.0",
),
tokenProvider =
object : StreamTokenProvider {
override suspend fun loadToken(userId: StreamUserId): StreamToken {
return token
}
},
serializationConfig =
StreamClientSerializationConfig.default(
object : StreamEventSerialization<Unit> {
override fun serialize(data: Unit): Result<String> = Result.success("")

override fun deserialize(raw: String): Result<Unit> =
Result.success(Unit)
}
),
)
}
}

This file was deleted.

Loading