Skip to content

Commit

Permalink
#1257 - Prevent the syncing indicator from showing over the offline one
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu committed Aug 8, 2023
1 parent 9a7a9c8 commit b79ed28
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
11 changes: 7 additions & 4 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions ElementX/Sources/Other/NetworkMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool, Never>

private let reachabilitySubject: CurrentValueSubject<NetworkMonitorReachability, Never>
var reachabilityPublisher: CurrentValuePublisher<NetworkMonitorReachability, Never> {
reachabilitySubject.asCurrentValuePublisher()
}

var isCurrentConnectionExpensive: Bool {
pathMonitor.currentPath.isExpensive
Expand All @@ -34,14 +43,14 @@ class NetworkMonitor {
init() {
queue = DispatchQueue(label: "io.element.elementx.networkmonitor", qos: .background)
pathMonitor = NWPathMonitor()
reachabilityPublisher = CurrentValueSubject<Bool, Never>(true)
reachabilitySubject = CurrentValueSubject<NetworkMonitorReachability, Never>(.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)
}
}
}
Expand Down

0 comments on commit b79ed28

Please sign in to comment.