forked from rwbutler/Connectivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombineViewController.swift
89 lines (77 loc) · 2.95 KB
/
CombineViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// CombineViewController.swift
// Connectivity
//
// Created by Ross Butler on 05/05/2020.
// Copyright © 2020 Ross Butler. All rights reserved.
//
#if canImport(Combine)
import Combine
import Connectivity
import UIKit
@available(iOS 13.0, *)
class CombineViewController: UIViewController {
// MARK: Outlets
@IBOutlet var activityIndicator: UIActivityIndicatorView!
@IBOutlet var notifierButton: UIButton!
@IBOutlet var statusLabel: UILabel!
// MARK: Status
fileprivate var isCheckingConnectivity: Bool = false
private var cancellable: AnyCancellable?
}
// IB Actions
@available(iOS 13.0, *)
extension CombineViewController {
@IBAction func notifierButtonTapped(_: UIButton) {
isCheckingConnectivity ? stopConnectivityChecks() : startConnectivityChecks()
}
}
// Private API
@available(iOS 13.0, *)
private extension CombineViewController {
func startConnectivityChecks() {
activityIndicator.startAnimating()
let publisher = Connectivity.Publisher(
configuration:
.init()
.configureURLSession(.default)
).eraseToAnyPublisher()
cancellable = publisher.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { [weak self] _ in
guard let strongSelf = self else {
return
}
strongSelf.activityIndicator.stopAnimating()
strongSelf.isCheckingConnectivity = false
strongSelf.updateNotifierButton(isCheckingConnectivity: strongSelf.isCheckingConnectivity)
}, receiveValue: { [weak self] connectivity in
self?.updateConnectionStatus(connectivity.status)
})
isCheckingConnectivity = true
updateNotifierButton(isCheckingConnectivity: isCheckingConnectivity)
}
func stopConnectivityChecks() {
activityIndicator.stopAnimating()
cancellable?.cancel()
isCheckingConnectivity = false
updateNotifierButton(isCheckingConnectivity: isCheckingConnectivity)
}
func updateConnectionStatus(_ status: Connectivity.Status) {
switch status {
case .connectedViaWiFi, .connectedViaCellular, .connected, .connectedViaEthernet:
statusLabel.textColor = UIColor.darkGreen
case .connectedViaWiFiWithoutInternet, .connectedViaCellularWithoutInternet, .connectedViaEthernetWithoutInternet, .notConnected:
statusLabel.textColor = UIColor.red
case .determining:
statusLabel.textColor = UIColor.black
}
statusLabel.text = status.description
}
func updateNotifierButton(isCheckingConnectivity: Bool) {
let buttonText = isCheckingConnectivity ? "Stop notifier" : "Start notifier"
let buttonTextColor = isCheckingConnectivity ? UIColor.red : UIColor.darkGreen
notifierButton.setTitle(buttonText, for: .normal)
notifierButton.setTitleColor(buttonTextColor, for: .normal)
}
}
#endif