Skip to content

Commit

Permalink
Update to new Eureka API
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiet480 committed Sep 23, 2016
1 parent 79d61ae commit bf86f57
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
7 changes: 4 additions & 3 deletions HomeAssistant/EurekaLocationRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import MapKit
public final class LocationRow : SelectorRow<PushSelectorCell<CLLocation>, MapViewController>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
presentationMode = .show(controllerProvider: ControllerProvider.callback { return MapViewController(){ _ in } }, completionCallback: { vc in _ = vc.navigationController?.popViewController(animated: true) })
presentationMode = .show(controllerProvider: ControllerProvider.callback { return MapViewController(){ _ in } }, onDismiss: { vc in _ = vc.navigationController?.popViewController(animated: true) })

displayValueFor = {
guard let location = $0 else { return "" }
Expand All @@ -33,7 +33,8 @@ public final class LocationRow : SelectorRow<PushSelectorCell<CLLocation>, MapVi
public class MapViewController : UIViewController, TypedRowControllerType, MKMapViewDelegate {

public var row: RowOf<CLLocation>!
public var completionCallback : ((UIViewController) -> ())?
/// A closure to be called when the controller disappears.
public var onDismissCallback: ((UIViewController) -> ())?

lazy var mapView : MKMapView = { [unowned self] in
let v = MKMapView(frame: self.view.bounds)
Expand All @@ -51,7 +52,7 @@ public class MapViewController : UIViewController, TypedRowControllerType, MKMap

convenience public init(_ callback: ((UIViewController) -> ())?){
self.init(nibName: nil, bundle: nil)
completionCallback = callback
onDismissCallback = callback
}

public override func viewDidLoad() {
Expand Down
12 changes: 6 additions & 6 deletions HomeAssistant/GroupViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class GroupViewController: FormViewController {
self.form.last! <<< ButtonRow(entity.ID) {
$0.title = entity.Name
if url.scheme == "http" || url.scheme == "https" {
$0.presentationMode = .presentModally(controllerProvider: ControllerProvider.callback { return SFSafariViewController(url: url, entersReaderIfAvailable: false) }, completionCallback: { vc in let _ = vc.navigationController?.popViewController(animated: true) })
$0.presentationMode = .presentModally(controllerProvider: ControllerProvider.callback { return SFSafariViewController(url: url, entersReaderIfAvailable: false) }, onDismiss: { vc in let _ = vc.navigationController?.popViewController(animated: true) })
}
}.cellUpdate { cell, row in
cell.imageView?.image = entity.EntityIcon
Expand All @@ -88,7 +88,7 @@ class GroupViewController: FormViewController {
let attributesView = EntityAttributesViewController()
attributesView.entityID = entity.ID
return attributesView
}, completionCallback: {
}, onDismiss: {
vc in let _ = vc.navigationController?.popViewController(animated: true)
})
}.cellUpdate { cell, row in
Expand Down Expand Up @@ -122,7 +122,7 @@ class GroupViewController: FormViewController {
let attributesView = EntityAttributesViewController()
attributesView.entityID = entity.ID
return attributesView
}, completionCallback: { vc in let _ = vc.navigationController?.popViewController(animated: true) })
}, onDismiss: { vc in let _ = vc.navigationController?.popViewController(animated: true) })
}.cellUpdate { cell, row in
cell.detailTextLabel?.text = entity.CleanedState
if let uom = entity.UnitOfMeasurement {
Expand All @@ -142,7 +142,7 @@ class GroupViewController: FormViewController {
let attributesView = EntityAttributesViewController()
attributesView.entityID = entity.ID
return attributesView
}, completionCallback: { vc in let _ = vc.navigationController?.popViewController(animated: true) })
}, onDismiss: { vc in let _ = vc.navigationController?.popViewController(animated: true) })
}.cellUpdate { cell, row in
cell.detailTextLabel?.text = entity.CleanedState
if let uom = entity.UnitOfMeasurement {
Expand Down Expand Up @@ -239,14 +239,14 @@ class GroupViewController: FormViewController {
if let event = Mapper<StateChangedEvent>().map(JSON: userInfo as! [String : Any]) {
if let newState = event.NewState {
if newState.Domain == "lock" || newState.Domain == "garage_door" {
if let row : SwitchRow = self.form.rowByTag(newState.ID) {
if let row : SwitchRow = self.form.rowBy(tag: newState.ID) {
row.value = (newState.State == "on") ? true : false
row.cell.imageView?.image = newState.EntityIcon
row.updateCell()
row.reload()
}
} else {
if let row : ButtonRow = self.form.rowByTag(newState.ID) {
if let row : ButtonRow = self.form.rowBy(tag: newState.ID) {
row.value = newState.State
if let uom = newState.UnitOfMeasurement {
row.value = newState.State + " " + uom
Expand Down
63 changes: 32 additions & 31 deletions HomeAssistant/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class SettingsViewController: FormViewController {
$0.disabled = Condition(booleanLiteral: self.configured)
}.onChange { row in
if row.value == URL(string: "https://") { return }
let apiPasswordRow: PasswordRow = self.form.rowByTag("apiPassword")!
let apiPasswordRow: PasswordRow = self.form.rowBy(tag: "apiPassword")!
apiPasswordRow.value = ""
if let url = row.value {
let cleanUrl = HomeAssistantAPI.sharedInstance.CleanBaseURL(baseUrl: url)
Expand All @@ -99,12 +99,14 @@ class SettingsViewController: FormViewController {
self.baseURL = cleanUrl.cleanedURL
}
}
}.onCellHighlight { cell, row in
row.value = URL(string: "https://")
}.onCellUnHighlight { cell, row in
if row.value == URL(string: "https://") {
row.value = nil
cell.update()
}.cellUpdate { cell, row in
if row.isHighlighted {
row.value = URL(string: "https://")
} else {
if row.value == URL(string: "https://") {
row.value = nil
cell.update()
}
}
}

Expand All @@ -124,10 +126,10 @@ class SettingsViewController: FormViewController {
if self.connectStep == 1 {
if let url = self.baseURL {
HomeAssistantAPI.sharedInstance.GetDiscoveryInfo(baseUrl: url).then { discoveryInfo -> Void in
let urlRow: URLRow = self.form.rowByTag("baseURL")!
let urlRow: URLRow = self.form.rowBy(tag: "baseURL")!
urlRow.disabled = true
urlRow.evaluateDisabled()
let apiPasswordRow: PasswordRow = self.form.rowByTag("apiPassword")!
let apiPasswordRow: PasswordRow = self.form.rowBy(tag: "apiPassword")!
apiPasswordRow.value = ""
apiPasswordRow.hidden = Condition(booleanLiteral: !discoveryInfo.RequiresPassword)
apiPasswordRow.evaluateHidden()
Expand Down Expand Up @@ -181,13 +183,12 @@ class SettingsViewController: FormViewController {
$0.value = removeSpecialCharsFromString(text: UIDevice.current.name).replacingOccurrences(of: " ", with: "_").lowercased()
}
$0.cell.textField.autocapitalizationType = .none
}.onCellHighlight { cell, row in
print("Cell highlight")
}.onCellUnHighlight { cell, row in
print("Cell unhighlight")
}.cellUpdate { cell, row in
if row.isHighlighted == false {
self.prefs.setValue(row.value, forKey: "deviceId")
self.prefs.synchronize()
}
}
// <<< SwitchRow("allowAllGroups") {
// $0.title = "Show all groups"
// $0.value = prefs.bool(forKey: "allowAllGroups")
Expand All @@ -199,7 +200,7 @@ class SettingsViewController: FormViewController {
let view = SettingsDetailViewController()
view.detailGroup = "display"
return view
}, completionCallback: { vc in
}, onDismiss: { vc in
let _ = vc.navigationController?.popViewController(animated: true)
})
}
Expand All @@ -223,7 +224,7 @@ class SettingsViewController: FormViewController {
}
row.hidden = true
row.evaluateHidden()
let locationSettingsRow: ButtonRow = self.form.rowByTag("locationSettings")!
let locationSettingsRow: ButtonRow = self.form.rowBy(tag: "locationSettings")!
locationSettingsRow.hidden = false
locationSettingsRow.evaluateHidden()
}
Expand All @@ -240,7 +241,7 @@ class SettingsViewController: FormViewController {
let view = SettingsDetailViewController()
view.detailGroup = "location"
return view
}, completionCallback: { vc in
}, onDismiss: { vc in
let _ = vc.navigationController?.popViewController(animated: true)
})
}
Expand All @@ -262,7 +263,7 @@ class SettingsViewController: FormViewController {
}
row.hidden = true
row.evaluateHidden()
let notificationSettingsRow: ButtonRow = self.form.rowByTag("notificationSettings")!
let notificationSettingsRow: ButtonRow = self.form.rowBy(tag: "notificationSettings")!
notificationSettingsRow.hidden = false
notificationSettingsRow.evaluateHidden()
}
Expand All @@ -280,7 +281,7 @@ class SettingsViewController: FormViewController {
let view = SettingsDetailViewController()
view.detailGroup = "notifications"
return view
}, completionCallback: { vc in
}, onDismiss: { vc in
let _ = vc.navigationController?.popViewController(animated: true)
})
}
Expand Down Expand Up @@ -309,15 +310,15 @@ class SettingsViewController: FormViewController {
$0.title = "Help"
$0.presentationMode = .presentModally(controllerProvider: ControllerProvider.callback {
return SFSafariViewController(url: URL(string: "https://community.home-assistant.io/c/ios")!, entersReaderIfAvailable: false)
}, completionCallback: { vc in
}, onDismiss: { vc in
let _ = vc.navigationController?.popViewController(animated: true)
})
}
<<< ButtonRow("acknowledgementsButton") {
$0.title = "Acknowledgements"
$0.presentationMode = .show(controllerProvider: ControllerProvider.callback {
return AcknowListViewController()
}, completionCallback: { vc in
}, onDismiss: { vc in
let _ = vc.navigationController?.popViewController(animated: true)
})
}
Expand Down Expand Up @@ -408,14 +409,14 @@ class SettingsViewController: FormViewController {


func HomeAssistantDiscovered(_ notification: Notification){
let discoverySection : Section = self.form.sectionByTag("discoveredInstances")!
let discoverySection : Section = self.form.sectionBy(tag: "discoveredInstances")!
discoverySection.hidden = false
discoverySection.evaluateHidden()
if let userInfo = (notification as Notification).userInfo as? [String:Any] {
let discoveryInfo = DiscoveryInfoResponse(JSON: userInfo)!
let needsPass = discoveryInfo.RequiresPassword ? " - Requires password" : ""
let detailTextLabel = "\(discoveryInfo.BaseURL!.host!):\(discoveryInfo.BaseURL!.port!) - \(discoveryInfo.Version) - \(discoveryInfo.BaseURL!.scheme!.uppercased()) \(needsPass)"
if self.form.rowByTag(discoveryInfo.LocationName) == nil {
if self.form.rowBy(tag: discoveryInfo.LocationName) == nil {
discoverySection
<<< ButtonRow(discoveryInfo.LocationName) {
$0.title = discoveryInfo.LocationName
Expand All @@ -424,19 +425,19 @@ class SettingsViewController: FormViewController {
cell.textLabel?.textColor = .black
cell.detailTextLabel?.text = detailTextLabel
}.onCellSelection({ cell, row in
let urlRow: URLRow = self.form.rowByTag("baseURL")!
let urlRow: URLRow = self.form.rowBy(tag: "baseURL")!
urlRow.value = discoveryInfo.BaseURL
urlRow.disabled = true
urlRow.evaluateDisabled()
let apiPasswordRow: PasswordRow = self.form.rowByTag("apiPassword")!
let apiPasswordRow: PasswordRow = self.form.rowBy(tag: "apiPassword")!
apiPasswordRow.value = ""
apiPasswordRow.hidden = Condition(booleanLiteral: !discoveryInfo.RequiresPassword)
apiPasswordRow.evaluateHidden()
self.connectStep = 2
})
self.tableView?.reloadData()
} else {
if let readdedRow : ButtonRow = self.form.rowByTag(discoveryInfo.LocationName) {
if let readdedRow : ButtonRow = self.form.rowBy(tag: discoveryInfo.LocationName) {
readdedRow.hidden = false
readdedRow.updateCell()
readdedRow.evaluateHidden()
Expand All @@ -448,13 +449,13 @@ class SettingsViewController: FormViewController {
func HomeAssistantUndiscovered(_ notification: Notification){
if let userInfo = (notification as Notification).userInfo {
let name = userInfo["name"] as! String
if let removingRow : ButtonRow = self.form.rowByTag(name) {
if let removingRow : ButtonRow = self.form.rowBy(tag: name) {
removingRow.hidden = true
removingRow.evaluateHidden()
removingRow.updateCell()
}
}
let discoverySection : Section = self.form.sectionByTag("discoveredInstances")!
let discoverySection : Section = self.form.sectionBy(tag: "discoveredInstances")!
discoverySection.hidden = Condition(booleanLiteral: (discoverySection.count < 1))
discoverySection.evaluateHidden()
}
Expand Down Expand Up @@ -492,22 +493,22 @@ class SettingsViewController: FormViewController {
}

func saveSettings() {
if let urlRow: URLRow = self.form.rowByTag("baseURL") {
if let urlRow: URLRow = self.form.rowBy(tag: "baseURL") {
if let url = urlRow.value {
self.prefs.setValue(url.absoluteString, forKey: "baseURL")
}
}
if let apiPasswordRow: PasswordRow = self.form.rowByTag("apiPassword") {
if let apiPasswordRow: PasswordRow = self.form.rowBy(tag: "apiPassword") {
if let password = apiPasswordRow.value {
self.prefs.setValue(password, forKey: "apiPassword")
}
}
if let deviceIdRow: TextRow = self.form.rowByTag("deviceId") {
if let deviceIdRow: TextRow = self.form.rowBy(tag: "deviceId") {
if let deviceId = deviceIdRow.value {
self.prefs.setValue(deviceId, forKey: "deviceId")
}
}
if let allowAllGroupsRow: SwitchRow = self.form.rowByTag("allowAllGroups") {
if let allowAllGroupsRow: SwitchRow = self.form.rowBy(tag: "allowAllGroups") {
if let allowAllGroups = allowAllGroupsRow.value {
self.prefs.set(allowAllGroups, forKey: "allowAllGroups")
}
Expand Down

0 comments on commit bf86f57

Please sign in to comment.