Skip to content

Commit 0c38218

Browse files
committed
Implement StreamVideoInitializer
1 parent 210dafd commit 0c38218

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
android:exported="false"
5353
tools:node="merge">
5454
<meta-data
55-
android:name="io.getstream.whatsappclone.chats.initializer.StreamChatInitializer"
55+
android:name="io.getstream.whatsappclone.initializer.MainInitializer"
5656
android:value="androidx.startup" />
5757
</provider>
5858
</application>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2023 Stream.IO, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.getstream.whatsappclone.initializer
18+
19+
import android.content.Context
20+
import androidx.startup.Initializer
21+
import io.getstream.whatsappclone.chats.initializer.StreamChatInitializer
22+
import io.getstream.whatsappclone.video.initializer.StreamVideoInitializer
23+
24+
class MainInitializer : Initializer<Unit> {
25+
26+
override fun create(context: Context) {
27+
}
28+
29+
override fun dependencies(): List<Class<out Initializer<*>>> = listOf(
30+
StreamChatInitializer::class.java,
31+
StreamVideoInitializer::class.java
32+
)
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023 Stream.IO, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.getstream.whatsappclone.video.initializer
18+
19+
import android.content.Context
20+
import dagger.hilt.EntryPoint
21+
import dagger.hilt.InstallIn
22+
import dagger.hilt.android.EntryPointAccessors
23+
import dagger.hilt.components.SingletonComponent
24+
25+
@EntryPoint
26+
@InstallIn(SingletonComponent::class)
27+
internal interface StreamVideoEntryPoint {
28+
29+
fun inject(streamVideoInitializer: StreamVideoInitializer)
30+
31+
companion object {
32+
33+
fun resolve(context: Context): StreamVideoEntryPoint {
34+
val appContext = context.applicationContext ?: throw IllegalStateException(
35+
"applicationContext was not found in NetworkEntryPoint"
36+
)
37+
return EntryPointAccessors.fromApplication(
38+
appContext,
39+
StreamVideoEntryPoint::class.java
40+
)
41+
}
42+
}
43+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2023 Stream.IO, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.getstream.whatsappclone.video.initializer
18+
19+
import android.content.Context
20+
import androidx.startup.Initializer
21+
import io.getstream.video.android.core.StreamVideoBuilder
22+
import io.getstream.video.android.model.User
23+
import io.getstream.whatsappclone.network.Dispatcher
24+
import io.getstream.whatsappclone.network.WhatsAppDispatchers
25+
import io.getstream.whatsappclone.network.service.StreamVideoTokenService
26+
import io.getstream.whatsappclone.video.BuildConfig
27+
import javax.inject.Inject
28+
import kotlinx.coroutines.CoroutineDispatcher
29+
import kotlinx.coroutines.CoroutineScope
30+
import kotlinx.coroutines.launch
31+
32+
class StreamVideoInitializer : Initializer<Unit> {
33+
34+
@Inject
35+
lateinit var streamVideoTokenService: StreamVideoTokenService
36+
37+
@Inject
38+
@Dispatcher(WhatsAppDispatchers.IO)
39+
lateinit var dispatcher: CoroutineDispatcher
40+
41+
private val coroutineScope = CoroutineScope(dispatcher)
42+
43+
override fun create(context: Context) {
44+
StreamVideoEntryPoint.resolve(context)
45+
46+
val userId = "stream_user"
47+
coroutineScope.launch {
48+
val token = streamVideoTokenService.fetchToken(
49+
userId = userId,
50+
apiKey = BuildConfig.STREAM_API_KEY
51+
).getOrThrow().token
52+
53+
// initialize Stream Video SDK
54+
StreamVideoBuilder(
55+
context = context,
56+
apiKey = BuildConfig.STREAM_API_KEY,
57+
token = token,
58+
user = User(
59+
id = userId,
60+
name = "Stream User",
61+
image = "http://placekitten.com/200/300",
62+
role = "admin"
63+
)
64+
).build()
65+
}
66+
}
67+
68+
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
69+
}

0 commit comments

Comments
 (0)