iOS/Sources/Shared/API/ConnectionInfo.swift
Zac West 5c104f76e9
Multi-server (#1906)
## Summary
Most, but not all, of the changes necessary to support multi-server throughout the app and all its features.

## Screenshots
| Light | Dark |
| ----- | ---- |
| ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 52 24](https://user-images.githubusercontent.com/74188/143670011-9b9905ac-1b5b-4a82-b9f3-1490465c4ec5.png) | ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 52 26](https://user-images.githubusercontent.com/74188/143670012-0080230a-8f68-4f34-9691-db9f5e825a83.png) |
| ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 52 30](https://user-images.githubusercontent.com/74188/143670015-ceeac558-e039-4639-a186-b5001ab418b8.png) | ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 52 29](https://user-images.githubusercontent.com/74188/143670016-d72bb69d-83f5-4197-a742-59d208467258.png) |
| ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 52 47](https://user-images.githubusercontent.com/74188/143670021-6c90c40f-c2f1-4a33-aad9-da6626e99d9d.png) | ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 52 45](https://user-images.githubusercontent.com/74188/143670024-e99de69d-61d8-4e12-be73-a172242806a0.png) |
| ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 53 05](https://user-images.githubusercontent.com/74188/143670033-1a41ac7e-d4d1-458b-974e-2efdaf8e2288.png) | ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 53 03](https://user-images.githubusercontent.com/74188/143670049-baf4db64-64db-4bfb-88cf-4930f9e5661b.png) |
| ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 53 21](https://user-images.githubusercontent.com/74188/143670053-7ec794f1-857c-4ef6-a92a-5318e90ac6b6.png) | ![Simulator Screen Shot - iPhone 13 Pro - 2021-11-26 at 21 53 19](https://user-images.githubusercontent.com/74188/143670056-a6a5207c-3bba-49fc-b5c6-fc6fa8141f9c.png) |

## Any other notes
- Encapsulates all connectivity, token & server-specific knowledge in a Server model object which gets passed around.
- Updates various places throughout the app to know about and use Server rather than accessing said information through non-server-specific methods.
- Visually requests/notes server in places where it's ambiguous. For example, the Open Page widget will gain a subtitle if multiple servers are set up.
- Allows switching which server is shown in the WebViews. Note that this doesn't take into account multi-window support on iPad/macOS yet.

Most things will migrate successfully however adding an additional server causes things like Shortcuts to start erroring requiring you specify which to use in the particular Shortcut.

Future work necessary:
- Model objects currently clobber each other if their identifiers match. For example, both servers having a zone named `home` means one of them wins the fight for which is known to the app.
- Being remotely logged out on any account causes the app to require onboarding again, when instead it should only do that if the last known server is logged out.
2021-11-27 12:33:46 -08:00

264 lines
8.2 KiB
Swift

import Alamofire
import Foundation
#if os(watchOS)
import Communicator
#endif
public struct ConnectionInfo: Codable, Equatable {
private var externalURL: URL?
private var internalURL: URL?
private var remoteUIURL: URL?
public var webhookID: String
public var webhookSecret: String?
public var useCloud: Bool = false
public var cloudhookURL: URL?
public var internalSSIDs: [String]? {
didSet {
overrideActiveURLType = nil
}
}
public var internalHardwareAddresses: [String]? {
didSet {
overrideActiveURLType = nil
}
}
public var canUseCloud: Bool {
remoteUIURL != nil
}
public var overrideActiveURLType: URLType?
public private(set) var activeURLType: URLType = .internal
public var isLocalPushEnabled = true {
didSet {
guard oldValue != isLocalPushEnabled else { return }
Current.Log.verbose("updated local push from \(oldValue) to \(isLocalPushEnabled)")
}
}
public init(
externalURL: URL?,
internalURL: URL?,
cloudhookURL: URL?,
remoteUIURL: URL?,
webhookID: String,
webhookSecret: String?,
internalSSIDs: [String]?,
internalHardwareAddresses: [String]?,
isLocalPushEnabled: Bool
) {
self.externalURL = externalURL
self.internalURL = internalURL
self.cloudhookURL = cloudhookURL
self.remoteUIURL = remoteUIURL
self.webhookID = webhookID
self.webhookSecret = webhookSecret
self.internalSSIDs = internalSSIDs
self.internalHardwareAddresses = internalHardwareAddresses
self.isLocalPushEnabled = isLocalPushEnabled
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.externalURL = try container.decodeIfPresent(URL.self, forKey: .externalURL)
self.internalURL = try container.decodeIfPresent(URL.self, forKey: .internalURL)
self.remoteUIURL = try container.decodeIfPresent(URL.self, forKey: .remoteUIURL)
self.webhookID = try container.decode(String.self, forKey: .webhookID)
self.webhookSecret = try container.decodeIfPresent(String.self, forKey: .webhookSecret)
self.cloudhookURL = try container.decodeIfPresent(URL.self, forKey: .cloudhookURL)
self.internalSSIDs = try container.decodeIfPresent([String].self, forKey: .internalSSIDs)
self.internalHardwareAddresses =
try container.decodeIfPresent([String].self, forKey: .internalHardwareAddresses)
self.useCloud = try container.decodeIfPresent(Bool.self, forKey: .useCloud) ?? false
self.isLocalPushEnabled = try container.decodeIfPresent(Bool.self, forKey: .isLocalPushEnabled) ?? true
}
public enum URLType: Int, Codable, CaseIterable, CustomStringConvertible, CustomDebugStringConvertible {
case `internal`
case remoteUI
case external
public var debugDescription: String {
switch self {
case .internal:
return "Internal URL"
case .remoteUI:
return "Remote UI"
case .external:
return "External URL"
}
}
public var description: String {
switch self {
case .internal:
return L10n.Settings.ConnectionSection.InternalBaseUrl.title
case .remoteUI:
return L10n.Settings.ConnectionSection.RemoteUiUrl.title
case .external:
return L10n.Settings.ConnectionSection.ExternalBaseUrl.title
}
}
public var isAffectedBySSID: Bool {
switch self {
case .internal: return true
case .remoteUI, .external: return false
}
}
public var isAffectedByCloud: Bool {
switch self {
case .internal: return false
case .remoteUI, .external: return true
}
}
public var isAffectedByHardwareAddress: Bool {
switch self {
case .internal: return Current.isCatalyst
case .remoteUI, .external: return false
}
}
public var hasLocalPush: Bool {
switch self {
case .internal:
if Current.isCatalyst {
return false
}
if #available(iOS 14, *) {
return true
} else {
return false
}
default: return false
}
}
}
/// Returns the url that should be used at this moment to access the Home Assistant instance.
public mutating func activeURL() -> URL {
if let overrideActiveURLType = overrideActiveURLType {
let overrideURL: URL?
switch overrideActiveURLType {
case .internal:
activeURLType = .internal
overrideURL = internalURL
case .remoteUI:
activeURLType = .remoteUI
overrideURL = remoteUIURL
case .external:
activeURLType = .external
overrideURL = externalURL
}
if let overrideURL = overrideURL {
return overrideURL.sanitized()
}
}
let url: URL
if let internalURL = internalURL, isOnInternalNetwork || overrideActiveURLType == .internal {
activeURLType = .internal
url = internalURL
} else if let remoteUIURL = remoteUIURL, useCloud {
activeURLType = .remoteUI
url = remoteUIURL
} else if let externalURL = externalURL {
activeURLType = .external
url = externalURL
} else {
activeURLType = .internal
url = URL(string: "http://homeassistant.local:8123")!
}
return url.sanitized()
}
/// Returns the activeURL with /api appended.
public mutating func activeAPIURL() -> URL {
activeURL().appendingPathComponent("api", isDirectory: false)
}
public mutating func webhookURL() -> URL {
if useCloud, let cloudURL = cloudhookURL {
return cloudURL
}
return activeURL().appendingPathComponent(webhookPath, isDirectory: false)
}
public var webhookPath: String {
"api/webhook/\(webhookID)"
}
public func address(for addressType: URLType) -> URL? {
switch addressType {
case .internal: return internalURL
case .external: return externalURL
case .remoteUI: return remoteUIURL
}
}
public mutating func set(address: URL?, for addressType: URLType) {
switch addressType {
case .internal:
internalURL = address
case .external:
externalURL = address
case .remoteUI:
remoteUIURL = address
}
}
/// Returns true if current SSID is SSID marked for internal URL use.
public var isOnInternalNetwork: Bool {
#if targetEnvironment(simulator)
return true
#else
if let current = Current.connectivity.currentWiFiSSID(),
internalSSIDs?.contains(current) == true {
return true
}
if let current = Current.connectivity.currentNetworkHardwareAddress(),
internalHardwareAddresses?.contains(current) == true {
return true
}
return false
#endif
}
}
class ServerRequestAdapter: RequestAdapter {
let server: Server
init(server: Server) {
self.server = server
}
func adapt(
_ urlRequest: URLRequest,
for session: Session,
completion: @escaping (Result<URLRequest, Error>) -> Void
) {
var updatedRequest: URLRequest = urlRequest
if let currentURL = urlRequest.url {
let expectedURL = server.info.connection.activeURL().adapting(url: currentURL)
if currentURL != expectedURL {
Current.Log.verbose("Changing request URL from \(currentURL) to \(expectedURL)")
updatedRequest.url = expectedURL
}
}
completion(.success(updatedRequest))
}
}