Skip to content

Commit e145821

Browse files
Merge pull request #100 from Adamant-im/feature/connection_bug_fix
Fixed incorrect connection label showing
2 parents 4686e9c + d890476 commit e145821

File tree

5 files changed

+64
-96
lines changed

5 files changed

+64
-96
lines changed

Adamant/AppDelegate.swift

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,30 +152,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
152152
if let reachability = container.resolve(ReachabilityMonitor.self) {
153153
reachability.start()
154154

155-
switch reachability.connection {
156-
case .cellular, .wifi:
155+
if reachability.connection {
157156
dialogService.dissmisNoConnectionNotification()
158-
break
159-
160-
case .none:
157+
} else {
161158
dialogService.showNoConnectionNotification()
162159
repeater.pauseAll()
163160
}
164161

165162
NotificationCenter.default.addObserver(forName: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged, object: reachability, queue: nil) { [weak self] notification in
166-
guard let connection = notification.userInfo?[AdamantUserInfoKey.ReachabilityMonitor.connection] as? AdamantConnection,
163+
guard let connection = notification.userInfo?[AdamantUserInfoKey.ReachabilityMonitor.connection] as? Bool,
167164
let repeater = self?.repeater else {
168165
return
169166
}
170167

171-
switch connection {
172-
case .cellular, .wifi:
168+
if connection {
173169
DispatchQueue.onMainSync {
174170
self?.dialogService.dissmisNoConnectionNotification()
175171
}
176172
repeater.resumeAll()
177-
178-
case .none:
173+
} else {
179174
DispatchQueue.onMainSync {
180175
self?.dialogService.showNoConnectionNotification()
181176
}
@@ -254,17 +249,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
254249
notificationService.removeAllDeliveredNotifications()
255250
}
256251

257-
if let connection = container.resolve(ReachabilityMonitor.self)?.connection {
258-
switch connection {
259-
case .wifi, .cellular:
260-
repeater.resumeAll()
261-
262-
case .none:
263-
break
264-
}
265-
} else {
266-
repeater.resumeAll()
267-
}
252+
guard container.resolve(ReachabilityMonitor.self)?.connection == true
253+
else { return }
254+
255+
repeater.resumeAll()
268256
}
269257
}
270258

Adamant/ServiceProtocols/ReachabilityMonitor.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ extension AdamantUserInfoKey {
2525
}
2626
}
2727

28-
enum AdamantConnection {
29-
case none, wifi, cellular
30-
}
31-
3228
protocol ReachabilityMonitor {
33-
var connection: AdamantConnection { get }
29+
var connection: Bool { get }
3430

3531
func start()
3632
func stop()

Adamant/Services/AdamantAccountService.swift

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,24 @@ class AdamantAccountService: AccountService {
140140
case .failure(let error):
141141
switch error {
142142
case .networkError:
143-
NotificationCenter.default.addObserver(forName: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged, object: nil, queue: nil) { notification in
144-
guard let connection = notification.userInfo?[AdamantUserInfoKey.ReachabilityMonitor.connection] as? AdamantConnection else {
145-
return
146-
}
143+
NotificationCenter.default.addObserver(
144+
forName: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged,
145+
object: nil, queue: nil
146+
) { notification in
147+
guard (notification.userInfo?[AdamantUserInfoKey.ReachabilityMonitor.connection] as? Bool) == true
148+
else { return }
147149

148-
switch connection {
149-
case .none:
150-
break
151-
152-
case .wifi, .cellular:
153-
ethWallet.initiateNetwork(apiUrl: url) { result in
154-
switch result {
155-
case .success:
156-
NotificationCenter.default.removeObserver(self, name: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged, object: nil)
157-
158-
case .failure(let error):
159-
// self.dialogService.showRichError(error: error)
160-
print(error)
161-
}
150+
ethWallet.initiateNetwork(apiUrl: url) { result in
151+
switch result {
152+
case .success:
153+
NotificationCenter.default.removeObserver(
154+
self,
155+
name: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged,
156+
object: nil
157+
)
158+
159+
case .failure(let error):
160+
print(error)
162161
}
163162
}
164163
}

Adamant/Services/AdamantReachability.swift

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,43 @@ import Foundation
1010
import Reachability
1111
import Network
1212

13-
// MAKR: - Convinients
14-
extension Reachability.Connection {
15-
var adamantConnection: AdamantConnection {
16-
switch self {
17-
case .none, .unavailable:
18-
return .none
19-
20-
case .wifi:
21-
return .wifi
22-
23-
case .cellular:
24-
return .cellular
25-
}
26-
}
27-
}
28-
2913
// MARK: - AdamantReachability wrapper
3014
class AdamantReachability: ReachabilityMonitor {
31-
let monitorForWifi = NWPathMonitor(requiredInterfaceType: .wifi)
32-
let monitorForCellular = NWPathMonitor(requiredInterfaceType: .cellular)
33-
private var wifiStatus: NWPath.Status = .satisfied
34-
private var cellularStatus: NWPath.Status = .satisfied
35-
36-
var connection: AdamantConnection {
37-
if wifiStatus == .satisfied { return AdamantConnection.wifi }
38-
if cellularStatus == .satisfied { return AdamantConnection.cellular }
39-
return AdamantConnection.none
40-
}
15+
private let monitor = NWPathMonitor()
16+
private(set) var connection = true
4117

4218
func start() {
43-
monitorForWifi.pathUpdateHandler = { [weak self] path in
44-
self?.wifiStatus = path.status
45-
let status = path.status == .satisfied ? AdamantConnection.wifi : AdamantConnection.none
46-
let userInfo: [String:Any] = [AdamantUserInfoKey.ReachabilityMonitor.connection: status]
47-
NotificationCenter.default.post(name: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged, object: self, userInfo: userInfo)
48-
}
49-
monitorForCellular.pathUpdateHandler = { [weak self] path in
50-
self?.cellularStatus = path.status
51-
let status = path.status == .satisfied ? AdamantConnection.cellular : AdamantConnection.none
52-
let userInfo: [String:Any] = [AdamantUserInfoKey.ReachabilityMonitor.connection: status]
53-
NotificationCenter.default.post(name: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged, object: self, userInfo: userInfo)
19+
monitor.pathUpdateHandler = { [weak self] path in
20+
guard let self = self else { return }
21+
self.updateConnection()
22+
23+
let userInfo: [String: Any] = [
24+
AdamantUserInfoKey.ReachabilityMonitor.connection: self.connection
25+
]
26+
27+
NotificationCenter.default.post(
28+
name: Notification.Name.AdamantReachabilityMonitor.reachabilityChanged,
29+
object: self,
30+
userInfo: userInfo
31+
)
5432
}
5533

5634
let queue = DispatchQueue(label: "NetworkMonitor")
57-
monitorForCellular.start(queue: queue)
58-
monitorForWifi.start(queue: queue)
35+
monitor.start(queue: queue)
5936
}
6037

6138
func stop() {
62-
monitorForWifi.cancel()
63-
monitorForCellular.cancel()
39+
monitor.cancel()
6440
}
6541

42+
private func updateConnection() {
43+
switch monitor.currentPath.status {
44+
case .satisfied:
45+
connection = true
46+
case .unsatisfied, .requiresConnection:
47+
connection = false
48+
@unknown default:
49+
connection = false
50+
}
51+
}
6652
}

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class AdamantChatsProvider: ChatsProvider {
4141
public var isChatLoaded: [String : Bool] = [:]
4242
public var chatMaxMessages: [String : Int] = [:]
4343
public var chatLoadedMessages: [String : Int] = [:]
44-
private var connection: AdamantConnection?
4544
private var isConnectedToTheInternet = true
4645
private var onConnectionToTheInternetRestored: (() -> Void)?
4746

@@ -143,21 +142,21 @@ class AdamantChatsProvider: ChatsProvider {
143142
queue: nil
144143
) { [weak self] notification in
145144
guard let connection = notification
146-
.userInfo?[AdamantUserInfoKey.ReachabilityMonitor.connection] as? AdamantConnection
145+
.userInfo?[AdamantUserInfoKey.ReachabilityMonitor.connection] as? Bool
147146
else {
148147
return
149148
}
150-
self?.connection = connection
151-
switch self?.connection {
152-
case .some(.cellular), .some(.wifi):
153-
if self?.isConnectedToTheInternet == false {
154-
self?.onConnectionToTheInternetRestored?()
155-
self?.onConnectionToTheInternetRestored = nil
156-
}
157-
self?.isConnectedToTheInternet = true
158-
case nil, .some(.none):
149+
150+
guard connection == true else {
159151
self?.isConnectedToTheInternet = false
152+
return
153+
}
154+
155+
if self?.isConnectedToTheInternet == false {
156+
self?.onConnectionToTheInternetRestored?()
157+
self?.onConnectionToTheInternetRestored = nil
160158
}
159+
self?.isConnectedToTheInternet = true
161160
}
162161
}
163162

0 commit comments

Comments
 (0)