Async-await refactor

This commit is contained in:
Alfonso Grillo
2023-02-01 10:14:40 +01:00
parent 2a764d2805
commit 47c6a34bb4
6 changed files with 127 additions and 172 deletions
@@ -44,7 +44,9 @@ class MXNotificationSettingsService: NotificationSettingsServiceType {
// Observe future updates to content rules
rulesUpdated
.compactMap { _ in self.session.notificationCenter.rules.global.content as? [MXPushRule] }
.compactMap { [weak self] _ in
self?.session.notificationCenter.rules.global.content as? [MXPushRule]
}
.assign(to: &$contentRules)
// Set initial value of rules
@@ -53,7 +55,9 @@ class MXNotificationSettingsService: NotificationSettingsServiceType {
}
// Observe future updates to rules
rulesUpdated
.compactMap { _ in self.session.notificationCenter.flatRules as? [MXPushRule] }
.compactMap { [weak self] _ in
self?.session.notificationCenter.flatRules as? [MXPushRule]
}
.assign(to: &$rules)
}
@@ -72,52 +76,50 @@ class MXNotificationSettingsService: NotificationSettingsServiceType {
func updatePushRuleActions(for ruleId: String,
enabled: Bool,
actions: NotificationActions?,
completion: ((Result<Void, Error>) -> Void)?) {
actions: NotificationActions?) async throws {
guard let rule = session.notificationCenter.rule(byId: ruleId) else {
completion?(.success)
return
}
guard let actions = actions else {
enableRule(rule: rule, enabled: enabled, completion: completion)
try await session.notificationCenter.enableRule(pushRule: rule, isEnabled: enabled)
return
}
// Updating the actions before enabling the rule allows the homeserver to triggers just one sync update
session.notificationCenter.updatePushRuleActions(ruleId,
try await session.notificationCenter.updatePushRuleActions(ruleId,
kind: rule.kind,
notify: actions.notify,
soundName: actions.sound,
highlight: actions.highlight) { [weak self] error in
switch error.result {
case .success:
self?.enableRule(rule: rule, enabled: enabled, completion: completion)
case .failure:
completion?(error.result)
highlight: actions.highlight)
try await session.notificationCenter.enableRule(pushRule: rule, isEnabled: enabled)
}
}
private extension MXNotificationCenter {
func enableRule(pushRule: MXPushRule, isEnabled: Bool) async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
enableRule(pushRule, isEnabled: isEnabled) { error in
if let error = error {
continuation.resume(with: .failure(error))
} else {
continuation.resume()
}
}
}
}
func updatePushRuleActions(ruleId: String, kind: __MXPushRuleKind, notify: Bool, soundName: String, highlight: Bool) async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
updatePushRuleActions(ruleId, kind: kind, notify: notify, soundName: soundName, highlight: highlight) { error in
if let error = error {
continuation.resume(with: .failure(error))
} else {
continuation.resume()
}
}
}
}
}
private extension MXNotificationSettingsService {
func enableRule(rule: MXPushRule, enabled: Bool, completion: ((Result<Void, Error>) -> Void)?) {
session.notificationCenter.enableRule(rule, isEnabled: enabled) { error in
completion?(error.result)
}
}
}
private extension Result where Success == Void {
static var success: Self {
.success(())
}
}
private extension Optional where Wrapped == Error {
var result: Result<Void, Error> {
map { .failure($0) } ?? .success
}
}
@@ -44,15 +44,11 @@ class MockNotificationSettingsService: NotificationSettingsServiceType, Observab
keywords.remove(keyword)
}
func updatePushRuleActions(for ruleId: String, enabled: Bool, actions: NotificationActions?, completion: ((Result<Void, Error>) -> Void)?) {
func updatePushRuleActions(for ruleId: String, enabled: Bool, actions: NotificationActions?) async throws {
guard let ruleIndex = rules.firstIndex(where: { $0.ruleId == ruleId }) else {
completion?(.success(()))
return
}
rules[ruleIndex] = MockNotificationPushRule(ruleId: ruleId,
enabled: enabled,
actions: actions)
completion?(.success(()))
rules[ruleIndex] = MockNotificationPushRule(ruleId: ruleId, enabled: enabled, actions: actions)
}
}
@@ -40,16 +40,5 @@ protocol NotificationSettingsServiceType {
/// - ruleId: The id of the rule.
/// - enabled: Whether the rule should be enabled or disabled.
/// - actions: The actions to update with.
/// - completion: The completion of the operation.
func updatePushRuleActions(for ruleId: String, enabled: Bool, actions: NotificationActions?, completion: ((Result<Void, Error>) -> Void)?)
}
extension NotificationSettingsServiceType {
func updatePushRuleActions(for ruleId: String, enabled: Bool, actions: NotificationActions?) async throws {
try await withCheckedThrowingContinuation { continuation in
updatePushRuleActions(for: ruleId, enabled: enabled, actions: actions) { result in
continuation.resume(with: result)
}
}
}
func updatePushRuleActions(for ruleId: String, enabled: Bool, actions: NotificationActions?) async throws
}