Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Shows the device sensors and information about them, but doesn't let you turn off individual sensors yet. - Only refreshes on appearance or when pulling to refresh the list of sensors. Refs #358.
- Loading branch information
Showing
8 changed files
with
225 additions
and
0 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
70 changes: 70 additions & 0 deletions
70
HomeAssistant/Views/Settings/Sensors/SensorDetailViewController.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,70 @@ | ||
import Foundation | ||
import Shared | ||
import Eureka | ||
import UIKit | ||
|
||
class SensorDetailViewController: FormViewController { | ||
private(set) var sensor: WebhookSensor | ||
|
||
init(sensor: WebhookSensor) { | ||
self.sensor = sensor | ||
super.init(style: .grouped) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
updateModels() | ||
} | ||
|
||
private func updateModels() { | ||
title = sensor.Name | ||
|
||
form.removeAll() | ||
|
||
let baseSection = Section() | ||
baseSection <<< LabelRow { | ||
$0.title = L10n.SettingsSensors.Detail.uniqueId | ||
$0.value = sensor.UniqueID | ||
} | ||
baseSection <<< LabelRow { | ||
$0.title = L10n.SettingsSensors.Detail.state | ||
$0.value = sensor.StateDescription | ||
} | ||
|
||
if let deviceClass = sensor.DeviceClass { | ||
baseSection <<< LabelRow { | ||
$0.title = L10n.SettingsSensors.Detail.deviceClass | ||
$0.value = deviceClass.rawValue | ||
} | ||
} | ||
|
||
if let icon = sensor.Icon { | ||
baseSection <<< LabelRow { | ||
$0.title = L10n.SettingsSensors.Detail.icon | ||
$0.value = icon | ||
} | ||
} | ||
|
||
form +++ baseSection | ||
|
||
if let attributes = sensor.Attributes { | ||
let attributesSection = Section(header: L10n.SettingsSensors.Detail.attributes, footer: nil) | ||
let attributeRows = attributes | ||
.sorted(by: { lhs, rhs in lhs.0 < rhs.0 }) | ||
.map(Self.row(attribute:value:)) | ||
attributesSection.append(contentsOf: attributeRows) | ||
form.append(attributesSection) | ||
} | ||
} | ||
|
||
class func row(attribute: String, value: Any) -> BaseRow { | ||
return LabelRow { row in | ||
row.title = attribute | ||
row.value = String(describing: value) | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
HomeAssistant/Views/Settings/Sensors/SensorListViewController.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,91 @@ | ||
import Foundation | ||
import Eureka | ||
import Shared | ||
import Iconic | ||
import PromiseKit | ||
|
||
class SensorListViewController: FormViewController { | ||
private let sensorSection = Section() | ||
private let refreshControl = UIRefreshControl() | ||
private let sensors = WebhookSensors() | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
title = L10n.SettingsSensors.title | ||
|
||
updateSensors(section: sensorSection) | ||
form +++ sensorSection | ||
|
||
tableView.addSubview(refreshControl) | ||
refreshControl.addTarget(self, action: #selector(refresh), for: .primaryActionTriggered) | ||
} | ||
|
||
@objc private func refresh() { | ||
updateSensors(section: sensorSection) | ||
} | ||
|
||
private func updateSensors(section: Section) { | ||
guard !refreshControl.isRefreshing else { return } | ||
|
||
refreshControl.beginRefreshing() | ||
|
||
firstly { | ||
sensors.AllSensors | ||
}.map { | ||
$0.map { Self.row(for: $0) } | ||
}.done { | ||
section.removeAll() | ||
section.append(contentsOf: $0) | ||
}.ensure { [refreshControl] in | ||
refreshControl.endRefreshing() | ||
}.catch { error in | ||
let alert = UIAlertController( | ||
title: L10n.SettingsSensors.LoadingError.title, | ||
message: error.localizedDescription, | ||
preferredStyle: .alert | ||
) | ||
alert.addAction(UIAlertAction(title: L10n.retryLabel, style: .default, handler: { [weak self] _ in | ||
self?.refresh() | ||
})) | ||
alert.addAction(UIAlertAction(title: L10n.cancelLabel, style: .cancel, handler: { _ in | ||
|
||
})) | ||
self.present(alert, animated: true, completion: nil) | ||
} | ||
} | ||
|
||
class func row(for sensor: WebhookSensor) -> BaseRow { | ||
return ButtonRow { row in | ||
func updateDetails(from sensor: WebhookSensor, isInitial: Bool) { | ||
row.title = sensor.Name | ||
row.value = sensor.StateDescription | ||
row.cellStyle = .value1 | ||
|
||
row.cellUpdate { cell, _ in | ||
cell.detailTextLabel?.text = row.value | ||
|
||
cell.imageView?.image = | ||
sensor.Icon | ||
.flatMap(MaterialDesignIcons.init(named:))? | ||
.image(ofSize: CGSize(width: 28, height: 28), color: .black) | ||
.withRenderingMode(.alwaysTemplate) | ||
} | ||
|
||
if !isInitial { | ||
row.updateCell() | ||
} | ||
} | ||
|
||
updateDetails(from: sensor, isInitial: true) | ||
|
||
row.presentationMode = .show(controllerProvider: .callback(builder: { | ||
SensorDetailViewController(sensor: sensor) | ||
}), onDismiss: { | ||
if let controller = $0 as? SensorDetailViewController { | ||
updateDetails(from: controller.sensor, isInitial: false) | ||
} | ||
}) | ||
} | ||
} | ||
} |
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