-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading without internet #103
Changes from 6 commits
732c237
5faf666
fdb9c88
1de1b51
9d1a2df
ae2d704
4aaa48f
9d33ddd
5bfc3b9
15f94f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,86 @@ | |
import UIKit | ||
import Moya | ||
import SnapKit | ||
import Reachability | ||
|
||
class NetworkManager: NSObject { | ||
|
||
var reachability: Reachability! | ||
|
||
// Create a singleton instance | ||
static let sharedInstance: NetworkManager = { return NetworkManager() }() | ||
|
||
|
||
override init() { | ||
super.init() | ||
|
||
// Initialise reachability | ||
reachability = Reachability()! | ||
|
||
// Register an observer for the network status | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(networkStatusChanged(_:)), | ||
name: .reachabilityChanged, | ||
object: reachability | ||
) | ||
|
||
do { | ||
// Start the network status notifier | ||
try reachability.startNotifier() | ||
} catch { | ||
print("Unable to start notifier") | ||
} | ||
} | ||
|
||
@objc func networkStatusChanged(_ notification: Notification) { | ||
// Do something globally here! | ||
} | ||
|
||
static func stopNotifier() -> Void { | ||
do { | ||
// Stop the network status notifier | ||
try (NetworkManager.sharedInstance.reachability).startNotifier() | ||
} catch { | ||
print("Error stopping notifier") | ||
} | ||
} | ||
|
||
// Network is reachable | ||
static func isReachable(completed: @escaping (NetworkManager) -> Void) { | ||
if (NetworkManager.sharedInstance.reachability).connection != .none { | ||
completed(NetworkManager.sharedInstance) | ||
} | ||
} | ||
|
||
// Network is unreachable | ||
static func isUnreachable(completed: @escaping (NetworkManager) -> Void) { | ||
if (NetworkManager.sharedInstance.reachability).connection == .none { | ||
completed(NetworkManager.sharedInstance) | ||
} | ||
} | ||
|
||
// Network is reachable via WWAN/Cellular | ||
static func isReachableViaWWAN(completed: @escaping (NetworkManager) -> Void) { | ||
if (NetworkManager.sharedInstance.reachability).connection == .cellular { | ||
completed(NetworkManager.sharedInstance) | ||
} | ||
} | ||
|
||
// Network is reachable via WiFi | ||
static func isReachableViaWiFi(completed: @escaping (NetworkManager) -> Void) { | ||
if (NetworkManager.sharedInstance.reachability).connection == .wifi { | ||
completed(NetworkManager.sharedInstance) | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
class MenuVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{ | ||
|
||
let provider = MoyaProvider<API_methods>() | ||
let reachability = Reachability()! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this. Delete this. |
||
|
||
@IBOutlet weak var allergensBar: AllergensBarScrollView! | ||
var topBar = TopBar() | ||
|
@@ -39,8 +115,20 @@ class MenuVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSo | |
NSAttributedStringKey.underlineStyle: 1 | ||
] as [NSAttributedStringKey: Any] | ||
|
||
|
||
|
||
|
||
@IBOutlet weak var loadingLabel: UILabel! | ||
|
||
|
||
override func viewDidLoad() { | ||
NetworkManager.isReachable { networkManagerInstance in | ||
print("Network is available") | ||
} | ||
|
||
NetworkManager.isUnreachable { networkManagerInstance in | ||
self.loadingLabel.text = "No internet!" | ||
} | ||
|
||
self.view.backgroundColor = UIColor.white | ||
|
||
super.viewDidLoad() | ||
|
@@ -50,6 +138,8 @@ class MenuVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSo | |
menuCardsCollection.dataSource = self | ||
menuCardsCollection.alwaysBounceVertical = true | ||
menuCardsCollection.allowsSelection = false | ||
|
||
|
||
|
||
self.view.addSubview(topBar) | ||
topBar.snp.makeConstraints { (make) -> Void in | ||
|
@@ -321,7 +411,5 @@ class MenuVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSo | |
if (segue.identifier == "segueToItemDetailVC") { | ||
let vc = segue.destination as! ItemDetailViewController | ||
vc.menuItem = self.menuItem | ||
} | ||
} | ||
} } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unrelated change. please revert to older indentation. |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,6 @@ target 'Bruin Bite' do | |
pod 'Starscream' | ||
pod 'Repeat' | ||
pod "Pulsator" | ||
pod 'ReachabilitySwift' | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,65 @@ | ||
PODS: | ||
- Alamofire (4.7.2) | ||
- Alamofire (4.8.0) | ||
- Bolts (1.9.0): | ||
- Bolts/AppLinks (= 1.9.0) | ||
- Bolts/Tasks (= 1.9.0) | ||
- Bolts/AppLinks (1.9.0): | ||
- Bolts/Tasks | ||
- Bolts/Tasks (1.9.0) | ||
- CZPicker (0.4.3) | ||
- FacebookCore (0.3.0): | ||
- Bolts (~> 1.8) | ||
- FBSDKCoreKit (~> 4.27) | ||
- FacebookLogin (0.3.0): | ||
- Bolts (~> 1.8) | ||
- FacebookCore (~> 0.3) | ||
- FBSDKCoreKit (~> 4.27) | ||
- FBSDKLoginKit (~> 4.27) | ||
- FBSDKCoreKit (4.33.0): | ||
- Bolts (~> 1.7) | ||
- FBSDKLoginKit (4.33.0): | ||
- FacebookCore (0.5.0): | ||
- Bolts (~> 1.9) | ||
- FBSDKCoreKit (~> 4.37) | ||
- FacebookLogin (0.5.0): | ||
- Bolts (~> 1.9) | ||
- FacebookCore (~> 0.5) | ||
- FBSDKCoreKit (~> 4.37) | ||
- FBSDKLoginKit (~> 4.37) | ||
- FBSDKCoreKit (4.38.1): | ||
- Bolts (~> 1.9) | ||
- FBSDKLoginKit (4.38.1): | ||
- FBSDKCoreKit | ||
- Moya (10.0.2): | ||
- Moya/Core (= 10.0.2) | ||
- Moya/Core (10.0.2): | ||
- Alamofire (~> 4.1) | ||
- Result (~> 3.0) | ||
- Pulsator (0.5.1) | ||
- ReachabilitySwift (4.3.0) | ||
- Repeat (0.5.7) | ||
- Result (3.2.4) | ||
- SnapKit (4.0.0) | ||
- Starscream (3.0.5) | ||
- SwiftyJSON (4.1.0) | ||
- SnapKit (4.2.0) | ||
- Starscream (3.0.6) | ||
- SwiftyJSON (4.2.0) | ||
|
||
DEPENDENCIES: | ||
- CZPicker | ||
- FacebookLogin | ||
- Moya (~> 10.0) | ||
- Pulsator | ||
- ReachabilitySwift | ||
- Repeat | ||
- SnapKit | ||
- Starscream | ||
- SwiftyJSON | ||
|
||
SPEC CHECKSUMS: | ||
Alamofire: e4fa87002c137ba2d8d634d2c51fabcda0d5c223 | ||
Alamofire: 3ec537f71edc9804815215393ae2b1a8ea33a844 | ||
Bolts: ac6567323eac61e203f6a9763667d0f711be34c8 | ||
CZPicker: 55aa1751ea26e209814552da1a9a08cba8bbf72a | ||
FacebookCore: 3ffa190a3f1f96cec0e44d3fc221bc322c595ffa | ||
FacebookLogin: 0af839ea858faef99550d16596b87b4a8e8906e6 | ||
FBSDKCoreKit: 572b047a7e029bc44542bcf8a59414e7ff2b543e | ||
FBSDKLoginKit: 88cb456349cfb3b554427ce4f8b43729d85dfb40 | ||
FacebookCore: 74288d0add931d361b1596beae787ab72c438249 | ||
FacebookLogin: ebcf34714a1cb9f0759d9f071cfb01ed500996cf | ||
FBSDKCoreKit: 8d47857400e2f5bdea697a80daff882e91c84ef6 | ||
FBSDKLoginKit: 4621c690d9dd8628031a4791497062183ea34b0d | ||
Moya: 2b0531a9fef318e0bc484030c3db1e6f279ccb66 | ||
Pulsator: f465ca37212d826523fe8c79b07c1a4c1a941f5e | ||
ReachabilitySwift: 408477d1b6ed9779dba301953171e017c31241f3 | ||
Repeat: 73c5ee602aed8f3c7bf1aa12d4cd0d3dc3edea0a | ||
Result: d2d07204ce72856f1fd9130bbe42c35a7b0fea10 | ||
SnapKit: a42d492c16e80209130a3379f73596c3454b7694 | ||
Starscream: faf918b2f2eff7d5dd21180646bf015a669673bd | ||
SwiftyJSON: c29297daf073d2aa016295d5809cdd68045c39b3 | ||
SnapKit: fe8a619752f3f27075cc9a90244d75c6c3f27e2a | ||
Starscream: ef3ece99d765eeccb67de105bfa143f929026cf5 | ||
SwiftyJSON: c4bcba26dd9ec7a027fc8eade48e2c911f229e96 | ||
|
||
PODFILE CHECKSUM: d796cb8069d903701ca59324d85a281c506be28f | ||
PODFILE CHECKSUM: d94fc98ba096932c93fecaeb22de708dca702695 | ||
|
||
COCOAPODS: 1.4.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this class to a new file called
NetworkManager.swift
under theHelpers
folder.