Implement Notification Settings
- Add UI and logic to inform the user of mismatched notification settings and help them correct it. - Display a warning when the system notification settings are disabled and a link out to the app settings. - A toggle to disable notifications for the device - A screen for editing the group and direct chat notification defaults. - A toggle for switching on/off atRoom and call notifications.
This commit is contained in:
parent
e64f0bd7d9
commit
a0ac324e3f
27 changed files with 924 additions and 59 deletions
|
|
@ -21,16 +21,22 @@ import io.element.android.libraries.matrix.api.room.MatrixRoomNotificationSettin
|
|||
import io.element.android.libraries.matrix.api.room.RoomNotificationMode
|
||||
import io.element.android.libraries.matrix.api.room.RoomNotificationSettings
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
interface NotificationSettingsService {
|
||||
/**
|
||||
* State of the current room notification settings flow ([MatrixRoomNotificationSettingsState.Unknown] if not started).
|
||||
*/
|
||||
val notificationSettingsChangeFlow : SharedFlow<Unit>
|
||||
suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, membersCount: Long): Result<RoomNotificationSettings>
|
||||
suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, membersCount: Long): Result<RoomNotificationMode>
|
||||
suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result<RoomNotificationSettings>
|
||||
suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, isOneToOne: Boolean): Result<RoomNotificationMode>
|
||||
suspend fun setDefaultRoomNotificationMode(isEncrypted: Boolean, mode: RoomNotificationMode, isOneToOne: Boolean): Result<Unit>
|
||||
suspend fun setRoomNotificationMode(roomId: RoomId, mode: RoomNotificationMode): Result<Unit>
|
||||
suspend fun restoreDefaultRoomNotificationMode(roomId: RoomId): Result<Unit>
|
||||
suspend fun muteRoom(roomId: RoomId): Result<Unit>
|
||||
suspend fun unmuteRoom(roomId: RoomId, isEncrypted: Boolean, membersCount: Long): Result<Unit>
|
||||
suspend fun unmuteRoom(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result<Unit>
|
||||
suspend fun isRoomMentionEnabled(): Result<Boolean>
|
||||
suspend fun setRoomMentionEnabled(enabled: Boolean): Result<Unit>
|
||||
suspend fun isCallEnabled(): Result<Boolean>
|
||||
suspend fun setCallEnabled(enabled: Boolean): Result<Unit>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,12 @@ interface MatrixRoom : Closeable {
|
|||
val activeMemberCount: Long
|
||||
val joinedMemberCount: Long
|
||||
|
||||
/**
|
||||
* A one-to-one is a room with exactly 2 members.
|
||||
* See [the Matrix spec](https://spec.matrix.org/latest/client-server-api/#default-underride-rules).
|
||||
*/
|
||||
val isOneToOne: Boolean get() = joinedMemberCount == 2L
|
||||
|
||||
/**
|
||||
* The current loaded members as a StateFlow.
|
||||
* Initial value is [MatrixRoomMembersState.Unknown].
|
||||
|
|
@ -178,6 +184,7 @@ interface MatrixRoom : Closeable {
|
|||
suspend fun endPoll(pollStartId: EventId, text: String): Result<Unit>
|
||||
|
||||
override fun close() = destroy()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -47,16 +47,22 @@ class RustNotificationSettingsService(
|
|||
notificationSettings.setDelegate(notificationSettingsDelegate)
|
||||
}
|
||||
|
||||
override suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, membersCount: Long): Result<RoomNotificationSettings> =
|
||||
override suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result<RoomNotificationSettings> =
|
||||
runCatching {
|
||||
notificationSettings.getRoomNotificationSettings(roomId.value, isEncrypted, isOneToOne(membersCount)).let(RoomNotificationSettingsMapper::map)
|
||||
notificationSettings.getRoomNotificationSettings(roomId.value, isEncrypted, isOneToOne).let(RoomNotificationSettingsMapper::map)
|
||||
}
|
||||
|
||||
override suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, membersCount: Long): Result<RoomNotificationMode> =
|
||||
override suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, isOneToOne: Boolean): Result<RoomNotificationMode> =
|
||||
runCatching {
|
||||
notificationSettings.getDefaultRoomNotificationMode(isEncrypted, isOneToOne(membersCount)).let(RoomNotificationSettingsMapper::mapMode)
|
||||
notificationSettings.getDefaultRoomNotificationMode(isEncrypted, isOneToOne).let(RoomNotificationSettingsMapper::mapMode)
|
||||
}
|
||||
|
||||
override suspend fun setDefaultRoomNotificationMode(isEncrypted: Boolean, mode: RoomNotificationMode, isOneToOne: Boolean): Result<Unit> = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
notificationSettings.setDefaultRoomNotificationMode(isEncrypted, isOneToOne, mode.let(RoomNotificationSettingsMapper::mapMode))
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setRoomNotificationMode(roomId: RoomId, mode: RoomNotificationMode): Result<Unit> = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
notificationSettings.setRoomNotificationMode(roomId.value, mode.let(RoomNotificationSettingsMapper::mapMode))
|
||||
|
|
@ -71,16 +77,33 @@ class RustNotificationSettingsService(
|
|||
|
||||
override suspend fun muteRoom(roomId: RoomId): Result<Unit> = setRoomNotificationMode(roomId, RoomNotificationMode.MUTE)
|
||||
|
||||
override suspend fun unmuteRoom(roomId: RoomId, isEncrypted: Boolean, membersCount: Long) = withContext(dispatchers.io) {
|
||||
override suspend fun unmuteRoom(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean) = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
notificationSettings.unmuteRoom(roomId.value, isEncrypted, isOneToOne(membersCount))
|
||||
notificationSettings.unmuteRoom(roomId.value, isEncrypted, isOneToOne)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A one-to-one is a room with exactly 2 members.
|
||||
* See [the Matrix spec](https://spec.matrix.org/latest/client-server-api/#default-underride-rules).
|
||||
* @param membersCount The active members count in a room
|
||||
*/
|
||||
private fun isOneToOne(membersCount: Long) = membersCount == 2L
|
||||
override suspend fun isRoomMentionEnabled(): Result<Boolean> = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
notificationSettings.isRoomMentionEnabled()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setRoomMentionEnabled(enabled: Boolean): Result<Unit> = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
notificationSettings.setRoomMentionEnabled(enabled)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun isCallEnabled(): Result<Boolean> = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
notificationSettings.isCallEnabled()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setCallEnabled(enabled: Boolean): Result<Unit> = withContext(dispatchers.io) {
|
||||
runCatching {
|
||||
notificationSettings.setCallEnabled(enabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -210,7 +210,7 @@ class RustMatrixRoom(
|
|||
val currentRoomNotificationSettings = currentState.roomNotificationSettings()
|
||||
_roomNotificationSettingsStateFlow.value = MatrixRoomNotificationSettingsState.Pending(prevRoomNotificationSettings = currentRoomNotificationSettings)
|
||||
runCatching {
|
||||
roomNotificationSettingsService.getRoomNotificationSettings(roomId, isEncrypted, activeMemberCount).getOrThrow()
|
||||
roomNotificationSettingsService.getRoomNotificationSettings(roomId, isEncrypted, isOneToOne).getOrThrow()
|
||||
}.map {
|
||||
_roomNotificationSettingsStateFlow.value = MatrixRoomNotificationSettingsState.Ready(it)
|
||||
}.onFailure {
|
||||
|
|
|
|||
|
|
@ -36,14 +36,18 @@ class FakeNotificationSettingsService : NotificationSettingsService {
|
|||
override val notificationSettingsChangeFlow: SharedFlow<Unit>
|
||||
get() = _roomNotificationSettingsStateFlow
|
||||
|
||||
override suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, membersCount: Long): Result<RoomNotificationSettings> {
|
||||
override suspend fun getRoomNotificationSettings(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result<RoomNotificationSettings> {
|
||||
return getRoomNotificationSettingsResult
|
||||
}
|
||||
|
||||
override suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, membersCount: Long): Result<RoomNotificationMode> {
|
||||
override suspend fun getDefaultRoomNotificationMode(isEncrypted: Boolean, isOneToOne: Boolean): Result<RoomNotificationMode> {
|
||||
return getDefaultRoomNotificationMode
|
||||
}
|
||||
|
||||
override suspend fun setDefaultRoomNotificationMode(isEncrypted: Boolean, mode: RoomNotificationMode, isOneToOne: Boolean): Result<Unit> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override suspend fun setRoomNotificationMode(roomId: RoomId, mode: RoomNotificationMode): Result<Unit> {
|
||||
return setRoomNotificationMode
|
||||
}
|
||||
|
|
@ -56,7 +60,24 @@ class FakeNotificationSettingsService : NotificationSettingsService {
|
|||
return muteRoomResult
|
||||
}
|
||||
|
||||
override suspend fun unmuteRoom(roomId: RoomId, isEncrypted: Boolean, membersCount: Long): Result<Unit> {
|
||||
override suspend fun unmuteRoom(roomId: RoomId, isEncrypted: Boolean, isOneToOne: Boolean): Result<Unit> {
|
||||
return unmuteRoomResult
|
||||
}
|
||||
|
||||
override suspend fun isRoomMentionEnabled(): Result<Boolean> {
|
||||
return Result.success(false)
|
||||
}
|
||||
|
||||
override suspend fun setRoomMentionEnabled(enabled: Boolean): Result<Unit> {
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
override suspend fun isCallEnabled(): Result<Boolean> {
|
||||
return Result.success(false)
|
||||
}
|
||||
|
||||
override suspend fun setCallEnabled(enabled: Boolean): Result<Unit> {
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
|||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
|
@ -123,7 +124,7 @@ class DefaultPushHandler @Inject constructor(
|
|||
}
|
||||
|
||||
val userPushStore = userPushStoreFactory.create(userId)
|
||||
if (!userPushStore.areNotificationEnabledForDevice()) {
|
||||
if (!userPushStore.getNotificationEnabledForDevice().first()) {
|
||||
// TODO We need to check if this is an incoming call
|
||||
Timber.tag(loggerTag.value).i("Notification are disabled for this device, ignore push.")
|
||||
return
|
||||
|
|
|
|||
|
|
@ -22,5 +22,6 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.coroutines.core)
|
||||
implementation(projects.libraries.matrix.api)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
|
||||
package io.element.android.libraries.pushstore.api
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
|
||||
/**
|
||||
* Store data related to push about a user.
|
||||
|
|
@ -25,7 +27,7 @@ interface UserPushStore {
|
|||
suspend fun getCurrentRegisteredPushKey(): String?
|
||||
suspend fun setCurrentRegisteredPushKey(value: String)
|
||||
|
||||
suspend fun areNotificationEnabledForDevice(): Boolean
|
||||
fun getNotificationEnabledForDevice(): Flow<Boolean>
|
||||
suspend fun setNotificationEnabledForDevice(enabled: Boolean)
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import androidx.test.platform.app.InstrumentationRegistry
|
|||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.pushstore.api.UserPushStore
|
||||
import io.element.android.libraries.sessionstorage.test.observer.NoOpSessionObserver
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Test
|
||||
import kotlin.concurrent.thread
|
||||
|
|
@ -49,8 +50,8 @@ class DefaultUserPushStoreFactoryTest {
|
|||
thread1.join()
|
||||
thread2.join()
|
||||
runBlocking {
|
||||
userPushStore1!!.areNotificationEnabledForDevice()
|
||||
userPushStore2!!.areNotificationEnabledForDevice()
|
||||
userPushStore1!!.getNotificationEnabledForDevice().first()
|
||||
userPushStore2!!.getNotificationEnabledForDevice().first()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,9 @@ import androidx.datastore.preferences.preferencesDataStore
|
|||
import io.element.android.libraries.core.bool.orTrue
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.pushstore.api.UserPushStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
/**
|
||||
* Store data related to push about a user.
|
||||
|
|
@ -60,8 +62,8 @@ class UserPushStoreDataStore(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun areNotificationEnabledForDevice(): Boolean {
|
||||
return context.dataStore.data.first()[notificationEnabled].orTrue()
|
||||
override fun getNotificationEnabledForDevice(): Flow<Boolean> {
|
||||
return context.dataStore.data.map{ it[notificationEnabled].orTrue() }
|
||||
}
|
||||
|
||||
override suspend fun setNotificationEnabledForDevice(enabled: Boolean) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue