Skip to content

Commit 86aae64

Browse files
committed
Introduce ChatClientRepository to encapsulate internal repositories used by ChatClient. This new repository is now a required dependency for ChatClient.
1 parent 9b737d8 commit 86aae64

File tree

8 files changed

+38
-8
lines changed

8 files changed

+38
-8
lines changed

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ import io.getstream.chat.android.client.parser2.adapters.internal.StreamDateForm
122122
import io.getstream.chat.android.client.persistance.repository.RepositoryFacade
123123
import io.getstream.chat.android.client.persistance.repository.factory.RepositoryFactory
124124
import io.getstream.chat.android.client.persistance.repository.noop.NoOpRepositoryFactory
125+
import io.getstream.chat.android.client.persistence.db.ChatClientDatabase
126+
import io.getstream.chat.android.client.persistence.repository.ChatClientRepository
125127
import io.getstream.chat.android.client.plugin.DependencyResolver
126128
import io.getstream.chat.android.client.plugin.MessageDeliveredPluginFactory
127129
import io.getstream.chat.android.client.plugin.Plugin
@@ -276,6 +278,7 @@ internal constructor(
276278
@InternalStreamChatApi
277279
public val audioPlayer: AudioPlayer,
278280
private val now: () -> Date = ::Date,
281+
private val repository: ChatClientRepository,
279282
) {
280283
private val logger by taggedLogger(TAG)
281284
private val waitConnection = MutableSharedFlow<Result<ConnectionData>>()
@@ -1509,6 +1512,8 @@ internal constructor(
15091512
userCredentialStorage.clear()
15101513
}
15111514

1515+
repository.clear()
1516+
15121517
_repositoryFacade = null
15131518
attachmentsSender.cancelJobs()
15141519
appSettingsManager.clear()
@@ -4751,12 +4756,14 @@ internal constructor(
47514756
isMarshmallowOrHigher = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M,
47524757
)
47534758

4759+
val database = ChatClientDatabase.build(appContext)
4760+
47544761
return ChatClient(
4755-
config,
4756-
module.api(),
4757-
module.dtoMapping,
4758-
module.notifications(),
4759-
tokenManager,
4762+
config = config,
4763+
api = module.api(),
4764+
dtoMapping = module.dtoMapping,
4765+
notifications = module.notifications(),
4766+
tokenManager = tokenManager,
47604767
userCredentialStorage = userCredentialStorage ?: SharedPreferencesCredentialStorage(appContext),
47614768
userStateService = module.userStateService,
47624769
clientDebugger = clientDebugger ?: StubChatClientDebugger,
@@ -4774,6 +4781,7 @@ internal constructor(
47744781
mutableClientState = MutableClientState(module.networkStateProvider),
47754782
currentUserFetcher = module.currentUserFetcher,
47764783
audioPlayer = audioPlayer,
4784+
repository = ChatClientRepository.from(database),
47774785
).apply {
47784786
attachmentsSender = AttachmentsSender(
47794787
context = appContext,

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/persistence/repository/ChatClientRepository.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

1717
package io.getstream.chat.android.client.persistence.repository
1818

19+
import io.getstream.chat.android.client.ChatClient
1920
import io.getstream.chat.android.client.persistence.db.ChatClientDatabase
2021

22+
/**
23+
* Repository that aggregates all internal repositories used by [ChatClient].
24+
*/
2125
internal class ChatClientRepository(
2226
private val messageReceiptRepository: MessageReceiptRepository,
2327
) : MessageReceiptRepository by messageReceiptRepository {

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/ChatClientConnectionTests.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import io.getstream.chat.android.client.events.ErrorEvent
2626
import io.getstream.chat.android.client.network.NetworkStateProvider
2727
import io.getstream.chat.android.client.parser2.adapters.internal.StreamDateFormatter
2828
import io.getstream.chat.android.client.persistance.repository.noop.NoOpRepositoryFactory
29+
import io.getstream.chat.android.client.persistence.repository.ChatClientRepository
2930
import io.getstream.chat.android.client.scope.ClientTestScope
3031
import io.getstream.chat.android.client.scope.UserTestScope
3132
import io.getstream.chat.android.client.setup.state.internal.MutableClientState
3233
import io.getstream.chat.android.client.socket.FakeChatSocket
3334
import io.getstream.chat.android.client.token.FakeTokenManager
34-
import io.getstream.chat.android.client.token.TokenManager
3535
import io.getstream.chat.android.client.user.CredentialConfig
3636
import io.getstream.chat.android.client.user.storage.UserCredentialStorage
3737
import io.getstream.chat.android.client.utils.TokenUtils
@@ -60,6 +60,7 @@ import org.amshove.kluent.shouldBeInstanceOf
6060
import org.junit.jupiter.api.BeforeEach
6161
import org.junit.jupiter.api.Test
6262
import org.junit.jupiter.api.extension.RegisterExtension
63+
import org.mockito.kotlin.any
6364
import org.mockito.kotlin.doReturn
6465
import org.mockito.kotlin.eq
6566
import org.mockito.kotlin.mock
@@ -91,7 +92,6 @@ internal class ChatClientConnectionTests {
9192
private val mutableClientState: MutableClientState = MutableClientState(mock())
9293
private val streamDateFormatter = StreamDateFormatter()
9394
private val config = mock<ChatClientConfig>()
94-
private val tokenManager = mock<TokenManager>()
9595
private val userCredentialStorage = mock<UserCredentialStorage>()
9696

9797
@BeforeEach
@@ -114,6 +114,9 @@ internal class ChatClientConnectionTests {
114114
tokenManager = tokenManager,
115115
networkStateProvider = networkStateProvider,
116116
)
117+
val mockRepository = mock<ChatClientRepository> {
118+
onBlocking { getAllMessageReceiptsByType(type = any(), limit = any()) } doReturn emptyList()
119+
}
117120
client = ChatClient(
118121
config = config,
119122
api = chatApi,
@@ -133,6 +136,7 @@ internal class ChatClientConnectionTests {
133136
mutableClientState = mutableClientState,
134137
currentUserFetcher = mock(),
135138
audioPlayer = mock(),
139+
repository = mockRepository,
136140
).apply {
137141
attachmentsSender = mock()
138142
}

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/ChatClientTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import io.getstream.chat.android.client.notifications.handler.NotificationConfig
3535
import io.getstream.chat.android.client.parser.EventArguments
3636
import io.getstream.chat.android.client.parser2.adapters.internal.StreamDateFormatter
3737
import io.getstream.chat.android.client.persistance.repository.noop.NoOpRepositoryFactory
38+
import io.getstream.chat.android.client.persistence.repository.ChatClientRepository
3839
import io.getstream.chat.android.client.plugin.factory.PluginFactory
3940
import io.getstream.chat.android.client.scope.ClientTestScope
4041
import io.getstream.chat.android.client.scope.UserTestScope
@@ -97,8 +98,8 @@ internal class ChatClientTest {
9798
val tokenUtils: TokenUtils = mock()
9899
var pluginFactories: List<PluginFactory> = emptyList()
99100
var errorHandlerFactories: List<ErrorHandlerFactory> = emptyList()
100-
private val streamDateFormatter = StreamDateFormatter()
101101

102+
@Suppress("LongMethod")
102103
@BeforeEach
103104
fun setUp() {
104105
val apiKey = "api-key"
@@ -133,6 +134,9 @@ internal class ChatClientTest {
133134
wssUrl = wssUrl,
134135
networkStateProvider = networkStateProvider,
135136
)
137+
val mockRepository = mock<ChatClientRepository> {
138+
onBlocking { getAllMessageReceiptsByType(type = any(), limit = any()) } doReturn emptyList()
139+
}
136140
client = ChatClient(
137141
config = config,
138142
api = api,
@@ -152,6 +156,7 @@ internal class ChatClientTest {
152156
repositoryFactoryProvider = NoOpRepositoryFactory.Provider,
153157
currentUserFetcher = mock(),
154158
audioPlayer = mock(),
159+
repository = mockRepository,
155160
).apply {
156161
attachmentsSender = mock()
157162
connectUser(user, token).enqueue()

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/DependencyResolverTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.getstream.chat.android.client
1818

19+
import io.getstream.chat.android.client.DependencyResolverTest.Companion.initializationStatesArguments
1920
import io.getstream.chat.android.client.api2.mapping.DtoMapping
2021
import io.getstream.chat.android.client.plugin.Plugin
2122
import io.getstream.chat.android.client.plugin.factory.PluginFactory
@@ -178,6 +179,7 @@ public class DependencyResolverTest {
178179
mutableClientState = mutableClientState,
179180
currentUserFetcher = mock(),
180181
audioPlayer = mock(),
182+
repository = mock(),
181183
).apply {
182184
this.plugins = this@Fixture.plugins
183185
}

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/MockClientBuilder.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ internal class MockClientBuilder(
126126
mutableClientState = mutableClientState,
127127
currentUserFetcher = mock(),
128128
audioPlayer = streamPlayer,
129+
repository = mock(),
129130
)
130131

131132
client.attachmentsSender = attachmentSender

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/chatclient/BaseChatClientTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ internal open class BaseChatClientTest {
128128
currentUserFetcher = currentUserFetcher,
129129
audioPlayer = mock(),
130130
now = { now },
131+
repository = mock(),
131132
)
132133
chatClient.attachmentsSender = attachmentsSender
133134
chatClient.plugins = plugins

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/debugger/ChatClientDebuggerTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import io.getstream.chat.android.client.events.ChatEvent
2929
import io.getstream.chat.android.client.network.NetworkStateProvider
3030
import io.getstream.chat.android.client.notifications.handler.NotificationConfig
3131
import io.getstream.chat.android.client.persistance.repository.noop.NoOpRepositoryFactory
32+
import io.getstream.chat.android.client.persistence.repository.ChatClientRepository
3233
import io.getstream.chat.android.client.plugin.factory.PluginFactory
3334
import io.getstream.chat.android.client.scope.ClientTestScope
3435
import io.getstream.chat.android.client.scope.UserTestScope
@@ -123,6 +124,9 @@ internal class ChatClientDebuggerTest {
123124
isRetrying: Boolean,
124125
): SendMessageDebugger = sendMessageDebugger
125126
}
127+
val mockRepository = mock<ChatClientRepository> {
128+
onBlocking { getAllMessageReceiptsByType(type = any(), limit = any()) } doReturn emptyList()
129+
}
126130
client = ChatClient(
127131
config = config,
128132
api = api,
@@ -143,6 +147,7 @@ internal class ChatClientDebuggerTest {
143147
repositoryFactoryProvider = NoOpRepositoryFactory.Provider,
144148
currentUserFetcher = mock(),
145149
audioPlayer = mock(),
150+
repository = mockRepository,
146151
).apply {
147152
attachmentsSender = this@ChatClientDebuggerTest.attachmentsSender
148153
connectUser(user, token).enqueue()

0 commit comments

Comments
 (0)