diff --git a/Config/AppConfiguration.swift b/Config/AppConfiguration.swift index 64d16eafc..70b1d78d5 100644 --- a/Config/AppConfiguration.swift +++ b/Config/AppConfiguration.swift @@ -14,7 +14,6 @@ // limitations under the License. // -import Combine import Foundation /// AppConfiguration is CommonConfiguration plus configurations dedicated to the app @@ -55,17 +54,12 @@ 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 extension AppConfiguration { - func setupWidgetReadReceipts(for matrixSession: MXSession) { + + private func setupWidgetReadReceipts(for matrixSession: MXSession) { var acknowledgableEventTypes = matrixSession.acknowledgableEventTypes ?? [] acknowledgableEventTypes.append(kWidgetMatrixEventTypeString) acknowledgableEventTypes.append(kWidgetModularEventTypeString) @@ -73,16 +67,4 @@ private extension AppConfiguration { matrixSession.acknowledgableEventTypes = acknowledgableEventTypes } - func setupPushRuleSync(for matrixSession: MXSession) { - let sessionIsReady = NotificationCenter.default.publisher(for: .mxSessionStateDidChange) - .first { _ in - matrixSession.state >= .running - } - .eraseOutput() - - let applicationDidBecomeActive = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification).eraseOutput() - let needsRulesCheck = Publishers.CombineLatest(sessionIsReady, applicationDidBecomeActive).eraseOutput() - - pushRulesUpdater = .init(notificationSettingsService: MXNotificationSettingsService(session: matrixSession), needsCheck: needsRulesCheck) - } } diff --git a/Riot/Modules/Application/AppCoordinator.swift b/Riot/Modules/Application/AppCoordinator.swift index 802f49e16..6c3e55275 100755 --- a/Riot/Modules/Application/AppCoordinator.swift +++ b/Riot/Modules/Application/AppCoordinator.swift @@ -61,6 +61,8 @@ final class AppCoordinator: NSObject, AppCoordinatorType { } private var currentSpaceId: String? + private var cancellables: Set = .init() + private var pushRulesUpdater: PushRulesUpdater? // MARK: Public @@ -85,6 +87,7 @@ final class AppCoordinator: NSObject, AppCoordinatorType { setupLogger() setupTheme() excludeAllItemsFromBackup() + setupPushRulesSync() // Setup navigation router store _ = NavigationRouterStore.shared @@ -260,6 +263,37 @@ final class AppCoordinator: NSObject, AppCoordinatorType { // Reload split view with selected space id self.splitViewCoordinator?.start(with: spaceId) } + + private func setupPushRulesSync() { + let sessionReady = NotificationCenter.default.publisher(for: .mxSessionStateDidChange) + .compactMap { $0.object as? MXSession } + .filter { $0.state == .running } + .removeDuplicates { session1, session2 in + session1 == session2 + } + + sessionReady + .print("*** ready") + .sink { [weak self] session in + let applicationDidBecomeActive = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification).eraseOutput() + let needsCheckPublisher = applicationDidBecomeActive.merge(with: Just(())).eraseToAnyPublisher() + + self?.pushRulesUpdater = .init(notificationSettingsService: MXNotificationSettingsService(session: session), needsCheck: needsCheckPublisher) + } + .store(in: &cancellables) + + + let sessionClosed = NotificationCenter.default.publisher(for: .mxSessionStateDidChange) + .compactMap { $0.object as? MXSession } + .filter { $0.state == .closed } + + sessionClosed + .print("*** closed") + .sink { [weak self] _ in + self?.pushRulesUpdater = nil + } + .store(in: &cancellables) + } } // MARK: - LegacyAppDelegateDelegate