- 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
54 lines
1.4 KiB
Swift
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()
|
|
}
|
|
}
|