Skip to content

Commit

Permalink
Merge pull request #7324 from vector-im/alfogrillo/sync_polls_rulles_…
Browse files Browse the repository at this point in the history
…error_handling

Sync polls rules error handling (PSG-1126)
  • Loading branch information
Alfonso Grillo authored Feb 1, 2023
2 parents 223224c + 4f5dfe3 commit c6ea789
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions Riot/Assets/en.lproj/Vector.strings
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ Tap the + to start adding people.";
"settings_your_keywords" = "Your Keywords";
"settings_new_keyword" = "Add new Keyword";
"settings_mentions_and_keywords_encryption_notice" = "You won’t get notifications for mentions & keywords in encrypted rooms on mobile.";
"settings_push_rules_error" = "An error occurred when updating your notification preferences. Please try to toggle your option again.";

"settings_enable_callkit" = "Integrated calling";
"settings_callkit_info" = "Receive incoming calls on your lock screen. See your %@ calls in the system's call history. If iCloud is enabled, this call history will be shared with Apple.";
Expand Down
4 changes: 4 additions & 0 deletions Riot/Generated/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7767,6 +7767,10 @@ public class VectorL10n: NSObject {
public static var settingsProfilePicture: String {
return VectorL10n.tr("Vector", "settings_profile_picture")
}
/// An error occurred when updating your notification preferences. Please try to toggle your option again.
public static var settingsPushRulesError: String {
return VectorL10n.tr("Vector", "settings_push_rules_error")
}
/// Are you sure you want to remove the email address %@?
public static func settingsRemoveEmailPromptMsg(_ p1: String) -> String {
return VectorL10n.tr("Vector", "settings_remove_email_prompt_msg", p1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ final class NotificationSettingsViewModelTests: XCTestCase {

// The one to one room rule ui flag should match the loudest related poll rule
XCTAssertEqual(viewModel.viewState.selectionState[.oneToOneRoom], true)

// the oneToOneRoom rule should be flagged as "out of sync"
XCTAssertTrue(viewModel.isRuleOutOfSync(.oneToOneRoom))
XCTAssertFalse(viewModel.isRuleOutOfSync(.allOtherMessages))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import SwiftUI
/// Also renders an optional bottom section.
/// Used in the case of keywords, for the keyword chips and input.
struct NotificationSettings<BottomSection: View>: View {
@Environment(\.theme) var theme: ThemeSwiftUI
@ObservedObject var viewModel: NotificationSettingsViewModel

var bottomSection: BottomSection?
Expand All @@ -31,10 +32,20 @@ struct NotificationSettings<BottomSection: View>: View {
header: FormSectionHeader(text: VectorL10n.settingsNotifyMeFor)
) {
ForEach(viewModel.viewState.ruleIds) { ruleId in
let checked = viewModel.viewState.selectionState[ruleId] ?? false
FormPickerItem(title: ruleId.title, selected: checked) {
Task {
await viewModel.update(ruleID: ruleId, isChecked: !checked)
VStack(alignment: .leading, spacing: 4) {
let checked = viewModel.viewState.selectionState[ruleId] ?? false
FormPickerItem(title: ruleId.title, selected: checked) {
Task {
await viewModel.update(ruleID: ruleId, isChecked: !checked)
}
}

if viewModel.isRuleOutOfSync(ruleId) {
Text(VectorL10n.settingsPushRulesError)
.font(theme.fonts.caption1)
.foregroundColor(theme.colors.alert)
.padding(.horizontal)
.padding(.bottom, 16)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ struct NotificationSettingsViewState {
var saving: Bool
var ruleIds: [NotificationPushRuleId]
var selectionState: [NotificationPushRuleId: Bool]
var outOfSyncRules: Set<NotificationPushRuleId> = .init()
var keywords = [String]()
}
1 change: 1 addition & 0 deletions changelog.d/pr-7324.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Polls: add error handling when syncing push rules with the ones of normal messages.

0 comments on commit c6ea789

Please sign in to comment.