From 14b21dc0397c5b4e77a5ad5c94e220847b82cb14 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 17 Nov 2022 10:34:11 +0100 Subject: [PATCH] Adding use cases to create and delete notifications settings in account data --- .../LocalNotificationSettingsContent.kt | 3 +- ...eNotificationSettingsAccountDataUseCase.kt | 38 ++++++++++++ ...eNotificationSettingsAccountDataUseCase.kt | 33 +++++++++++ ...ificationSettingsAccountDataUseCaseTest.kt | 59 +++++++++++++++++++ ...ificationSettingsAccountDataUseCaseTest.kt | 49 +++++++++++++++ 5 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt index 2a95ccce7a..6998d9dcf2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/account/LocalNotificationSettingsContent.kt @@ -21,5 +21,6 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) data class LocalNotificationSettingsContent( - @Json(name = "is_silenced") val isSilenced: Boolean = false + @Json(name = "is_silenced") + val isSilenced: Boolean? = false ) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt new file mode 100644 index 0000000000..e2ee19e5cd --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCase.kt @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.devices.v2.notification + +import im.vector.app.features.settings.VectorPreferences +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent +import org.matrix.android.sdk.api.session.Session +import javax.inject.Inject + +class CreateNotificationSettingsAccountDataUseCase @Inject constructor( + private val vectorPreferences: VectorPreferences, + private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase +) { + + // TODO to be called on session start when background sync is enabled + when switching to background sync + suspend fun execute(session: Session) { + val deviceId = session.sessionParams.deviceId ?: return + val isSilenced = !vectorPreferences.areNotificationEnabledForDevice() + val notificationSettingsContent = LocalNotificationSettingsContent( + isSilenced = isSilenced + ) + setNotificationSettingsAccountDataUseCase.execute(deviceId, notificationSettingsContent) + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt new file mode 100644 index 0000000000..51c24e500c --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCase.kt @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.devices.v2.notification + +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent +import javax.inject.Inject + +class DeleteNotificationSettingsAccountDataUseCase @Inject constructor( + private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase, +) { + + // TODO to be called when switching to push notifications method + suspend fun execute(deviceId: String) { + val emptyNotificationSettingsContent = LocalNotificationSettingsContent( + isSilenced = null + ) + setNotificationSettingsAccountDataUseCase.execute(deviceId, emptyNotificationSettingsContent) + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt new file mode 100644 index 0000000000..e4cadaa005 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/CreateNotificationSettingsAccountDataUseCaseTest.kt @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.devices.v2.notification + +import im.vector.app.test.fakes.FakeSession +import im.vector.app.test.fakes.FakeVectorPreferences +import io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.test.runTest +import org.junit.Test +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent + +class CreateNotificationSettingsAccountDataUseCaseTest { + + private val fakeVectorPreferences = FakeVectorPreferences() + private val fakeSetNotificationSettingsAccountDataUseCase = mockk() + + private val createNotificationSettingsAccountDataUseCase = CreateNotificationSettingsAccountDataUseCase( + vectorPreferences = fakeVectorPreferences.instance, + setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, + ) + + @Test + fun `given a device id when execute then content with the current notification preference is set for the account data`() = runTest { + // Given + val aDeviceId = "device-id" + val session = FakeSession() + session.givenSessionId(aDeviceId) + coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any()) } + val areNotificationsEnabled = true + fakeVectorPreferences.givenAreNotificationEnabled(areNotificationsEnabled) + val expectedContent = LocalNotificationSettingsContent( + isSilenced = !areNotificationsEnabled + ) + + // When + createNotificationSettingsAccountDataUseCase.execute(session) + + // Then + verify { fakeVectorPreferences.instance.areNotificationEnabledForDevice() } + coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aDeviceId, expectedContent) } + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt new file mode 100644 index 0000000000..038a1f436a --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/notification/DeleteNotificationSettingsAccountDataUseCaseTest.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.devices.v2.notification + +import io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.junit.Test +import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent + +class DeleteNotificationSettingsAccountDataUseCaseTest { + + private val fakeSetNotificationSettingsAccountDataUseCase = mockk() + + private val deleteNotificationSettingsAccountDataUseCase = DeleteNotificationSettingsAccountDataUseCase( + setNotificationSettingsAccountDataUseCase = fakeSetNotificationSettingsAccountDataUseCase, + ) + + @Test + fun `given a device id when execute then empty content is set for the account data`() = runTest { + // Given + val aDeviceId = "device-id" + coJustRun { fakeSetNotificationSettingsAccountDataUseCase.execute(any(), any()) } + val expectedContent = LocalNotificationSettingsContent( + isSilenced = null + ) + + // When + deleteNotificationSettingsAccountDataUseCase.execute(aDeviceId) + + // Then + coVerify { fakeSetNotificationSettingsAccountDataUseCase.execute(aDeviceId, expectedContent) } + } +}