Files
Woka_Native_iOS/WOKA/Helpers/Network/NetworkMonitor.swift
BilalKhanWDI 08a2c7f8a2 - Fixed TC 41, TC 42, TC 43, TC 44, TC 45
- TC 40 fixed. Handled no internet
- TC 19 fixed. Now if the count it 0 it will hide the badge
- Added error handling to shop
- Fixed issue while switching themes. Now doing it on main thread with completion handler
2024-08-28 19:37:21 +05:30

54 lines
1.4 KiB
Swift

//
// NetworkMonitor.swift
// WOKA
//
// Created by MacBook Pro on 28/08/24.
//
import Foundation
import Network
extension Notification.Name {
static let connectivityStatus = Notification.Name(rawValue: "connectivityStatusChanged")
}
extension NWInterface.InterfaceType: CaseIterable {
public static var allCases: [NWInterface.InterfaceType] = [
.other,
.wifi,
.cellular,
.loopback,
.wiredEthernet
]
}
final class NetworkMonitor {
static let shared = NetworkMonitor()
private let queue = DispatchQueue(label: "NetworkConnectivityMonitor")
private let monitor: NWPathMonitor
private(set) var isConnected = false
private(set) var isExpensive = false
private(set) var currentConnectionType: NWInterface.InterfaceType?
private init() {
monitor = NWPathMonitor()
}
func startMonitoring() {
monitor.pathUpdateHandler = { [weak self] path in
self?.isConnected = path.status != .unsatisfied
self?.isExpensive = path.isExpensive
self?.currentConnectionType = NWInterface.InterfaceType.allCases.filter { path.usesInterfaceType($0) }.first
NotificationCenter.default.post(name: .connectivityStatus, object: nil)
}
monitor.start(queue: queue)
}
func stopMonitoring() {
monitor.cancel()
}
}