Add error handling for push sync

This commit is contained in:
Alfonso Grillo
2023-01-31 12:24:55 +01:00
parent 1d94c4776d
commit 2b70ef843e
5 changed files with 47 additions and 6 deletions
@@ -147,6 +147,10 @@ final class NotificationSettingsViewModel: NotificationSettingsViewModelType, Ob
keywordsOrdered = keywordsOrdered.filter { $0 != keyword }
notificationSettingsService.remove(keyword: keyword)
}
func isRuleOutOfSync(_ ruleId: NotificationPushRuleId) -> Bool {
viewState.outOfSyncRules.contains(ruleId) && viewState.saving == false
}
}
// MARK: - Private
@@ -206,11 +210,12 @@ private extension NotificationSettingsViewModel {
@MainActor
func completeUpdate() {
#warning("Handle error here in the next ticket")
viewState.saving = false
}
func rulesUpdated(newRules: [NotificationPushRuleType]) {
var outOfSyncRules: Set<NotificationPushRuleId> = .init()
for rule in newRules {
guard
let ruleId = NotificationPushRuleId(rawValue: rule.ruleId),
@@ -219,8 +224,15 @@ private extension NotificationSettingsViewModel {
continue
}
viewState.selectionState[ruleId] = isChecked(rule: rule, syncedRules: ruleId.syncedRules(in: newRules))
let relatedSyncedRules = ruleId.syncedRules(in: newRules)
viewState.selectionState[ruleId] = isChecked(rule: rule, syncedRules: relatedSyncedRules)
if isOutOfSync(rule: rule, syncedRules: relatedSyncedRules) {
outOfSyncRules.insert(ruleId)
}
}
viewState.outOfSyncRules = outOfSyncRules
}
func keywordRuleUpdated(anyEnabled: Bool) {
@@ -266,6 +278,20 @@ private extension NotificationSettingsViewModel {
return defaultIsChecked(rule: rule)
}
}
func isOutOfSync(rule: NotificationPushRuleType, syncedRules: [NotificationPushRuleType]) -> Bool {
guard let ruleId = NotificationPushRuleId(rawValue: rule.ruleId) else {
return false
}
switch ruleId {
case .oneToOneRoom, .allOtherMessages:
let ruleIsChecked = defaultIsChecked(rule: rule)
return syncedRules.contains(where: { defaultIsChecked(rule: $0) != ruleIsChecked })
default:
return false
}
}
}
private extension NotificationPushRuleId {
@@ -22,5 +22,6 @@ struct NotificationSettingsViewState {
var saving: Bool
var ruleIds: [NotificationPushRuleId]
var selectionState: [NotificationPushRuleId: Bool]
var outOfSyncRules: Set<NotificationPushRuleId> = .init()
var keywords = [String]()
}