From b742d08d9f03da1fad0b8fb59904162f827938e3 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Sun, 20 Oct 2024 13:19:45 -0700 Subject: [PATCH] Adds NetworkTracker to watch, adds Watch Sitemap selection in main settings. Signed-off-by: Dan Cunningham --- .../OpenHABCore/Util/NetworkTracker.swift | 1 + openHAB/OpenHABSitemapViewController.swift | 2 ++ openHAB/SettingsView.swift | 36 +++++++++++++++++++ openHABWatch/Domain/UserData.swift | 17 ++++++++- 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/OpenHABCore/Sources/OpenHABCore/Util/NetworkTracker.swift b/OpenHABCore/Sources/OpenHABCore/Util/NetworkTracker.swift index 21b7a5ab..2ada699f 100644 --- a/OpenHABCore/Sources/OpenHABCore/Util/NetworkTracker.swift +++ b/OpenHABCore/Sources/OpenHABCore/Util/NetworkTracker.swift @@ -71,6 +71,7 @@ public final class NetworkTracker: ObservableObject { public func startTracking(connectionConfigurations: [ConnectionConfiguration], username: String, password: String, alwaysSendBasicAuth: Bool) { self.connectionConfigurations = connectionConfigurations httpClient = HTTPClient(username: username, password: password, alwaysSendBasicAuth: alwaysSendBasicAuth) + setActiveConnection(nil) attemptConnection() } diff --git a/openHAB/OpenHABSitemapViewController.swift b/openHAB/OpenHABSitemapViewController.swift index 70e30d8a..56a45262 100644 --- a/openHAB/OpenHABSitemapViewController.swift +++ b/openHAB/OpenHABSitemapViewController.swift @@ -179,6 +179,8 @@ class OpenHABSitemapViewController: OpenHABViewController, GenericUITableViewCel case .notConnected: os_log("Tracking error", log: .viewCycle, type: .info) self.showPopupMessage(seconds: 60, title: NSLocalizedString("error", comment: ""), message: NSLocalizedString("network_not_available", comment: ""), theme: .error) + case .connected: + self.hidePopupMessages() case _: break } diff --git a/openHAB/SettingsView.swift b/openHAB/SettingsView.swift index 44b24745..4dcdf269 100644 --- a/openHAB/SettingsView.swift +++ b/openHAB/SettingsView.swift @@ -33,6 +33,7 @@ struct SettingsView: View { @State var settingsSortSitemapsBy: SortSitemapsOrder = .label @State var settingsDefaultMainUIPath = "" @State var settingsAlwaysAllowWebRTC = true + @State var settingsSitemapForWatch = "" @State private var showingCacheAlert = false @State private var showCrashReportingAlert = false @@ -40,6 +41,8 @@ struct SettingsView: View { @State private var hasBeenLoaded = false + @State private var sitemaps: [OpenHABSitemap] = [] + @Environment(\.dismiss) private var dismiss var appData: OpenHABDataObject? { @@ -154,6 +157,7 @@ struct SettingsView: View { // Setting .onAppear of view required here because onAppear of entire view is run after onChange is active // when migrating to iOS17 this settingsSendCrashReports = Preferences.sendCrashReports + loadSitemaps() hasBeenLoaded = true } .onChange(of: settingsSendCrashReports) { newValue in @@ -273,6 +277,14 @@ struct SettingsView: View { } label: { Text("Sort sitemaps by") } + + Picker(selection: $settingsSitemapForWatch) { + ForEach(sitemaps, id: \.name) { sitemap in + Text(sitemap.name).tag(sitemap as OpenHABSitemap?) + } + } label: { + Text("Sitemap For Apple Watch") + } } Section(header: Text(LocalizedStringKey("about_settings"))) { @@ -344,6 +356,7 @@ struct SettingsView: View { settingsSortSitemapsBy = SortSitemapsOrder(rawValue: Preferences.sortSitemapsby) ?? .label settingsDefaultMainUIPath = Preferences.defaultMainUIPath settingsAlwaysAllowWebRTC = Preferences.alwaysAllowWebRTC + settingsSitemapForWatch = Preferences.sitemapForWatch } func saveSettings() { @@ -361,10 +374,33 @@ struct SettingsView: View { Preferences.sortSitemapsby = settingsSortSitemapsBy.rawValue Preferences.defaultMainUIPath = settingsDefaultMainUIPath Preferences.alwaysAllowWebRTC = settingsAlwaysAllowWebRTC + Preferences.sitemapForWatch = settingsSitemapForWatch WatchMessageService.singleton.syncPreferencesToWatch() Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(settingsSendCrashReports) logger.debug("setCrashlyticsCollectionEnabled to \(settingsSendCrashReports)") } + + private func loadSitemaps() { + NetworkConnection.sitemaps(openHABRootUrl: appData?.openHABRootUrl ?? "") { response in + switch response.result { + case let .success(data): + os_log("Sitemap response", log: .viewCycle, type: .info) + + sitemaps = deriveSitemaps(data) + + if sitemaps.last?.name == "_default", sitemaps.count > 1 { + sitemaps = Array(sitemaps.dropLast()) + } + + switch SortSitemapsOrder(rawValue: Preferences.sortSitemapsby) ?? .label { + case .label: sitemaps.sort { $0.label < $1.label } + case .name: sitemaps.sort { $0.name < $1.name } + } + case let .failure(error): + os_log("%{PUBLIC}@", log: .default, type: .error, error.localizedDescription) + } + } + } } extension UIApplication { diff --git a/openHABWatch/Domain/UserData.swift b/openHABWatch/Domain/UserData.swift index de1ed923..c1119a9b 100644 --- a/openHABWatch/Domain/UserData.swift +++ b/openHABWatch/Domain/UserData.swift @@ -68,6 +68,7 @@ final class UserData: ObservableObject { } init(sitemapName: String = "watch") { + updateNetworkTracker() NetworkTracker.shared.$activeConnection .receive(on: DispatchQueue.main) .sink { [weak self] activeConnection in @@ -93,11 +94,25 @@ final class UserData: ObservableObject { ObservableOpenHABDataObject.shared.objectRefreshed.sink { _ in // New settings updates from the phone app to start a reconnect self.logger.info("Settings update received, starting reconnect") - self.refreshUrl() + self.updateNetworkTracker() } .store(in: &cancellables) } + func updateNetworkTracker() { + if !ObservableOpenHABDataObject.shared.localUrl.isEmpty || !ObservableOpenHABDataObject.shared.remoteUrl.isEmpty { + let connection1 = ConnectionConfiguration( + url: ObservableOpenHABDataObject.shared.localUrl, + priority: 0 + ) + let connection2 = ConnectionConfiguration( + url: ObservableOpenHABDataObject.shared.remoteUrl, + priority: 1 + ) + NetworkTracker.shared.startTracking(connectionConfigurations: [connection1, connection2], username: ObservableOpenHABDataObject.shared.openHABUsername, password: ObservableOpenHABDataObject.shared.openHABPassword, alwaysSendBasicAuth: ObservableOpenHABDataObject.shared.openHABAlwaysSendCreds) + } + } + func loadPage(url: URL?, longPolling: Bool, refresh: Bool) {