@@ -18,8 +18,8 @@ class CharacteristicsController: UIViewController {
18
18
19
19
@IBOutlet weak var characteristicsTableView : UITableView !
20
20
21
- private var characteristicsList : [ Characteristic ] = [ ]
22
- private let characteristicCellId = " CharacteristicCell "
21
+ fileprivate var characteristicsList : [ Characteristic ] = [ ]
22
+ fileprivate let characteristicCellId = " CharacteristicCell "
23
23
24
24
override func viewDidLoad( ) {
25
25
super. viewDidLoad ( )
@@ -29,100 +29,96 @@ class CharacteristicsController: UIViewController {
29
29
characteristicsTableView. rowHeight = UITableViewAutomaticDimension
30
30
}
31
31
32
- override func viewWillAppear( animated: Bool ) {
32
+ override func viewWillAppear( _ animated: Bool ) {
33
33
super. viewWillAppear ( animated)
34
- getCharacteristicsForService ( service)
34
+ getCharacteristics ( for : service)
35
35
}
36
36
37
- private func getCharacteristicsForService ( service: Service ) {
37
+ private func getCharacteristics ( for service: Service ) {
38
38
service. discoverCharacteristics ( nil )
39
- . subscribeNext { characteristics in
39
+ . subscribe ( onNext : { characteristics in
40
40
self . characteristicsList = characteristics
41
41
self . characteristicsTableView. reloadData ( )
42
- } . addDisposableTo ( disposeBag)
42
+ } ) . addDisposableTo ( disposeBag)
43
43
}
44
44
45
- private func setNotificationsState( enabled enabled: Bool , characteristic: Characteristic ) {
45
+ fileprivate func setNotificationsState( enabled: Bool , characteristic: Characteristic ) {
46
46
characteristic. setNotifyValue ( enabled)
47
- . subscribeNext {
48
- self . refreshCharacteristic ( $0 )
49
- } . addDisposableTo ( disposeBag)
47
+ . subscribe ( onNext : { [ weak self ] _ in
48
+ self ? . characteristicsTableView . reloadData ( )
49
+ } ) . addDisposableTo ( disposeBag)
50
50
}
51
51
52
52
private func showWriteFieldForCharacteristic( characteristic: Characteristic ) {
53
53
let valueWriteController = UIAlertController ( title: " Write value " , message: " Specify value in HEX to write " ,
54
- preferredStyle: . Alert )
55
- valueWriteController. addTextFieldWithConfigurationHandler { textField in
54
+ preferredStyle: . alert )
55
+ valueWriteController. addTextField { textField in
56
56
57
57
}
58
- valueWriteController. addAction ( UIAlertAction ( title: " Cancel " , style: . Cancel , handler: nil ) )
59
- valueWriteController. addAction ( UIAlertAction ( title: " Write " , style: . Default ) { _ in
58
+ valueWriteController. addAction ( UIAlertAction ( title: " Cancel " , style: . cancel , handler: nil ) )
59
+ valueWriteController. addAction ( UIAlertAction ( title: " Write " , style: . default ) { _ in
60
60
print ( " " )
61
61
} )
62
62
}
63
63
64
- private func triggerValueReadForCharacteristic ( characteristic: Characteristic ) {
64
+ fileprivate func triggerValueRead ( for characteristic: Characteristic ) {
65
65
characteristic. readValue ( )
66
- . subscribeNext {
67
- self . refreshCharacteristic ( $0)
68
- } . addDisposableTo ( disposeBag)
69
- }
70
-
71
- private func refreshCharacteristic( characteristic: Characteristic ) {
72
- characteristicsTableView. reloadData ( )
66
+ . subscribe ( onNext: { [ weak self] _ in
67
+ self ? . characteristicsTableView. reloadData ( )
68
+ } ) . addDisposableTo ( disposeBag)
73
69
}
74
70
}
75
71
76
72
extension CharacteristicsController : UITableViewDataSource , UITableViewDelegate {
77
73
78
- func tableView( tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
74
+ func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
79
75
return characteristicsList. count
80
76
}
81
77
82
- func tableView( tableView: UITableView , cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell {
83
- let cell = tableView. dequeueReusableCellWithIdentifier ( characteristicCellId, forIndexPath : indexPath)
78
+ func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
79
+ let cell = tableView. dequeueReusableCell ( withIdentifier : characteristicCellId, for : indexPath)
84
80
let characteristic = characteristicsList [ indexPath. row]
85
81
if let cell = cell as? CharacteristicTableViewCell {
86
- cell. updateWithCharacteristic ( characteristic)
82
+ cell. update ( with : characteristic)
87
83
}
88
84
return cell
89
85
}
90
86
91
- func tableView( tableView: UITableView , didSelectRowAtIndexPath indexPath: NSIndexPath ) {
87
+ func tableView( _ tableView: UITableView , didSelectRowAt indexPath: IndexPath ) {
92
88
let characteristic = characteristicsList [ indexPath. row]
93
- let actionSheet = UIAlertController ( title: " Choose action " , message: nil , preferredStyle: . ActionSheet )
89
+ let actionSheet = UIAlertController ( title: " Choose action " , message: nil , preferredStyle: . actionSheet )
94
90
95
- if characteristic. properties. contains ( . Notify ) {
96
- let turnNotificationOffAction = UIAlertAction ( title: " Turn OFF notifications " , style: . Default ) { _ in
91
+ if characteristic. properties. contains ( . notify ) {
92
+ let turnNotificationOffAction = UIAlertAction ( title: " Turn OFF notifications " , style: . default ) { _ in
97
93
self . setNotificationsState ( enabled: false , characteristic: characteristic)
98
94
}
99
- let turnNotificationOnAction = UIAlertAction ( title: " Turn ON notifications " , style: . Default ) { _ in
95
+ let turnNotificationOnAction = UIAlertAction ( title: " Turn ON notifications " , style: . default ) { _ in
100
96
self . setNotificationsState ( enabled: true , characteristic: characteristic)
101
97
}
102
98
actionSheet. addAction ( turnNotificationOffAction)
103
99
actionSheet. addAction ( turnNotificationOnAction)
104
100
}
105
- if characteristic. properties. contains ( . Read ) {
106
- let readValueNotificationAction = UIAlertAction ( title: " Trigger value read " , style: . Default ) { _ in
107
- self . triggerValueReadForCharacteristic ( characteristic)
101
+ if characteristic. properties. contains ( . read ) {
102
+ let readValueNotificationAction = UIAlertAction ( title: " Trigger value read " , style: . default ) { _ in
103
+ self . triggerValueRead ( for : characteristic)
108
104
}
109
105
actionSheet. addAction ( readValueNotificationAction)
110
106
}
111
- self . presentViewController ( actionSheet, animated: true , completion: nil )
107
+ self . present ( actionSheet, animated: true , completion: nil )
112
108
}
113
109
114
- func tableView( tableView: UITableView , viewForFooterInSection section: Int ) -> UIView ? {
110
+ func tableView( _ tableView: UITableView , viewForFooterInSection section: Int ) -> UIView ? {
115
111
return UIView ( frame: . zero)
116
112
}
117
113
118
- func tableView( tableView: UITableView , titleForHeaderInSection section: Int ) -> String ? {
114
+ func tableView( _ tableView: UITableView , titleForHeaderInSection section: Int ) -> String ? {
119
115
return " CHARACTERISTICS "
120
116
}
121
117
}
122
118
123
119
extension CharacteristicTableViewCell {
124
- func updateWithCharacteristic ( characteristic: Characteristic ) {
125
- self . UUIDLabel. text = characteristic. UUID . UUIDString
120
+ func update ( with characteristic: Characteristic ) {
121
+ self . UUIDLabel. text = characteristic. uuid . uuidString
126
122
self . isNotifyingLabel. text = characteristic. isNotifying ? " true " : " false "
127
123
self . valueLabel. text = characteristic. value? . hexadecimalString ?? " Empty "
128
124
}
0 commit comments