Created
August 4, 2019 13:37
-
-
Save rinat-enikeev/3030c55c30790ee55ee743223b03400a to your computer and use it in GitHub Desktop.
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
import UIKit | |
import BTKit | |
class ViewController: UITableViewController { | |
private var ruuviTagsSet = Set<RuuviTag>() | |
private var ruuviTags = [RuuviTag]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
BTKit.scanner.scan(self) { (observer, device) in | |
if let ruuviTag = device.ruuvi?.tag { | |
observer.ruuviTagsSet.update(with: ruuviTag) | |
} | |
} | |
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in | |
self.ruuviTags = self.ruuviTagsSet.sorted(by: { $0.rssi < $1.rssi }) | |
self.tableView.reloadData() | |
} | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return ruuviTags.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellReuseIdentifier", for: indexPath) as! TableViewCell | |
let tag = ruuviTags[indexPath.row] | |
cell.uuidValueLabel.text = tag.uuid | |
cell.temperatureValueLabel.text = "\(tag.celsius ?? 0) °C" | |
cell.humidityValueLabel.text = "\(tag.humidity ?? 0) %" | |
cell.pressureValueLabel.text = "\(tag.pressure ?? 0) hPa" | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment