Adding use cases to create and delete notifications settings in account data

This commit is contained in:
Maxime NATUREL 2022-11-17 10:34:11 +01:00
parent b163b42d3d
commit 14b21dc039
5 changed files with 181 additions and 1 deletions

View File

@ -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
)

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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<SetNotificationSettingsAccountDataUseCase>()
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) }
}
}

View File

@ -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<SetNotificationSettingsAccountDataUseCase>()
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) }
}
}