Check network reachability before attempting to restart the sync service

This commit is contained in:
Stefan Ceriu
2023-10-12 12:39:53 +03:00
parent 8e03b89641
commit 7f09c02298
6 changed files with 70 additions and 14 deletions
@@ -33,9 +33,9 @@ class ServiceLocator {
settings = appSettings
}
private(set) var networkMonitor: NetworkMonitor!
private(set) var networkMonitor: NetworkMonitorProtocol!
func register(networkMonitor: NetworkMonitor) {
func register(networkMonitor: NetworkMonitorProtocol) {
self.networkMonitor = networkMonitor
}
@@ -18,12 +18,7 @@ import Combine
import Foundation
import Network
enum NetworkMonitorReachability {
case reachable
case unreachable
}
class NetworkMonitor {
class NetworkMonitor: NetworkMonitorProtocol {
private let pathMonitor: NWPathMonitor
private let queue: DispatchQueue
@@ -0,0 +1,28 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
enum NetworkMonitorReachability {
case reachable
case unreachable
}
protocol NetworkMonitorProtocol {
var reachabilityPublisher: CurrentValuePublisher<NetworkMonitorReachability, Never> { get }
var isCurrentConnectionExpensive: Bool { get }
var isCurrentConnectionConstrained: Bool { get }
}
@@ -23,6 +23,7 @@ class ClientProxy: ClientProxyProtocol {
private let client: ClientProtocol
private let backgroundTaskService: BackgroundTaskServiceProtocol
private let appSettings: AppSettings
private let networkMonitor: NetworkMonitorProtocol
private var sessionVerificationControllerProxy: SessionVerificationControllerProxy?
private let mediaLoader: MediaLoaderProtocol
@@ -77,10 +78,14 @@ class ClientProxy: ClientProxyProtocol {
loadingStateSubject.asCurrentValuePublisher()
}
init(client: ClientProtocol, backgroundTaskService: BackgroundTaskServiceProtocol, appSettings: AppSettings) async {
init(client: ClientProtocol,
backgroundTaskService: BackgroundTaskServiceProtocol,
appSettings: AppSettings,
networkMonitor: NetworkMonitorProtocol) async {
self.client = client
self.backgroundTaskService = backgroundTaskService
self.appSettings = appSettings
self.networkMonitor = networkMonitor
clientQueue = .init(label: "ClientProxyQueue", attributes: .concurrent)
@@ -94,6 +99,16 @@ class ClientProxy: ClientProxyProtocol {
self?.callbacks.send(.receivedAuthError(isSoftLogout: isSoftLogout))
})
networkMonitor.reachabilityPublisher
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { [weak self] reachability in
if reachability == .reachable {
self?.startSync()
}
}
.store(in: &cancellables)
await configureAppService()
loadUserAvatarURLFromCache()
@@ -136,6 +151,11 @@ class ClientProxy: ClientProxyProtocol {
return
}
guard networkMonitor.reachabilityPublisher.value == .reachable else {
MXLog.warning("Ignoring request, network unreachable.")
return
}
MXLog.info("Starting sync")
Task {
@@ -142,7 +142,8 @@ class UserSessionStore: UserSessionStoreProtocol {
let clientProxy = await ClientProxy(client: client,
backgroundTaskService: backgroundTaskService,
appSettings: ServiceLocator.shared.settings)
appSettings: ServiceLocator.shared.settings,
networkMonitor: ServiceLocator.shared.networkMonitor)
return .success(clientProxy)
}