From ab6a5303dc8c1829c8a23ab32789fc865aafa22d Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 2 Nov 2022 14:16:20 +0000 Subject: [PATCH] converting the muted rooms to a database table --- .../kotlin/app/dapk/st/domain/StoreModule.kt | 2 +- .../dapk/st/domain/room/MutedRoomsStore.kt | 34 ++++++++++--------- .../sqldelight/app/dapk/db/model/MutedRoom.sq | 16 +++++++++ .../app/dapk/db/model/RoomMember.sq | 1 - 4 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 domains/store/src/main/sqldelight/app/dapk/db/model/MutedRoom.sq diff --git a/domains/store/src/main/kotlin/app/dapk/st/domain/StoreModule.kt b/domains/store/src/main/kotlin/app/dapk/st/domain/StoreModule.kt index a36db06..076693b 100644 --- a/domains/store/src/main/kotlin/app/dapk/st/domain/StoreModule.kt +++ b/domains/store/src/main/kotlin/app/dapk/st/domain/StoreModule.kt @@ -34,7 +34,7 @@ class StoreModule( private val coroutineDispatchers: CoroutineDispatchers, ) { - private val muteableStore by unsafeLazy { MutedStorePersistence(preferences) } + private val muteableStore by unsafeLazy { MutedStorePersistence(database, coroutineDispatchers) } fun overviewStore(): OverviewStore = OverviewPersistence(database, coroutineDispatchers) fun roomStore(): RoomStore { diff --git a/domains/store/src/main/kotlin/app/dapk/st/domain/room/MutedRoomsStore.kt b/domains/store/src/main/kotlin/app/dapk/st/domain/room/MutedRoomsStore.kt index 2169b02..4a45012 100644 --- a/domains/store/src/main/kotlin/app/dapk/st/domain/room/MutedRoomsStore.kt +++ b/domains/store/src/main/kotlin/app/dapk/st/domain/room/MutedRoomsStore.kt @@ -1,39 +1,41 @@ package app.dapk.st.domain.room -import app.dapk.st.core.Preferences -import app.dapk.st.core.append -import app.dapk.st.core.removeFromSet +import app.dapk.db.DapkDb +import app.dapk.st.core.CoroutineDispatchers +import app.dapk.st.core.withIoContext import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.sync.MuteableStore +import com.squareup.sqldelight.runtime.coroutines.asFlow +import com.squareup.sqldelight.runtime.coroutines.mapToList import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.firstOrNull -import kotlinx.coroutines.flow.onStart - -private const val KEY_MUTE = "mute" +import kotlinx.coroutines.flow.map internal class MutedStorePersistence( - private val preferences: Preferences + private val database: DapkDb, + private val coroutineDispatchers: CoroutineDispatchers, ) : MuteableStore { private val allMutedFlow = MutableSharedFlow>(replay = 1) override suspend fun mute(roomId: RoomId) { - preferences.append(KEY_MUTE, roomId.value).notifyChange() + coroutineDispatchers.withIoContext { + database.mutedRoomQueries.insertMuted(roomId.value) + } } override suspend fun unmute(roomId: RoomId) { - preferences.removeFromSet(KEY_MUTE, roomId.value).notifyChange() + coroutineDispatchers.withIoContext { + database.mutedRoomQueries.removeMuted(roomId.value) + } } - private suspend fun Set.notifyChange() = allMutedFlow.emit(this.map { RoomId(it) }.toSet()) - override suspend fun isMuted(roomId: RoomId) = allMutedFlow.firstOrNull()?.contains(roomId) ?: false - override fun observeMuted(): Flow> = allMutedFlow.onStart { emit(readAll()) } - - private suspend fun readAll(): Set { - return preferences.readStrings(KEY_MUTE)?.map { RoomId(it) }?.toSet() ?: emptySet() - } + override fun observeMuted(): Flow> = database.mutedRoomQueries.select() + .asFlow() + .mapToList() + .map { it.map { RoomId(it) }.toSet() } } \ No newline at end of file diff --git a/domains/store/src/main/sqldelight/app/dapk/db/model/MutedRoom.sq b/domains/store/src/main/sqldelight/app/dapk/db/model/MutedRoom.sq new file mode 100644 index 0000000..2054a20 --- /dev/null +++ b/domains/store/src/main/sqldelight/app/dapk/db/model/MutedRoom.sq @@ -0,0 +1,16 @@ +CREATE TABLE IF NOT EXISTS dbMutedRoom ( + room_id TEXT NOT NULL, + PRIMARY KEY (room_id) +); + +insertMuted: +INSERT OR REPLACE INTO dbMutedRoom(room_id) +VALUES (?); + +removeMuted: +DELETE FROM dbMutedRoom +WHERE room_id = ?; + +select: +SELECT room_id +FROM dbMutedRoom; diff --git a/domains/store/src/main/sqldelight/app/dapk/db/model/RoomMember.sq b/domains/store/src/main/sqldelight/app/dapk/db/model/RoomMember.sq index 081bccd..4e1de82 100644 --- a/domains/store/src/main/sqldelight/app/dapk/db/model/RoomMember.sq +++ b/domains/store/src/main/sqldelight/app/dapk/db/model/RoomMember.sq @@ -16,7 +16,6 @@ FROM dbRoomMember WHERE room_id = ? LIMIT ?; - insert: INSERT OR REPLACE INTO dbRoomMember(user_id, room_id, blob) VALUES (?, ?, ?); \ No newline at end of file