observe unread events for notifications from rooms which are not muted

This commit is contained in:
Adam Brown 2022-11-02 14:28:04 +00:00
parent ab6a5303dc
commit 34aa7f9502
4 changed files with 31 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import app.dapk.st.domain.room.MutedStorePersistence
import app.dapk.st.matrix.common.EventId import app.dapk.st.matrix.common.EventId
import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.sync.* import app.dapk.st.matrix.sync.*
import com.squareup.sqldelight.Query
import com.squareup.sqldelight.runtime.coroutines.asFlow import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList import com.squareup.sqldelight.runtime.coroutines.mapToList
import com.squareup.sqldelight.runtime.coroutines.mapToOneNotNull import com.squareup.sqldelight.runtime.coroutines.mapToOneNotNull
@ -56,10 +57,8 @@ internal class RoomPersistence(
}.distinctUntilChanged() }.distinctUntilChanged()
return database.roomEventQueries.selectRoom(roomId.value) return database.roomEventQueries.selectRoom(roomId.value)
.asFlow() .distinctFlowList()
.mapToList()
.map { it.map { json.decodeFromString(RoomEvent.serializer(), it) } } .map { it.map { json.decodeFromString(RoomEvent.serializer(), it) } }
.distinctUntilChanged()
.combine(overviewFlow) { events, overview -> .combine(overviewFlow) { events, overview ->
RoomState(overview, events) RoomState(overview, events)
} }
@ -91,9 +90,7 @@ internal class RoomPersistence(
override fun observeUnread(): Flow<Map<RoomOverview, List<RoomEvent>>> { override fun observeUnread(): Flow<Map<RoomOverview, List<RoomEvent>>> {
return database.roomEventQueries.selectAllUnread() return database.roomEventQueries.selectAllUnread()
.asFlow() .distinctFlowList()
.mapToList()
.distinctUntilChanged()
.map { .map {
it.groupBy { RoomId(it.room_id) } it.groupBy { RoomId(it.room_id) }
.mapKeys { overviewPersistence.retrieve(it.key)!! } .mapKeys { overviewPersistence.retrieve(it.key)!! }
@ -115,6 +112,22 @@ internal class RoomPersistence(
} }
} }
override fun observeNotMutedUnread(): Flow<Map<RoomOverview, List<RoomEvent>>> {
return database.roomEventQueries.selectNotMutedUnread()
.distinctFlowList()
.map {
it.groupBy { RoomId(it.room_id) }
.mapKeys { overviewPersistence.retrieve(it.key)!! }
.mapValues {
it.value.map {
json.decodeFromString(RoomEvent.serializer(), it.blob)
}
}
}
}
private fun <T : Any> Query<T>.distinctFlowList() = this.asFlow().mapToList().distinctUntilChanged()
override suspend fun markRead(roomId: RoomId) { override suspend fun markRead(roomId: RoomId) {
coroutineDispatchers.withIoContext { coroutineDispatchers.withIoContext {
database.unreadEventQueries.removeRead(room_id = roomId.value) database.unreadEventQueries.removeRead(room_id = roomId.value)

View File

@ -34,6 +34,16 @@ INNER JOIN dbRoomEvent ON dbUnreadEvent.event_id = dbRoomEvent.event_id
ORDER BY dbRoomEvent.timestamp_utc DESC ORDER BY dbRoomEvent.timestamp_utc DESC
LIMIT 100; LIMIT 100;
selectNotMutedUnread:
SELECT dbRoomEvent.blob, dbRoomEvent.room_id
FROM dbUnreadEvent
INNER JOIN dbRoomEvent ON dbUnreadEvent.event_id = dbRoomEvent.event_id
LEFT OUTER JOIN dbMutedRoom
ON dbUnreadEvent.room_id = dbMutedRoom.room_id
WHERE dbMutedRoom.room_id IS NULL
ORDER BY dbRoomEvent.timestamp_utc DESC
LIMIT 100;
remove: remove:
DELETE FROM dbRoomEvent DELETE FROM dbRoomEvent
WHERE room_id = ?; WHERE room_id = ?;

View File

@ -16,7 +16,7 @@ internal typealias ObserveUnreadNotificationsUseCase = () -> Flow<UnreadNotifica
class ObserveUnreadNotificationsUseCaseImpl(private val roomStore: RoomStore) : ObserveUnreadNotificationsUseCase { class ObserveUnreadNotificationsUseCaseImpl(private val roomStore: RoomStore) : ObserveUnreadNotificationsUseCase {
override fun invoke(): Flow<UnreadNotifications> { override fun invoke(): Flow<UnreadNotifications> {
return roomStore.observeUnread() return roomStore.observeNotMutedUnread()
.mapWithDiff() .mapWithDiff()
.avoidShowingPreviousNotificationsOnLaunch() .avoidShowingPreviousNotificationsOnLaunch()
.onlyRenderableChanges() .onlyRenderableChanges()

View File

@ -16,6 +16,7 @@ interface RoomStore : MuteableStore {
suspend fun markRead(roomId: RoomId) suspend fun markRead(roomId: RoomId)
fun observeUnread(): Flow<Map<RoomOverview, List<RoomEvent>>> fun observeUnread(): Flow<Map<RoomOverview, List<RoomEvent>>>
fun observeUnreadCountById(): Flow<Map<RoomId, Int>> fun observeUnreadCountById(): Flow<Map<RoomId, Int>>
fun observeNotMutedUnread(): Flow<Map<RoomOverview, List<RoomEvent>>>
fun observeEvent(eventId: EventId): Flow<EventId> fun observeEvent(eventId: EventId): Flow<EventId>
suspend fun findEvent(eventId: EventId): RoomEvent? suspend fun findEvent(eventId: EventId): RoomEvent?