Created
May 22, 2022 17:06
-
-
Save saeid-rez/ce4dc200744f9c21a33427006325173c to your computer and use it in GitHub Desktop.
Network reachability helper with Combine and Alamofire
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
| import Alamofire | |
| import Combine | |
| final class NetworkReachabilityHelper { | |
| static let shared = NetworkReachabilityHelper() | |
| private enum Constants { | |
| static let host = "api1.\(Environment.domain)" | |
| } | |
| let networkStatus = PassthroughSubject<NetworkReachabilityManager.NetworkReachabilityStatus, Error>() | |
| var reachability = NetworkReachabilityManager(host: Constants.host) | |
| var isReachableOnCellular: Bool { | |
| get { | |
| return reachability?.isReachableOnCellular ?? false | |
| } | |
| } | |
| var isReachableOnEthernetOrWiFi: Bool { | |
| get { | |
| return reachability?.isReachableOnEthernetOrWiFi ?? false | |
| } | |
| } | |
| var isReachable: Bool { | |
| get { | |
| return reachability?.isReachable ?? false | |
| } | |
| } | |
| func startListening() { | |
| reachability?.startListening { [weak self] status in | |
| self?.networkStatus.send(status) | |
| } | |
| } | |
| func stopListening() { | |
| reachability?.stopListening() | |
| } | |
| deinit { | |
| stopListening() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment