Listening changes on notifications enabled preference to update the UI in settings

This commit is contained in:
Maxime NATUREL 2022-12-02 14:14:51 +01:00
parent 9fbfe82044
commit c12af5a800
3 changed files with 62 additions and 7 deletions

View File

@ -16,6 +16,8 @@
package im.vector.app.features.settings.notifications
import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
@ -50,6 +52,31 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(
companion object : MavericksViewModelFactory<VectorSettingsNotificationPreferenceViewModel, VectorDummyViewState> by hiltMavericksViewModelFactory()
@VisibleForTesting
val notificationsPreferenceListener: SharedPreferences.OnSharedPreferenceChangeListener =
SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY) {
if (vectorPreferences.areNotificationEnabledForDevice()) {
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceEnabled)
} else {
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceDisabled)
}
}
}
init {
observeNotificationsEnabledPreference()
}
private fun observeNotificationsEnabledPreference() {
vectorPreferences.subscribeToChanges(notificationsPreferenceListener)
}
override fun onCleared() {
vectorPreferences.unsubscribeToChanges(notificationsPreferenceListener)
super.onCleared()
}
override fun handle(action: VectorSettingsNotificationPreferenceViewAction) {
when (action) {
VectorSettingsNotificationPreferenceViewAction.DisableNotificationsForDevice -> handleDisableNotificationsForDevice()

View File

@ -21,6 +21,7 @@ import im.vector.app.core.platform.VectorDummyViewState
import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.VectorPreferences.Companion.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY
import im.vector.app.test.fakes.FakePushersManager
import im.vector.app.test.fakes.FakeVectorPreferences
import im.vector.app.test.test
@ -60,6 +61,40 @@ class VectorSettingsNotificationPreferenceViewModelTest {
toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
)
@Test
fun `given view model init when notifications are enabled in preferences then view event is posted`() {
// Given
fakeVectorPreferences.givenAreNotificationsEnabledForDevice(true)
val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceEnabled
val viewModel = createViewModel()
// When
val viewModelTest = viewModel.test()
viewModel.notificationsPreferenceListener.onSharedPreferenceChanged(mockk(), SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)
// Then
viewModelTest
.assertEvent { event -> event == expectedEvent }
.finish()
}
@Test
fun `given view model init when notifications are disabled in preferences then view event is posted`() {
// Given
fakeVectorPreferences.givenAreNotificationsEnabledForDevice(false)
val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceDisabled
val viewModel = createViewModel()
// When
val viewModelTest = viewModel.test()
viewModel.notificationsPreferenceListener.onSharedPreferenceChanged(mockk(), SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)
// Then
viewModelTest
.assertEvent { event -> event == expectedEvent }
.finish()
}
@Test
fun `given DisableNotificationsForDevice action when handling action then disable use case is called`() {
// Given

View File

@ -16,7 +16,6 @@
package im.vector.app.test.fakes
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences
import io.mockk.every
@ -78,10 +77,4 @@ class FakeVectorPreferences {
fun givenIsBackgroundSyncEnabled(isEnabled: Boolean) {
every { instance.isBackgroundSyncEnabled() } returns isEnabled
}
fun givenChangeOnPreference(key: String) {
every { instance.subscribeToChanges(any()) } answers {
firstArg<OnSharedPreferenceChangeListener>().onSharedPreferenceChanged(mockk(), key)
}
}
}