Skip to content

Commit

Permalink
static var to static let (#2642)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

- `static let` plays nicely with modern Swift concurrency (async/await)
- `static let` makes it impossible to mutate the values - causing
undefined behavior in some cases
  • Loading branch information
michalsrutek authored Mar 25, 2024
1 parent ecca604 commit 7de47c6
Show file tree
Hide file tree
Showing 23 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions HomeAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4834,7 +4834,7 @@
);
mainGroup = B657A8DD1CA646EB00121384;
packageReferences = (
427692E12B98B82500F24321 /* XCLocalSwiftPackageReference "Sources/PushServer/SharedPush" */,
427692E12B98B82500F24321 /* XCLocalSwiftPackageReference "SharedPush" */,
);
productRefGroup = B657A8E71CA646EB00121384 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -8133,7 +8133,7 @@
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
427692E12B98B82500F24321 /* XCLocalSwiftPackageReference "Sources/PushServer/SharedPush" */ = {
427692E12B98B82500F24321 /* XCLocalSwiftPackageReference "SharedPush" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = Sources/PushServer/SharedPush;
};
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Scenes/SceneManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct SceneManagerPreSceneCompatibility {

class SceneManager {
// types too hard here
fileprivate static var activityUserInfoKeyResolver = "resolver"
fileprivate static let activityUserInfoKeyResolver = "resolver"

private struct PendingResolver {
private var handleBlock: (Any) -> Void
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Scenes/WindowScenesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Shared
import UIKit

final class WindowScenesManager {
static var shared = WindowScenesManager()
static let shared = WindowScenesManager()
private(set) var windowSizeObservers: [WindowSizeObserver] = []

func sceneDidBecomeActive(_ scene: UIWindowScene) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/App/Scenes/WindowSizeObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ final class WindowSizeObserver: NSObject {
}

enum ScenesWindowSizeConfig {
static var defaultSceneLastestSystemFrameDataKey = "default-scene-latest-system-frame-data"
private static let defaultSceneLatestSystemFrameDataKey = "default-scene-latest-system-frame-data"
private static var defaultSceneLatestSystemFrameData: Data? {
get {
prefs.data(forKey: ScenesWindowSizeConfig.defaultSceneLastestSystemFrameDataKey)
prefs.data(forKey: ScenesWindowSizeConfig.defaultSceneLatestSystemFrameDataKey)
}
set {
prefs.set(newValue, forKey: ScenesWindowSizeConfig.defaultSceneLastestSystemFrameDataKey)
prefs.set(newValue, forKey: ScenesWindowSizeConfig.defaultSceneLatestSystemFrameDataKey)
}
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/App/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class SettingsViewController: HAFormViewController {
init(rawValue: Int) { self.rawValue = rawValue }
init(integerLiteral value: IntegerLiteralType) { self.init(rawValue: value) }

static var servers: ContentSection = 0b1
static var general: ContentSection = 0b10
static var integrations: ContentSection = 0b100
static var help: ContentSection = 0b1000
static var all = ContentSection(rawValue: ~0b0)
static let servers: ContentSection = 0b1
static let general: ContentSection = 0b10
static let integrations: ContentSection = 0b100
static let help: ContentSection = 0b1000
static let all = ContentSection(rawValue: ~0b0)
}

let contentSections: ContentSection
Expand Down
1 change: 0 additions & 1 deletion Sources/App/WebView/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKUIDeleg
private let refreshControl = UIRefreshControl()
private let sidebarGestureRecognizer: UIScreenEdgePanGestureRecognizer

private var keepAliveTimer: Timer?
private var initialURL: URL?
private var barCodeScannerController: UIViewController?

Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/AppIntents/IntentActionAppEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Shared

@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
struct IntentActionAppEntity: AppEntity {
static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Action")
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Action")

struct IntentActionAppEntityQuery: EntityQuery, EntityStringQuery {
func entities(for identifiers: [IntentActionAppEntity.ID]) async throws -> [IntentActionAppEntity] {
Expand All @@ -25,7 +25,7 @@ struct IntentActionAppEntity: AppEntity {
}
}

static var defaultQuery = IntentActionAppEntityQuery()
static let defaultQuery = IntentActionAppEntityQuery()

var id: String // if your identifier is not a String, conform the entity to EntityIdentifierConvertible.
var displayString: String
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/AppIntents/PerformAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Shared
struct PerformAction: AppIntent, CustomIntentMigratedAppIntent, PredictableIntent {
static let intentClassName = "PerformActionIntent"

static var title: LocalizedStringResource = "Perform Action"
static var description = IntentDescription("Performs an action defined in the app")
static let title: LocalizedStringResource = "Perform Action"
static let description = IntentDescription("Performs an action defined in the app")

@Parameter(title: "Action")
var action: IntentActionAppEntity?
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/AppIntents/WidgetActionsAppIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct WidgetActionsAppIntent: AppIntent, WidgetConfigurationIntent, CustomInten
ProgressReportingIntent {
static let intentClassName = "WidgetActionsIntent"

static var title: LocalizedStringResource = "Actions"
static var description = IntentDescription("View and run actions")
static let title: LocalizedStringResource = "Actions"
static let description = IntentDescription("View and run actions")

@Parameter(
title: "Actions",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/Widgets/Actions/WidgetActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ struct LegacyWidgetActions: Widget {
}

enum WidgetActionSupportedFamilies {
static var families: [WidgetFamily] = [.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge]
static let families: [WidgetFamily] = [.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge]
}
2 changes: 1 addition & 1 deletion Sources/MacBridge/MacBridgeNetworkMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
import SystemConfiguration

class MacBridgeNetworkMonitor {
static var connectivityDidChangeNotification: Notification.Name = .init("ha_connectivityDidChange")
static let connectivityDidChangeNotification: Notification.Name = .init("ha_connectivityDidChange")

private let wifiClient: CWWiFiClient
private var scStore: SCDynamicStore!
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/API/Fixtures/ServerFixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Shared

enum ServerFixture {
static var standard = Server(identifier: "123", getter: {
static let standard = Server(identifier: "123", getter: {
.init(
name: "A Name",
connection: .init(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/API/HAAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class HomeAssistantAPI {
public static let didConnectNotification = Notification.Name(rawValue: "HomeAssistantAPIConnected")

public private(set) var manager: Alamofire.Session!
public static var unauthenticatedManager: Alamofire.Session = configureSessionManager()
public static let unauthenticatedManager: Alamofire.Session = configureSessionManager()

public let tokenManager: TokenManager
public var server: Server
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shared/API/MJPEGStreamer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Foundation
import UIKit

class MJPEGStreamerSessionDelegate: SessionDelegate {
static var didReceiveResponse: Notification.Name = .init(rawValue: "MJPEGStreamerSessionDelegateDidReceiveResponse")
static var taskUserInfoKey: AnyHashable = "taskUserInfoKey"
static let didReceiveResponse: Notification.Name = .init(rawValue: "MJPEGStreamerSessionDelegateDidReceiveResponse")
static let taskUserInfoKey: AnyHashable = "taskUserInfoKey"

// if/when alamofire also implements this again, we need to update to handle it as the breakpoint between images
override func urlSession(
Expand Down
6 changes: 3 additions & 3 deletions Sources/Shared/API/Models/ModelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class ModelManager: ServerObserver {
]
}

public static var defaults: [Self] = [
public static let defaults: [Self] = [
CleanupDefinition(
model: LocationHistoryEntry.self,
createdKey: #keyPath(LocationHistoryEntry.CreatedAt)
Expand Down Expand Up @@ -229,7 +229,7 @@ public class ModelManager: ServerObserver {
})
}

public static var defaults: [Self] = [
public static let defaults: [Self] = [
.states(domain: "zone", type: RLMZone.self),
.states(domain: "scene", type: RLMScene.self),
]
Expand Down Expand Up @@ -257,7 +257,7 @@ public class ModelManager: ServerObserver {
_ modelManager: ModelManager
) -> Promise<Void>

public static var defaults: [Self] = [
public static let defaults: [Self] = [
FetchDefinition(update: { api, queue, manager in
api.GetMobileAppConfig().then(on: queue) {
when(fulfilled: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/API/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public struct ServerInfo: Codable, Equatable {
}

public final class Server: Hashable, Comparable, CustomStringConvertible {
public static var historicId: Identifier<Server> = "historic"
public static let historicId: Identifier<Server> = "historic"

public let identifier: Identifier<Server>
public var info: ServerInfo {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/Common/ObjectMapperTransformers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open class HomeAssistantTimestampTransform: DateFormatterTransform {
}

public extension DateFormatter {
static var iso8601Milliseconds: DateFormatter = {
static let iso8601Milliseconds: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shared/Common/URLComponents+WidgetAuthenticity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public extension URL {
}

public extension URLComponents {
private static var authenticityName = "widgetAuthenticity"
private static var serverName = "server"
private static let authenticityName = "widgetAuthenticity"
private static let serverName = "server"

mutating func insertWidgetAuthenticity() {
queryItems = (queryItems ?? []) + [
Expand Down
1 change: 0 additions & 1 deletion Sources/Shared/Environment/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public extension Version {
static let canSendDeviceID: Version = .init(minor: 104)
static let pedometerIconsAvailable: Version = .init(minor: 105)
static let tagWebhookAvailable: Version = .init(minor: 114, prerelease: "b5")
static let tagPlatformTrigger: Version = .init(minor: 115, prerelease: "any0")
static let actionSyncing: Version = .init(minor: 115, prerelease: "any0")
static let localPushConfirm: Version = .init(major: 2021, minor: 10, prerelease: "any0")
static let externalBusCommandRestart: Version = .init(major: 2021, minor: 12, prerelease: "b6")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class LocalPushManager {
public let server: Server
public weak var delegate: LocalPushManagerDelegate?

public static var stateDidChange: Notification.Name = .init(rawValue: "LocalPushManagerStateDidChange")
public static let stateDidChange: Notification.Name = .init(rawValue: "LocalPushManagerStateDidChange")

public enum State: Equatable, Codable {
case establishing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
public struct PushProviderConfiguration: Codable {
public static var providerConfigurationKey = "ha_configurations"
public static let providerConfigurationKey = "ha_configurations"

public var serverIdentifier: Identifier<Server>
public var settingsKey: String
public let serverIdentifier: Identifier<Server>
public let settingsKey: String

public init(serverIdentifier: Identifier<Server>, settingsKey: String) {
self.serverIdentifier = serverIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ObjectMapper
import UserNotifications

public extension UNNotificationContent {
private static var separator: String = "@duplicate_identifier-"
private static let separator: String = "@duplicate_identifier-"

static func uncombinedAction(from identifier: String) -> String {
if identifier.contains(separator), let substring = identifier.components(separatedBy: separator).first {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Shared

@available(iOS 16.0, *)
final class CarPlayServersListTemplate: CarPlayTemplateProvider {
private(set) static var carPlayPreferredServerKey = "carPlay-server"
static let carPlayPreferredServerKey = "carPlay-server"

private let viewModel: CarPlayServerListViewModel

Expand Down

0 comments on commit 7de47c6

Please sign in to comment.