From b79ed28b7d187ad6987b242f1cfa1fc0eb45bfee Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 8 Aug 2023 15:09:05 +0300 Subject: [PATCH] #1257 - Prevent the syncing indicator from showing over the offline one --- .../Sources/Application/AppCoordinator.swift | 11 +++++++---- ElementX/Sources/Other/NetworkMonitor.swift | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index edab801401..7f0ada6820 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -475,10 +475,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, networkMonitorObserver = ServiceLocator.shared.networkMonitor .reachabilityPublisher .removeDuplicates() - .sink { reachable in - MXLog.info("Reachability changed to \(reachable)") + .sink { reachability in + MXLog.info("Reachability changed to \(reachability)") - if reachable { + if reachability == .reachable { ServiceLocator.shared.userIndicatorController.retractIndicatorWithId(reachabilityNotificationIdentifier) } else { ServiceLocator.shared.userIndicatorController.submitIndicator(.init(id: reachabilityNotificationIdentifier, @@ -566,7 +566,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, ServiceLocator.shared.userIndicatorController.submitIndicator(.init(id: identifier, type: .toast(progress: .indeterminate), title: L10n.commonSyncing, persistent: true)) } - showLoadingIndicator() + // Prevent the syncing indicator from showing over the offline one + if ServiceLocator.shared.networkMonitor.reachabilityPublisher.value == .reachable { + showLoadingIndicator() + } clientProxyObserver = userSession.clientProxy .callbacks diff --git a/ElementX/Sources/Other/NetworkMonitor.swift b/ElementX/Sources/Other/NetworkMonitor.swift index 30c09f7e36..fa2f51a33c 100644 --- a/ElementX/Sources/Other/NetworkMonitor.swift +++ b/ElementX/Sources/Other/NetworkMonitor.swift @@ -18,10 +18,19 @@ import Combine import Foundation import Network +enum NetworkMonitorReachability { + case reachable + case unreachable +} + class NetworkMonitor { private let pathMonitor: NWPathMonitor private let queue: DispatchQueue - let reachabilityPublisher: CurrentValueSubject + + private let reachabilitySubject: CurrentValueSubject + var reachabilityPublisher: CurrentValuePublisher { + reachabilitySubject.asCurrentValuePublisher() + } var isCurrentConnectionExpensive: Bool { pathMonitor.currentPath.isExpensive @@ -34,14 +43,14 @@ class NetworkMonitor { init() { queue = DispatchQueue(label: "io.element.elementx.networkmonitor", qos: .background) pathMonitor = NWPathMonitor() - reachabilityPublisher = CurrentValueSubject(true) + reachabilitySubject = CurrentValueSubject(.reachable) pathMonitor.pathUpdateHandler = { [weak self] path in DispatchQueue.main.async { if path.status == .satisfied { - self?.reachabilityPublisher.send(true) + self?.reachabilitySubject.send(.reachable) } else { - self?.reachabilityPublisher.send(false) + self?.reachabilitySubject.send(.unreachable) } } }