diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationFragment.kt index 8a2099ecad..b7264f6cb3 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationFragment.kt @@ -36,6 +36,7 @@ abstract class VectorSettingsPushRuleNotificationFragment : override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) observeViewEvents() + viewModel.onEach(VectorSettingsPushRuleNotificationViewState::allRules) { refreshPreferences() } viewModel.onEach(VectorSettingsPushRuleNotificationViewState::isLoading) { updateLoadingView(it) } viewModel.onEach(VectorSettingsPushRuleNotificationViewState::rulesOnError) { refreshErrors(it) } } @@ -55,18 +56,11 @@ abstract class VectorSettingsPushRuleNotificationFragment : } override fun bindPref() { - for (preferenceKey in prefKeyToPushRuleId.keys) { - val preference = findPreference(preferenceKey)!! - preference.isIconSpaceReserved = false - val ruleAndKind = prefKeyToPushRuleId[preferenceKey]?.let { viewModel.getPushRuleAndKind(it) } - if (ruleAndKind == null) { - // The rule is not defined, hide the preference - preference.isVisible = false - } else { - preference.isVisible = true - updatePreference(ruleAndKind.pushRule.ruleId, viewModel.isPushRuleChecked(ruleAndKind.pushRule.ruleId)) - preference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> - viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(ruleAndKind, newValue as Boolean)) + prefKeyToPushRuleId.forEach { (preferenceKey, ruleId) -> + findPreference(preferenceKey)?.apply { + isIconSpaceReserved = false + onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> + viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(ruleId, newValue as Boolean)) false } } @@ -81,16 +75,21 @@ abstract class VectorSettingsPushRuleNotificationFragment : } } + private fun refreshPreferences() { + prefKeyToPushRuleId.values.forEach { ruleId -> updatePreference(ruleId, viewModel.isPushRuleChecked(ruleId)) } + } + private fun refreshErrors(rulesWithError: Set) { if (withState(viewModel, VectorSettingsPushRuleNotificationViewState::isLoading)) return prefKeyToPushRuleId.forEach { (preferenceKey, ruleId) -> - val preference = findPreference(preferenceKey)!! - if (ruleId in rulesWithError) { - preference.summaryTextColor = context?.let { ThemeUtils.getColor(it, R.attr.colorError) } - preference.setSummary(R.string.settings_notification_error_on_update) - } else { - preference.summaryTextColor = null - preference.summary = null + findPreference(preferenceKey)?.apply { + if (ruleId in rulesWithError) { + summaryTextColor = ThemeUtils.getColor(context, R.attr.colorError) + setSummary(R.string.settings_notification_error_on_update) + } else { + summaryTextColor = null + summary = null + } } } } @@ -102,6 +101,8 @@ abstract class VectorSettingsPushRuleNotificationFragment : private fun updatePreference(ruleId: String, checked: Boolean) { val preferenceKey = prefKeyToPushRuleId.entries.find { it.value == ruleId }?.key ?: return val preference = findPreference(preferenceKey) ?: return + val ruleIds = withState(viewModel) { state -> state.allRules.map { it.ruleId } } + preference.isVisible = ruleId in ruleIds preference.isChecked = checked } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewAction.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewAction.kt index 61bf7c5b15..94b7666122 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewAction.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewAction.kt @@ -17,8 +17,7 @@ package im.vector.app.features.settings.notifications import im.vector.app.core.platform.VectorViewModelAction -import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind sealed interface VectorSettingsPushRuleNotificationViewAction : VectorViewModelAction { - data class UpdatePushRule(val pushRuleAndKind: PushRuleAndKind, val checked: Boolean) : VectorSettingsPushRuleNotificationViewAction + data class UpdatePushRule(val ruleId: String, val checked: Boolean) : VectorSettingsPushRuleNotificationViewAction } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt index eee199d550..31f7f9c531 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt @@ -20,7 +20,6 @@ import com.airbnb.mvrx.MavericksViewModelFactory import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject -import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.platform.VectorViewModel @@ -30,6 +29,7 @@ import im.vector.app.features.settings.notifications.usecase.GetPushRulesOnInval import kotlinx.coroutines.launch import org.matrix.android.sdk.api.failure.Failure.ServerError import org.matrix.android.sdk.api.failure.MatrixError +import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes import org.matrix.android.sdk.api.session.pushrules.Action import org.matrix.android.sdk.api.session.pushrules.RuleIds @@ -43,7 +43,7 @@ private typealias ViewState = VectorSettingsPushRuleNotificationViewState class VectorSettingsPushRuleNotificationViewModel @AssistedInject constructor( @Assisted initialState: ViewState, - private val activeSessionHolder: ActiveSessionHolder, + private val session: Session, private val getPushRulesOnInvalidStateUseCase: GetPushRulesOnInvalidStateUseCase, ) : VectorViewModel by hiltMavericksViewModelFactory() init { - val session = activeSessionHolder.getSafeActiveSession() - session?.flow() - ?.liveUserAccountData(UserAccountDataTypes.TYPE_PUSH_RULES) - ?.unwrap() - ?.setOnEach { + session.flow() + .liveUserAccountData(UserAccountDataTypes.TYPE_PUSH_RULES) + .unwrap() + .setOnEach { + val allRules = session.pushRuleService().getPushRules().getAllRules() val rulesOnError = getPushRulesOnInvalidStateUseCase.execute(session).map { it.ruleId }.toSet() - copy(rulesOnError = rulesOnError) + copy( + allRules = allRules, + rulesOnError = rulesOnError + ) } } override fun handle(action: VectorSettingsPushRuleNotificationViewAction) { when (action) { - is VectorSettingsPushRuleNotificationViewAction.UpdatePushRule -> handleUpdatePushRule(action.pushRuleAndKind, action.checked) + is VectorSettingsPushRuleNotificationViewAction.UpdatePushRule -> handleUpdatePushRule(action.ruleId, action.checked) } } fun getPushRuleAndKind(ruleId: String): PushRuleAndKind? { - return activeSessionHolder.getSafeActiveSession()?.pushRuleService()?.getPushRules()?.findDefaultRule(ruleId) + return session.pushRuleService().getPushRules().findDefaultRule(ruleId) } fun isPushRuleChecked(ruleId: String): Boolean { @@ -82,9 +85,8 @@ class VectorSettingsPushRuleNotificationViewModel @AssistedInject constructor( return rulesGroup.mapNotNull { getPushRuleAndKind(it) }.any { it.pushRule.notificationIndex != NotificationIndex.OFF } } - private fun handleUpdatePushRule(pushRuleAndKind: PushRuleAndKind, checked: Boolean) { - val ruleId = pushRuleAndKind.pushRule.ruleId - val kind = pushRuleAndKind.kind + private fun handleUpdatePushRule(ruleId: String, checked: Boolean) { + val kind = getPushRuleAndKind(ruleId)?.kind ?: return val newIndex = if (checked) NotificationIndex.NOISY else NotificationIndex.OFF val standardAction = getStandardAction(ruleId, newIndex) ?: return val enabled = standardAction != StandardActions.Disabled @@ -129,7 +131,7 @@ class VectorSettingsPushRuleNotificationViewModel @AssistedInject constructor( } private suspend fun updatePushRule(kind: RuleKind, ruleId: String, enable: Boolean, newActions: List?) { - activeSessionHolder.getSafeActiveSession()?.pushRuleService()?.updatePushRuleActions( + session.pushRuleService().updatePushRuleActions( kind = kind, ruleId = ruleId, enable = enable, diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewState.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewState.kt index 392a8f3bb5..091c183d49 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewState.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewState.kt @@ -17,8 +17,10 @@ package im.vector.app.features.settings.notifications import com.airbnb.mvrx.MavericksState +import org.matrix.android.sdk.api.session.pushrules.rest.PushRule data class VectorSettingsPushRuleNotificationViewState( val isLoading: Boolean = false, - val rulesOnError: Set = emptySet() + val allRules: List = emptyList(), + val rulesOnError: Set = emptySet(), ) : MavericksState