Skip to content
Permalink
Browse files

Fix toggle

Put version check after async menu population
  • Loading branch information
andrew-codechimp committed Feb 1, 2020
1 parent c844e46 commit 09b6c6c5e633960921f46b0abf32e9876b4157a1
@@ -53,8 +53,16 @@ final class MenuItemController: NSObject, NSMenuDelegate {

public func menuWillOpen(_ menu: NSMenu){
self.removeDynamicMenuItems()
self.addDynamicMenuItems()
self.checkForUpdate()

self.addDynamicMenuItems(){
result in
switch result {
case .success( _):
self.checkForUpdate()
case .failure( _):
break
}
}
}

public func menuDidClose(_ menu: NSMenu){
@@ -107,7 +115,7 @@ final class MenuItemController: NSObject, NSMenuDelegate {
}
}

func addDynamicMenuItems() {
func addDynamicMenuItems(completionHandler: @escaping (Result<Bool, HaService.HaServiceApiError>) -> Void) {
haService.getStates() {
result in
switch result {
@@ -131,6 +139,7 @@ final class MenuItemController: NSObject, NSMenuDelegate {
self.addMenuItem(menuItem: menuItem)
}
}
completionHandler(.success(true))

case .failure(let haServiceApiError):
switch haServiceApiError {
@@ -147,8 +156,9 @@ final class MenuItemController: NSObject, NSMenuDelegate {
case .JSONDecodeError:
self.addErrorMenuItem(message: "Error Decoding JSON")
case .UnknownError:
self.addErrorMenuItem(message: "Unknown Error")
self.addErrorMenuItem(message: "Unknown Error (check your server/port)")
}
completionHandler(.failure(haServiceApiError))
break
}
}
@@ -158,7 +168,7 @@ final class MenuItemController: NSObject, NSMenuDelegate {
switch menuItem.itemType {
case itemTypes.Domain:
let domainItems = self.haService.filterEntities(entityDomain: menuItem.entityId)
self.addEntitiesToMenu(menuItem: menuItem, entities: domainItems)
self.addMenuItem(menuItem: menuItem, entities: domainItems)
case itemTypes.Group:
self.haService.getState(entityId: "group.\(menuItem.entityId)") { result in
switch result {
@@ -213,7 +223,7 @@ final class MenuItemController: NSObject, NSMenuDelegate {

entities = entities.reversed()

self.addEntitiesToMenu(menuItem: menuItem, entities: entities)
self.addMenuItem(menuItem: menuItem, entities: entities)

break
case .failure( _):
@@ -224,7 +234,7 @@ final class MenuItemController: NSObject, NSMenuDelegate {

}

func addEntitiesToMenu(menuItem: PrefMenuItem, entities: [HaEntity]) {
func addMenuItem(menuItem: PrefMenuItem, entities: [HaEntity]) {
DispatchQueue.main.async {

if entities.count == 0 {
@@ -258,7 +268,7 @@ final class MenuItemController: NSObject, NSMenuDelegate {

func addEntityMenuItem(parent: NSMenu, haEntity: HaEntity) {

if haEntity.domain == EntityDomains.inputSelectDomain {
if haEntity.domainType == EntityDomains.inputSelectDomain {
let inputSelectMenuItem = NSMenuItem()
inputSelectMenuItem.title = haEntity.friendlyName
inputSelectMenuItem.tag = haEntity.type.rawValue
@@ -35,7 +35,7 @@ struct HaEntity {
var state: String
var options: [String]

var domain: EntityDomains {
var domainType: EntityDomains {
get {
switch entityId.components(separatedBy: ".")[0]{
case EntityDomains.switchDomain.rawValue:
@@ -56,9 +56,15 @@ struct HaEntity {
}
}

var domain: String {
get {
return entityId.components(separatedBy: ".")[0]
}
}

var type: EntityTypes {
get {
switch domain{
switch domainType{
case EntityDomains.switchDomain:
return EntityTypes.switchType
case EntityDomains.lightDomain:
@@ -40,7 +40,7 @@ class HaService {
return request
}

func getStates(completionHandler: @escaping (Result<Int, HaServiceApiError>) -> Void) {
func getStates(completionHandler: @escaping (Result<Bool, HaServiceApiError>) -> Void) {

if (prefs.server.count == 0 ) {
completionHandler(.failure(.URLMissing))
@@ -76,7 +76,7 @@ class HaService {
let decodedResponse = try JSONDecoder().decode([HaState].self, from: data)
self.haStates = decodedResponse

completionHandler(.success(1))
completionHandler(.success(true))

} catch {
completionHandler(.failure(.JSONDecodeError))
@@ -120,8 +120,9 @@ class HaService {

func toggleEntityState(haEntity: HaEntity) {
let params = ["entity_id": haEntity.entityId]
let urlString = "\(prefs.server)/api/services/\(haEntity.domain)/toggle"

var request = createAuthURLRequest(url: URL(string: "\(prefs.server)/api/services/\(haEntity.domain)/toggle")!)
var request = createAuthURLRequest(url: URL(string: urlString)!)

request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
@@ -10,6 +10,15 @@ import Foundation

struct Preferences {

var settingsVersion: Int {
get {
return UserDefaults.standard.integer(forKey: "settingsVersion")
}
set {
UserDefaults.standard.set(newValue, forKey: "settingsVersion")
}
}

var server: String {
get {
var serverUrl = UserDefaults.standard.string(forKey: "server") ?? ""
@@ -176,7 +185,8 @@ struct Preferences {
let data = try JSONEncoder().encode(newValue)
let dataString = String(data: data, encoding: .utf8)!
UserDefaults.standard.set(dataString, forKey: "menu_items")
UserDefaults.standard.synchronize()

settingsVersion = 2
}
catch {
// Error encoding json, don't write new value

0 comments on commit 09b6c6c

Please sign in to comment.
You can’t perform that action at this time.