From b5d1ed8c2be6cd33d2a7f2df13d9cdb2a55d0f46 Mon Sep 17 00:00:00 2001 From: Luis Lopes Date: Mon, 17 Apr 2023 12:14:21 +0100 Subject: [PATCH] Fixed lint warnings --- Sources/App/AppDelegate.swift | 4 +- Sources/App/Scenes/CarPlaySceneDelegate.swift | 55 ++++++++----------- .../Extensions/HAEntityExtension.swift | 34 +++++------- .../Extensions/ServerManagerExtension.swift | 16 ++---- .../Templates/DomainsListTemplate.swift | 30 ++++------ .../Templates/EntitiesGridTemplate.swift | 28 ++++------ 6 files changed, 67 insertions(+), 100 deletions(-) diff --git a/Sources/App/AppDelegate.swift b/Sources/App/AppDelegate.swift index 4f8e393e4..7db21096c 100644 --- a/Sources/App/AppDelegate.swift +++ b/Sources/App/AppDelegate.swift @@ -201,8 +201,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions ) -> UISceneConfiguration { - if #available(iOS 16.0, *), connectingSceneSession.role == UISceneSession.Role.carTemplateApplication { - let scene = UISceneConfiguration(name: "CarPlay", sessionRole: connectingSceneSession.role) + if #available(iOS 16.0, *), connectingSceneSession.role == UISceneSession.Role.carTemplateApplication { + let scene = UISceneConfiguration(name: "CarPlay", sessionRole: connectingSceneSession.role) scene.delegateClass = CarPlayDelegate.self return scene } else { diff --git a/Sources/App/Scenes/CarPlaySceneDelegate.swift b/Sources/App/Scenes/CarPlaySceneDelegate.swift index 97f2b0d1a..3eb9a5c2a 100644 --- a/Sources/App/Scenes/CarPlaySceneDelegate.swift +++ b/Sources/App/Scenes/CarPlaySceneDelegate.swift @@ -1,16 +1,9 @@ -// -// CarPlayDelegate.swift -// App -// -// Created by Luis Lopes on 15/02/2023. -// Copyright © 2023 Home Assistant. All rights reserved. -// - import CarPlay -import Shared +import Communicator import HAKit import PromiseKit -import Communicator +import Shared + public protocol EntitiesStateSubscription { func subscribe() @@ -18,17 +11,17 @@ public protocol EntitiesStateSubscription { } @available(iOS 16.0, *) -class CarPlayDelegate : UIResponder { +class CarPlayDelegate: UIResponder { public static let SUPPORTED_DOMAINS_WITH_STRING = [ - "button" : L10n.Carplay.Labels.buttons, - "cover" : L10n.Carplay.Labels.covers, - "input_boolean" : L10n.Carplay.Labels.inputBooleans, - "input_button" : L10n.Carplay.Labels.inputButtons, - "light" : L10n.Carplay.Labels.lights, - "lock" : L10n.Carplay.Labels.locks, - "scene" : L10n.Carplay.Labels.scenes, - "script" : L10n.Carplay.Labels.scripts, - "switch" : L10n.Carplay.Labels.switches + "button": L10n.Carplay.Labels.buttons, + "cover": L10n.Carplay.Labels.covers, + "input_boolean": L10n.Carplay.Labels.inputBooleans, + "input_button": L10n.Carplay.Labels.inputButtons, + "light": L10n.Carplay.Labels.lights, + "lock": L10n.Carplay.Labels.locks, + "scene": L10n.Carplay.Labels.scenes, + "script": L10n.Carplay.Labels.scripts, + "switch": L10n.Carplay.Labels.switches ] public let SUPPORTED_DOMAINS = SUPPORTED_DOMAINS_WITH_STRING.keys @@ -42,11 +35,11 @@ class CarPlayDelegate : UIResponder { private var interfaceController: CPInterfaceController? private var filteredEntities: [HAEntity] = [] - private var entitiesGridTemplate : EntitiesGridTemplate? - private var domainsListTemplate : DomainsListTemplate? - private var entitiesStateSubscribeCancelable : HACancellable? - private var serverObserver : HACancellable? - private var serverId : Identifier? { + private var entitiesGridTemplate: EntitiesGridTemplate? + private var domainsListTemplate: DomainsListTemplate? + private var entitiesStateSubscribeCancelable: HACancellable? + private var serverObserver: HACancellable? + private var serverId: Identifier? { didSet { loadEntities() } @@ -80,7 +73,7 @@ class CarPlayDelegate : UIResponder { } func getFilteredAndSortEntities(entities: [HAEntity]) -> [HAEntity] { - var tmpEntities : [HAEntity] = [] + var tmpEntities: [HAEntity] = [] for entity in entities where SUPPORTED_DOMAINS.contains(entity.domain) { tmpEntities.append(entity) @@ -134,7 +127,7 @@ class CarPlayDelegate : UIResponder { return } - let loginAlertAction : CPAlertAction = CPAlertAction(title: L10n.Carplay.Labels.alreadyAddedServer, style: .default) { _ in + let loginAlertAction: CPAlertAction = CPAlertAction(title: L10n.Carplay.Labels.alreadyAddedServer, style: .default) { _ in if !Current.servers.all.isEmpty { self.interfaceController?.dismissTemplate(animated: true) } @@ -144,7 +137,7 @@ class CarPlayDelegate : UIResponder { } func setDomainListTemplate() { - domainsListTemplate = DomainsListTemplate(title: L10n.About.Logo.appTitle, entities: filteredEntities, ic : interfaceController!, + domainsListTemplate = DomainsListTemplate(title: L10n.About.Logo.appTitle, entities: filteredEntities, ic: interfaceController!, listItemHandler: {[weak self] domain, entities in guard let self = self, let server = Current.servers.getServer(id: self.serverId) else { @@ -162,7 +155,7 @@ class CarPlayDelegate : UIResponder { } func setServerListTemplate() { - var serverList : [CPListItem] = [] + var serverList: [CPListItem] = [] for server in Current.servers.all { let serverItem = CPListItem(text: server.info.name, detailText: "\(server.info.connection.activeURLType.description) - \(server.info.connection.activeURL().absoluteString)") serverItem.handler = { [weak self] item, completion in @@ -182,7 +175,7 @@ class CarPlayDelegate : UIResponder { } @available(iOS 16.0, *) -extension CarPlayDelegate : CPTemplateApplicationSceneDelegate { +extension CarPlayDelegate: CPTemplateApplicationSceneDelegate { func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) { self.interfaceController = interfaceController self.interfaceController?.delegate = self @@ -247,7 +240,7 @@ extension CarPlayDelegate: CPInterfaceControllerDelegate { } @available(iOS 16.0, *) -extension CarPlayDelegate : ServerObserver { +extension CarPlayDelegate: ServerObserver { func serversDidChange(_ serverManager: ServerManager) { self.domainsListTemplate?.setServerListButton(show: Current.servers.all.count > 1) diff --git a/Sources/Vehicle/Extensions/HAEntityExtension.swift b/Sources/Vehicle/Extensions/HAEntityExtension.swift index 8719bc7a6..39fb89ad1 100644 --- a/Sources/Vehicle/Extensions/HAEntityExtension.swift +++ b/Sources/Vehicle/Extensions/HAEntityExtension.swift @@ -1,22 +1,18 @@ -// -// HAEntityExtentions.swift -// App -// -// Created by Luis Lopes on 27/02/2023. -// Copyright © 2023 Home Assistant. All rights reserved. -// - import Foundation import HAKit -import Shared import PromiseKit +import Shared import SwiftUI extension HAEntity { - public static func getIconForDomain(domain: String, size: CGSize) -> UIImage? { do { - let tmpEntity = try HAEntity(entityId: "\(domain).ha_ios_placeholder", domain: domain, state: "", lastChanged: Date(), lastUpdated: Date(), attributes: [:], context: HAResponseEvent.Context(id: "", userId: nil, parentId: nil)) + let tmpEntity = try HAEntity(entityId: "\(domain).ha_ios_placeholder", + domain: domain, state: "", + lastChanged: Date(), + lastUpdated: Date(), + attributes: [:], + context: HAResponseEvent.Context(id: "", userId: nil, parentId: nil)) return tmpEntity.getIcon(size: size) } catch { @@ -26,7 +22,7 @@ extension HAEntity { func onPress(for api: HomeAssistantAPI) -> Promise{ let domain = domain - var service : String + var service: String switch (domain) { case "lock": service = state == "unlocked" ? "lock" : "unlock" @@ -39,20 +35,20 @@ extension HAEntity { default: service = state == "on" ? "turn_off" : "turn_on"; } - return api.CallService(domain: domain, service: service, serviceData: ["entity_id" : entityId]) + return api.CallService(domain: domain, service: service, serviceData: ["entity_id": entityId]) } func getIcon(size: CGSize = CGSize(width: 64, height: 64), darkColor: UIColor = UIColor.white) -> UIImage?{ - var icon = attributes.icon ?? "" + let icon = attributes.icon ?? "" - var image : MaterialDesignIcons = MaterialDesignIcons.bookmarkIcon + var image: MaterialDesignIcons = MaterialDesignIcons.bookmarkIcon if icon.starts(with: "mdi:") { let mdiIcon = icon.components(separatedBy: ":")[1] let iconName = mdiIcon.replacingOccurrences(of: "-", with: "_") image = MaterialDesignIcons(named: iconName) } else { - var compareState = state + let compareState = state switch (domain) { case "button": guard let deviceClass = attributes.dictionary["device_class"] as? String else { break } @@ -118,7 +114,7 @@ extension HAEntity { image = MaterialDesignIcons.bookmarkIcon } } - var iconImage = image.image(ofSize: size, color: nil) + let iconImage = image.image(ofSize: size, color: nil) iconImage.imageAsset?.register(image.image(ofSize: size, color: darkColor), with: .init(userInterfaceStyle: .dark)) return iconImage } @@ -178,8 +174,8 @@ extension HAEntity { } func getFriendlyState() -> String { - var state = state - var friendlyState : String = state + let state = state + var friendlyState: String = state switch(state) { case "closed": friendlyState = L10n.State.closed diff --git a/Sources/Vehicle/Extensions/ServerManagerExtension.swift b/Sources/Vehicle/Extensions/ServerManagerExtension.swift index 9ddf87daa..0238db3c4 100644 --- a/Sources/Vehicle/Extensions/ServerManagerExtension.swift +++ b/Sources/Vehicle/Extensions/ServerManagerExtension.swift @@ -1,20 +1,12 @@ -// -// ServerManagerExtension.swift -// App -// -// Created by Luis Lopes on 06/03/2023. -// Copyright © 2023 Home Assistant. All rights reserved. -// - import Foundation import Shared -extension ServerManager { - public func isConnected() -> Bool { +public extension ServerManager { + func isConnected() -> Bool { return all.contains(where: { isConnected(server: $0) }) } - public func isConnected(server : Server) -> Bool{ + func isConnected(server: Server) -> Bool{ switch Current.api(for: server).connection.state { case .ready(version: _): return true @@ -23,7 +15,7 @@ extension ServerManager { } } - public func getServer(id : Identifier? = nil) -> Server? { + func getServer(id: Identifier? = nil) -> Server? { guard let id = id else { return all.first(where: {isConnected(server: $0)} ) } diff --git a/Sources/Vehicle/Templates/DomainsListTemplate.swift b/Sources/Vehicle/Templates/DomainsListTemplate.swift index 70499f55c..db32e203d 100644 --- a/Sources/Vehicle/Templates/DomainsListTemplate.swift +++ b/Sources/Vehicle/Templates/DomainsListTemplate.swift @@ -1,24 +1,16 @@ -// -// DomainListTemplate.swift -// App -// -// Created by Luis Lopes on 27/02/2023. -// Copyright © 2023 Home Assistant. All rights reserved. -// - -import Foundation import CarPlay +import Foundation import HAKit import Shared @available(iOS 16.0, *) class DomainsListTemplate { - private var title : String - private var listTemplate : CPListTemplate? - private var entities : [HAEntity] - private let listItemHandler : (String, [HAEntity]) -> Void + private var title: String + private var listTemplate: CPListTemplate? + private var entities: [HAEntity] + private let listItemHandler: (String, [HAEntity]) -> Void private var serverButtonHandler: CPBarButtonHandler? - private var domainList : [String] = [] + private var domainList: [String] = [] init(title: String, entities: [HAEntity], ic: CPInterfaceController, listItemHandler: @escaping (String, [HAEntity]) -> Void, @@ -39,12 +31,12 @@ class DomainsListTemplate { return listTemplate } - public func entitiesUpdate(updateEntities : [HAEntity]) { + public func entitiesUpdate(updateEntities: [HAEntity]) { entities = updateEntities updateSection() } - func setServerListButton(show : Bool) { + func setServerListButton(show: Bool) { if show { listTemplate?.trailingNavigationBarButtons = [CPBarButton(title: L10n.Carplay.Labels.servers, handler: serverButtonHandler)] } else { @@ -61,12 +53,14 @@ class DomainsListTemplate { return } - var items : [CPListItem] = [] + var items: [CPListItem] = [] for domain in domains { let itemTitle = CarPlayDelegate.SUPPORTED_DOMAINS_WITH_STRING[domain] ?? domain - let listItem = CPListItem(text: itemTitle, detailText: nil, image: HAEntity.getIconForDomain(domain: domain, size: CPListItem.maximumImageSize)) + let listItem = CPListItem(text: itemTitle, + detailText: nil, + image: HAEntity.getIconForDomain(domain: domain, size: CPListItem.maximumImageSize)) listItem.accessoryType = CPListItemAccessoryType.disclosureIndicator listItem.handler = { [weak self] item, completion in if let entitiesForSelectedDomain = self?.getEntitiesForDomain(domain: domain) { diff --git a/Sources/Vehicle/Templates/EntitiesGridTemplate.swift b/Sources/Vehicle/Templates/EntitiesGridTemplate.swift index 89882c5c8..dc4752a4b 100644 --- a/Sources/Vehicle/Templates/EntitiesGridTemplate.swift +++ b/Sources/Vehicle/Templates/EntitiesGridTemplate.swift @@ -1,26 +1,18 @@ -// -// EntityGridTemplate.swift -// App -// -// Created by Luis Lopes on 27/02/2023. -// Copyright © 2023 Home Assistant. All rights reserved. -// - -import Foundation import CarPlay +import Foundation import HAKit -import Shared import PromiseKit +import Shared @available(iOS 16.0, *) class EntitiesGridTemplate { - private let entityIconSize : CGSize = CGSize(width: 64, height: 64) - private var stateSubscriptionToken : HACancellable? - private let title : String - private let domain : String - private var server : Server - private var entities : [HAEntity] = [] + private let entityIconSize: CGSize = CGSize(width: 64, height: 64) + private var stateSubscriptionToken: HACancellable? + private let title: String + private let domain: String + private var server: Server + private var entities: [HAEntity] = [] private var gridTemplate: CPGridTemplate? private var gridPage: Int = 0 @@ -67,7 +59,7 @@ class EntitiesGridTemplate { } func getPageButtons() -> [CPBarButton] { - var barButtons : [CPBarButton] = [] + var barButtons: [CPBarButton] = [] if entities.count > CPGridTemplateMaximumItems { let maxPages = entities.count / CPGridTemplateMaximumItems if gridPage < maxPages { @@ -103,7 +95,7 @@ class EntitiesGridTemplate { } @available(iOS 16.0, *) -extension EntitiesGridTemplate : EntitiesStateSubscription { +extension EntitiesGridTemplate: EntitiesStateSubscription { public func subscribe() { stateSubscriptionToken = Current.api(for: server).connection.caches.states.subscribe { [self] cancellable, cachedStates in entities.removeAll { entity in