mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 03:48:12 +03:00
Listening changes on notifications enabled preference to update the UI in settings
This commit is contained in:
parent
9fbfe82044
commit
c12af5a800
3 changed files with 62 additions and 7 deletions
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue