Skip to content

Commit c92f85f

Browse files
committed
Add ChatClientDatabase, DateConverter, and ChatClientRepository for message receipt management
1 parent ff21ef9 commit c92f85f

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream 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+
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
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.chat.android.client.persistence.db
18+
19+
import android.content.Context
20+
import androidx.room.Database
21+
import androidx.room.Room
22+
import androidx.room.RoomDatabase
23+
import androidx.room.TypeConverters
24+
import androidx.sqlite.db.SupportSQLiteDatabase
25+
import io.getstream.chat.android.client.persistence.db.converter.DateConverter
26+
import io.getstream.chat.android.client.persistence.db.dao.MessageReceiptDao
27+
import io.getstream.chat.android.client.persistence.db.entity.MessageReceiptEntity
28+
29+
@Database(
30+
entities = [MessageReceiptEntity::class],
31+
version = 1,
32+
exportSchema = false,
33+
)
34+
@TypeConverters(
35+
DateConverter::class,
36+
)
37+
internal abstract class ChatClientDatabase : RoomDatabase() {
38+
abstract fun messageReceiptDao(): MessageReceiptDao
39+
40+
companion object {
41+
fun build(context: Context) = Room.databaseBuilder(
42+
context = context.applicationContext,
43+
klass = ChatClientDatabase::class.java,
44+
name = "stream_chat_client.db",
45+
)
46+
.fallbackToDestructiveMigration()
47+
.addCallback(
48+
object : Callback() {
49+
override fun onOpen(db: SupportSQLiteDatabase) {
50+
db.execSQL("PRAGMA synchronous = NORMAL")
51+
}
52+
},
53+
)
54+
.build()
55+
}
56+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream 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+
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
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.chat.android.client.persistence.db.converter
18+
19+
import androidx.room.TypeConverter
20+
import java.util.Date
21+
22+
internal class DateConverter {
23+
@TypeConverter
24+
fun fromDb(value: Long?): Date? = value?.let(::Date)
25+
26+
@TypeConverter
27+
fun toDb(date: Date?): Long? = date?.time
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream 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+
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
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.chat.android.client.persistence.repository
18+
19+
import io.getstream.chat.android.client.persistence.db.ChatClientDatabase
20+
21+
internal class ChatClientRepository(
22+
private val messageReceiptRepository: MessageReceiptRepository,
23+
) : MessageReceiptRepository by messageReceiptRepository {
24+
25+
suspend fun clear() {
26+
messageReceiptRepository.clearMessageReceipts()
27+
}
28+
29+
companion object {
30+
fun from(database: ChatClientDatabase) = ChatClientRepository(
31+
messageReceiptRepository = MessageReceiptRepository(database),
32+
)
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream 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+
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
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.chat.android.client.persistence.repository
18+
19+
import kotlinx.coroutines.test.runTest
20+
import org.junit.Test
21+
import org.mockito.Mockito.mock
22+
import org.mockito.kotlin.verifyBlocking
23+
24+
internal class ChatClientRepositoryTest {
25+
26+
@Test
27+
fun `should clear repositories`() = runTest {
28+
val fixture = Fixture()
29+
val sut = fixture.get()
30+
31+
sut.clear()
32+
33+
fixture.verifyRepositoriesCleared()
34+
}
35+
36+
private class Fixture {
37+
private val mockMessageReceiptRepository = mock<MessageReceiptRepository>()
38+
39+
fun verifyRepositoriesCleared() {
40+
verifyBlocking(mockMessageReceiptRepository) { clearMessageReceipts() }
41+
}
42+
43+
fun get() = ChatClientRepository(
44+
messageReceiptRepository = mockMessageReceiptRepository,
45+
)
46+
}
47+
}

0 commit comments

Comments
 (0)