diff --git a/Riot/Categories/Publisher+Riot.swift b/Riot/Categories/Publisher+Riot.swift index 98bb522b3..7c70404c6 100644 --- a/Riot/Categories/Publisher+Riot.swift +++ b/Riot/Categories/Publisher+Riot.swift @@ -33,4 +33,10 @@ extension Publisher { Just($0).delay(for: .seconds(spacingDelay), scheduler: scheduler) } } + + func eraseOutput() -> AnyPublisher { + self + .map { _ in () } + .eraseToAnyPublisher() + } } diff --git a/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift b/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift index 220b16100..8b45d3035 100644 --- a/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift +++ b/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift @@ -44,12 +44,12 @@ private extension PushRulesUpdater { let relatedRules = rule.syncedRules(in: rules) for relatedRule in relatedRules { - guard relatedRule == rule else { - MXLog.debug("*** mismatch not found. rule: \(relatedRule.ruleId)") + guard MXPushRule.haveSameContent(relatedRule, rule) == false else { + MXLog.debug("*** mismatch -> rule: \(relatedRule.ruleId)") continue } - MXLog.debug("*** mismatch found. rule: \(relatedRule.ruleId)") + MXLog.debug("*** OK -> rule: \(relatedRule.ruleId)") } } } @@ -67,4 +67,28 @@ private extension MXPushRule { return ruleId.syncedRules.contains(someRuleId) } } + + static func haveSameContent(_ firstRule: MXPushRule, _ secondRule: MXPushRule) -> Bool { + guard + firstRule.enabled == secondRule.enabled, + let firstActions = firstRule.mxActions, + let secondActions = secondRule.mxActions, + firstActions.count == secondActions.count + else { + return false + } + + return firstActions.indices.allSatisfy { index in + let action1 = firstActions[index] + let action2 = secondActions[index] + #warning("compare @property (nonatomic) NSDictionary *parameters") + return action1.actionType == action2.actionType + } + } +} + +private extension MXPushRule { + var mxActions: [MXPushRuleAction]? { + actions as? [MXPushRuleAction] + } } diff --git a/Riot/Modules/Application/AppCoordinator.swift b/Riot/Modules/Application/AppCoordinator.swift index 0b44632e5..389467ded 100755 --- a/Riot/Modules/Application/AppCoordinator.swift +++ b/Riot/Modules/Application/AppCoordinator.swift @@ -14,6 +14,7 @@ limitations under the License. */ +import Combine import Foundation import Intents import MatrixSDK @@ -263,14 +264,15 @@ final class AppCoordinator: NSObject, AppCoordinatorType { } private func setupPushRuleSync() { - let needsRulesCheck = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification) - .map { _ in () } - .eraseToAnyPublisher() + let firstSyncEndend = NotificationCenter.default.publisher(for: .mxSessionDidSync) + .first() + .eraseOutput() + + let rulesDidChange = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: kMXNotificationCenterDidUpdateRules)).eraseOutput() - #warning("Doesn't include the initial state of rules") - let rulesUpdated = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: kMXNotificationCenterDidUpdateRules)) - .compactMap { notification -> [MXPushRule]? in - guard let center = notification.object as? MXNotificationCenter else { + let rules = Publishers.Merge(rulesDidChange, firstSyncEndend) + .compactMap { [weak self] _ -> [MXPushRule]? in + guard let center = self?.mainMatrixSession?.notificationCenter else { return nil } @@ -278,7 +280,10 @@ final class AppCoordinator: NSObject, AppCoordinatorType { } .eraseToAnyPublisher() - pushRulesUpdater = .init(checkSignal: needsRulesCheck, rulesProvider: rulesUpdated) + let applicationDidBecomeActive = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification).eraseOutput() + let needsRulesCheck = Publishers.Merge(firstSyncEndend, applicationDidBecomeActive).eraseOutput() + + pushRulesUpdater = .init(checkSignal: needsRulesCheck, rulesProvider: rules) } }