diff --git a/Config/AppConfiguration.swift b/Config/AppConfiguration.swift index 70b1d78d5..3e70316b4 100644 --- a/Config/AppConfiguration.swift +++ b/Config/AppConfiguration.swift @@ -14,6 +14,7 @@ // limitations under the License. // +import Combine import Foundation /// AppConfiguration is CommonConfiguration plus configurations dedicated to the app @@ -54,12 +55,17 @@ class AppConfiguration: CommonConfiguration { // MARK: - Per matrix session settings + private var pushRulesUpdater: PushRulesUpdater? + override func setupSettings(for matrixSession: MXSession) { super.setupSettings(for: matrixSession) setupWidgetReadReceipts(for: matrixSession) + setupPushRuleSync(for: matrixSession) } - - private func setupWidgetReadReceipts(for matrixSession: MXSession) { +} + +private extension AppConfiguration { + func setupWidgetReadReceipts(for matrixSession: MXSession) { var acknowledgableEventTypes = matrixSession.acknowledgableEventTypes ?? [] acknowledgableEventTypes.append(kWidgetMatrixEventTypeString) acknowledgableEventTypes.append(kWidgetModularEventTypeString) @@ -67,4 +73,28 @@ class AppConfiguration: CommonConfiguration { matrixSession.acknowledgableEventTypes = acknowledgableEventTypes } + func setupPushRuleSync(for matrixSession: MXSession) { + let firstSyncEnded = NotificationCenter.default.publisher(for: .mxSessionDidSync) + .first() + .eraseOutput() + + let rulesDidChange = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: kMXNotificationCenterDidUpdateRules)).eraseOutput() + + let rules = Publishers.Merge(rulesDidChange, firstSyncEnded) + .compactMap { _ -> [MXPushRule]? in + guard let center = matrixSession.notificationCenter else { + return nil + } + + return center.flatRules as? [MXPushRule] + } + .eraseToAnyPublisher() + + let applicationDidBecomeActive = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification).eraseOutput() + let needsRulesCheck = Publishers.Merge(firstSyncEnded, applicationDidBecomeActive).eraseOutput() + + pushRulesUpdater = .init(notificationSettingsService: MXNotificationSettingsService(session: matrixSession), + rules: rules, + needsCheck: needsRulesCheck) + } } diff --git a/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift b/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift index 8b45d3035..b479febb1 100644 --- a/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift +++ b/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift @@ -19,13 +19,16 @@ import Combine final class PushRulesUpdater { private var cancellables: Set = .init() private var rules: [MXPushRule] = [] + private let notificationSettingsService: NotificationSettingsServiceType - init(checkSignal: AnyPublisher, rulesProvider: AnyPublisher<[MXPushRule], Never>) { - rulesProvider + init(notificationSettingsService: NotificationSettingsServiceType, rules: AnyPublisher<[MXPushRule], Never>, needsCheck: AnyPublisher) { + self.notificationSettingsService = notificationSettingsService + + rules .weakAssign(to: \.rules, on: self) .store(in: &cancellables) - checkSignal + needsCheck .sink { [weak self] _ in self?.updateRulesIfNeeded() } @@ -45,11 +48,20 @@ private extension PushRulesUpdater { for relatedRule in relatedRules { guard MXPushRule.haveSameContent(relatedRule, rule) == false else { - MXLog.debug("*** mismatch -> rule: \(relatedRule.ruleId)") + MXLog.debug("*** OK -> rule: \(relatedRule.ruleId)") continue } - MXLog.debug("*** OK -> rule: \(relatedRule.ruleId)") + let index = NotificationIndex.index(when: rule.enabled) + #warning("Fix me") + let standardActions = NotificationPushRuleId(rawValue: rule.ruleId)!.standardActions(for: index) + + MXLog.debug("*** mismatch -> rule: \(relatedRule.ruleId)") + Task { + try? await notificationSettingsService.updatePushRuleActions(for: relatedRule.ruleId, + enabled: rule.enabled, + actions: standardActions.actions) + } } } } diff --git a/Riot/Modules/Application/AppCoordinator.swift b/Riot/Modules/Application/AppCoordinator.swift index 389467ded..802f49e16 100755 --- a/Riot/Modules/Application/AppCoordinator.swift +++ b/Riot/Modules/Application/AppCoordinator.swift @@ -54,7 +54,6 @@ final class AppCoordinator: NSObject, AppCoordinatorType { fileprivate weak var sideMenuCoordinator: SideMenuCoordinatorType? private let userSessionsService: UserSessionsService - private var pushRulesUpdater: PushRulesUpdater? /// Main user Matrix session private var mainMatrixSession: MXSession? { @@ -86,7 +85,6 @@ final class AppCoordinator: NSObject, AppCoordinatorType { setupLogger() setupTheme() excludeAllItemsFromBackup() - setupPushRuleSync() // Setup navigation router store _ = NavigationRouterStore.shared @@ -262,29 +260,6 @@ final class AppCoordinator: NSObject, AppCoordinatorType { // Reload split view with selected space id self.splitViewCoordinator?.start(with: spaceId) } - - private func setupPushRuleSync() { - let firstSyncEndend = NotificationCenter.default.publisher(for: .mxSessionDidSync) - .first() - .eraseOutput() - - let rulesDidChange = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: kMXNotificationCenterDidUpdateRules)).eraseOutput() - - let rules = Publishers.Merge(rulesDidChange, firstSyncEndend) - .compactMap { [weak self] _ -> [MXPushRule]? in - guard let center = self?.mainMatrixSession?.notificationCenter else { - return nil - } - - return center.flatRules as? [MXPushRule] - } - .eraseToAnyPublisher() - - let applicationDidBecomeActive = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification).eraseOutput() - let needsRulesCheck = Publishers.Merge(firstSyncEndend, applicationDidBecomeActive).eraseOutput() - - pushRulesUpdater = .init(checkSignal: needsRulesCheck, rulesProvider: rules) - } } // MARK: - LegacyAppDelegateDelegate diff --git a/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift b/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift index 6ccd54c1a..84f25c106 100644 --- a/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift +++ b/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift @@ -38,14 +38,14 @@ class MXNotificationSettingsService: NotificationSettingsServiceType { let rulesUpdated = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: kMXNotificationCenterDidUpdateRules)) // Set initial value of the content rules - if let contentRules = session.notificationCenter.rules.global.content as? [MXPushRule] { + if let contentRules = session.notificationCenter.rules?.global.content as? [MXPushRule] { self.contentRules = contentRules } // Observe future updates to content rules rulesUpdated .compactMap { [weak self] _ in - self?.session.notificationCenter.rules.global.content as? [MXPushRule] + self?.session.notificationCenter.rules?.global.content as? [MXPushRule] } .assign(to: &$contentRules)