Skip to content

Commit

Permalink
DispatchGroup to TaskGroup refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alfogrillo committed Feb 2, 2023
1 parent a2b1177 commit 6c25dca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
48 changes: 23 additions & 25 deletions Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,33 @@ final class PushRulesUpdater {

private extension PushRulesUpdater {
func syncRulesIfNeeded() {
let dispatchGroup: DispatchGroup = .init()

for rule in rules {
guard let ruleId = rule.pushRuleId else {
continue
Task {
await withTaskGroup(of: Void.self) { [rules, notificationSettingsService] group in
for rule in rules {
guard let ruleId = rule.pushRuleId else {
continue
}

let relatedRules = ruleId.syncedRules(in: rules)

for relatedRule in relatedRules {
guard rule.hasSameContentOf(relatedRule) == false else {
continue
}

group.addTask {
try? await notificationSettingsService.updatePushRuleActions(for: relatedRule.ruleId,
enabled: rule.enabled,
actions: rule.ruleActions)
}
}
}
}

let relatedRules = ruleId.syncedRules(in: rules)

for relatedRule in relatedRules {
guard rule.hasSameContentOf(relatedRule) == false else {
continue
}

dispatchGroup.enter()
Task {
try? await sync(relatedRuleId: relatedRule.ruleId, with: rule)
dispatchGroup.leave()
}
await MainActor.run { [weak self] in
self?.didCompleteUpdateSubject.send(())
}
}

dispatchGroup.notify(queue: .main) { [weak self] in
self?.didCompleteUpdateSubject.send(())
}
}

func sync(relatedRuleId: String, with rule: NotificationPushRuleType) async throws {
try await notificationSettingsService.updatePushRuleActions(for: relatedRuleId, enabled: rule.enabled, actions: rule.ruleActions)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ class MXNotificationSettingsService: NotificationSettingsServiceType {

// Updating the actions before enabling the rule allows the homeserver to triggers just one sync update
try await session.notificationCenter.updatePushRuleActions(ruleId,
kind: rule.kind,
notify: actions.notify,
soundName: actions.sound,
highlight: actions.highlight)
kind: rule.kind,
notify: actions.notify,
soundName: actions.sound,
highlight: actions.highlight)

try await session.notificationCenter.enableRule(pushRule: rule, isEnabled: enabled)
}
Expand Down

0 comments on commit 6c25dca

Please sign in to comment.