forked from Polidea/RxBluetoothKit
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CharacteristicNotifyViewController
- Loading branch information
Mikołaj Piechocki
committed
Nov 2, 2020
1 parent
653b992
commit 853e50e
Showing
6 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
36 changes: 36 additions & 0 deletions
36
ExampleApp/ExampleApp/Screens/CharacteristicNotify/CharacteristicNotifyView.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import UIKit | ||
|
||
class CharacteristicNotifyView: UIView { | ||
|
||
init() { | ||
super.init(frame: .zero) | ||
backgroundColor = .systemBackground | ||
setupLayout() | ||
} | ||
|
||
required init?(coder: NSCoder) { nil } | ||
|
||
// MARK: - Subviews | ||
|
||
let label: UILabel = { | ||
let label = UILabel() | ||
label.text = "Updated value: --" | ||
label.font = UIFont.systemFont(ofSize: 20.0) | ||
label.textColor = .green | ||
return label | ||
}() | ||
|
||
// MARK: - Private | ||
|
||
private func setupLayout() { | ||
label.translatesAutoresizingMaskIntoConstraints = false | ||
addSubview(label) | ||
|
||
NSLayoutConstraint.activate([ | ||
label.centerXAnchor.constraint(equalTo: centerXAnchor), | ||
label.centerYAnchor.constraint(equalTo: centerYAnchor), | ||
label.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, constant: -32) | ||
]) | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
ExampleApp/ExampleApp/Screens/CharacteristicNotify/CharacteristicNotifyViewController.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import RxBluetoothKit | ||
import RxSwift | ||
import UIKit | ||
|
||
class CharacteristicNotifyViewController: UIViewController { | ||
|
||
init(characteristic: Characteristic, bluetoothProvider: BluetoothProvider) { | ||
self.characteristic = characteristic | ||
self.bluetoothProvider = bluetoothProvider | ||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
required init?(coder: NSCoder) { nil } | ||
|
||
// MARK: - View | ||
|
||
private(set) lazy var characteristicNotifyView = CharacteristicNotifyView() | ||
|
||
override func loadView() { | ||
view = characteristicNotifyView | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
setupBindings() | ||
} | ||
|
||
override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
|
||
viewDidAppearSubject.onNext(()) | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private let characteristic: Characteristic | ||
private let bluetoothProvider: BluetoothProvider | ||
private let viewDidAppearSubject = PublishSubject<Void>() | ||
private let disposeBag = DisposeBag() | ||
|
||
private func setupBindings() { | ||
viewDidAppearSubject | ||
.take(1) | ||
.flatMap { [characteristic, bluetoothProvider] in bluetoothProvider.getValueUpdates(for: characteristic) } | ||
.subscribe( | ||
onNext: { [weak self] in self?.characteristicNotifyView.label.text = "Updated value: " + $0 }, | ||
onError: { [weak self] in AlertPresenter.presentError(with: $0.printable, on: self?.navigationController) } | ||
) | ||
.disposed(by: disposeBag) | ||
} | ||
|
||
} |