Created
January 19, 2017 05:26
-
-
Save codyrobb/e072cadb207a10a92a734c8fe0013afd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NetworkStatus { | |
case wifi | |
case wwan | |
case noConnection | |
} | |
enum NetworkMonitorError: Error { | |
case notStarted | |
case unavailable | |
} | |
final class NetworkMonitor { | |
// MARK: - | |
// MARK: Properties | |
private let monitor: Reachability? | |
let status: SignalProducer<NetworkStatus, NetworkMonitorError> | |
// MARK: - | |
// MARK: Initialization | |
init() { | |
let monitor = Reachability() | |
self.monitor = monitor | |
self.status = SignalProducer { observer, disposable in | |
guard let monitor = monitor else { | |
observer.send(error: .unavailable) | |
return | |
} | |
monitor.whenReachable = { reachability in | |
if reachability.isReachableViaWiFi { | |
observer.send(value: .wifi) | |
} | |
if reachability.isReachableViaWWAN { | |
observer.send(value: .wwan) | |
} | |
} | |
monitor.whenUnreachable = { _ in | |
observer.send(value: .noConnection) | |
} | |
do { | |
try monitor.startNotifier() | |
} catch { | |
observer.send(error: .notStarted) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To demo, you can use this: