Skip to content

Commit

Permalink
Async-await refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alfogrillo committed Feb 1, 2023
1 parent 8d734b9 commit 6850633
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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 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 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()
}
}
}
}
}

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
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()
}
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,113 +32,71 @@ final class NotificationSettingsViewModelTests: XCTestCase {
XCTAssertTrue(viewModel.viewState.selectionState.values.allSatisfy { $0 })
}

func testUpdateRule() throws {
func testUpdateRule() async {
viewModel = .init(notificationSettingsService: notificationService, ruleIds: .default)
notificationService.rules = [MockNotificationPushRule].default

viewModel.update(ruleID: .encrypted, isChecked: false)
await viewModel.update(ruleID: .encrypted, isChecked: false)
XCTAssertEqual(viewModel.viewState.selectionState.count, 4)
XCTAssertEqual(viewModel.viewState.selectionState[.encrypted], false)
}

func testUpdateOneToOneRuleAlsoUpdatesPollRules() {
let expectation = expectation(description: #function)
func testUpdateOneToOneRuleAlsoUpdatesPollRules() async {
setupWithPollRules()

viewModel.update(ruleID: .oneToOneRoom, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

XCTAssertEqual(self.viewModel.viewState.selectionState.count, 8)
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOneRoom], false)
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOnePollStart], false)
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOnePollEnd], false)

// unrelated poll rules stay the same
XCTAssertEqual(self.viewModel.viewState.selectionState[.allOtherMessages], true)
XCTAssertEqual(self.viewModel.viewState.selectionState[.pollStart], true)
XCTAssertEqual(self.viewModel.viewState.selectionState[.pollEnd], true)

expectation.fulfill()
}
await viewModel.update(ruleID: .oneToOneRoom, isChecked: false)

XCTAssertEqual(viewModel.viewState.selectionState.count, 8)
XCTAssertEqual(viewModel.viewState.selectionState[.oneToOneRoom], false)
XCTAssertEqual(viewModel.viewState.selectionState[.oneToOnePollStart], false)
XCTAssertEqual(viewModel.viewState.selectionState[.oneToOnePollEnd], false)

waitForExpectations(timeout: 1.0)
// unrelated poll rules stay the same
XCTAssertEqual(viewModel.viewState.selectionState[.allOtherMessages], true)
XCTAssertEqual(viewModel.viewState.selectionState[.pollStart], true)
XCTAssertEqual(viewModel.viewState.selectionState[.pollEnd], true)
}

func testUpdateMessageRuleAlsoUpdatesPollRules() {
let expectation = expectation(description: #function)
func testUpdateMessageRuleAlsoUpdatesPollRules() async {
setupWithPollRules()

viewModel.update(ruleID: .allOtherMessages, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

XCTAssertEqual(self.viewModel.viewState.selectionState.count, 8)
XCTAssertEqual(self.viewModel.viewState.selectionState[.allOtherMessages], false)
XCTAssertEqual(self.viewModel.viewState.selectionState[.pollStart], false)
XCTAssertEqual(self.viewModel.viewState.selectionState[.pollEnd], false)

// unrelated poll rules stay the same
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOneRoom], true)
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOnePollStart], true)
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOnePollEnd], true)

expectation.fulfill()
}
await viewModel.update(ruleID: .allOtherMessages, isChecked: false)
XCTAssertEqual(viewModel.viewState.selectionState.count, 8)
XCTAssertEqual(viewModel.viewState.selectionState[.allOtherMessages], false)
XCTAssertEqual(viewModel.viewState.selectionState[.pollStart], false)
XCTAssertEqual(viewModel.viewState.selectionState[.pollEnd], false)

waitForExpectations(timeout: 1.0)
// unrelated poll rules stay the same
XCTAssertEqual(viewModel.viewState.selectionState[.oneToOneRoom], true)
XCTAssertEqual(viewModel.viewState.selectionState[.oneToOnePollStart], true)
XCTAssertEqual(viewModel.viewState.selectionState[.oneToOnePollEnd], true)
}

func testMismatchingRulesAreHandled() {
let expectation = expectation(description: #function)
func testMismatchingRulesAreHandled() async {
setupWithPollRules()

viewModel.update(ruleID: .allOtherMessages, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

// simulating a "mismatch" on the poll started rule
self.viewModel.update(ruleID: .pollStart, isChecked: true)

XCTAssertEqual(self.viewModel.viewState.selectionState.count, 8)

// The other messages rule ui flag should match the loudest related poll rule
XCTAssertEqual(self.viewModel.viewState.selectionState[.allOtherMessages], true)

expectation.fulfill()
}
await viewModel.update(ruleID: .allOtherMessages, isChecked: false)

waitForExpectations(timeout: 1.0)
// simulating a "mismatch" on the poll started rule
await viewModel.update(ruleID: .pollStart, isChecked: true)

XCTAssertEqual(viewModel.viewState.selectionState.count, 8)

// The other messages rule ui flag should match the loudest related poll rule
XCTAssertEqual(viewModel.viewState.selectionState[.allOtherMessages], true)
}

func testMismatchingOneToOneRulesAreHandled() {
let expectation = expectation(description: #function)
func testMismatchingOneToOneRulesAreHandled() async {
setupWithPollRules()

viewModel.update(ruleID: .oneToOneRoom, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

// simulating a "mismatch" on the one to one poll started rule
self.viewModel.update(ruleID: .oneToOnePollStart, isChecked: true)

XCTAssertEqual(self.viewModel.viewState.selectionState.count, 8)

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

expectation.fulfill()
}
await viewModel.update(ruleID: .oneToOneRoom, isChecked: false)
// simulating a "mismatch" on the one to one poll started rule
await viewModel.update(ruleID: .oneToOnePollStart, isChecked: true)

XCTAssertEqual(viewModel.viewState.selectionState.count, 8)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ struct NotificationSettings<BottomSection: View>: View {
ForEach(viewModel.viewState.ruleIds) { ruleId in
let checked = viewModel.viewState.selectionState[ruleId] ?? false
FormPickerItem(title: ruleId.title, selected: checked) {
viewModel.update(ruleID: ruleId, isChecked: !checked)
Task {
await viewModel.update(ruleID: ruleId, isChecked: !checked)
}
}
}
}
Expand Down
Loading

0 comments on commit 6850633

Please sign in to comment.