Skip to content

Commit

Permalink
Adds NetworkTracker to watch, adds Watch Sitemap selection in main se…
Browse files Browse the repository at this point in the history
…ttings.

Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan committed Oct 20, 2024
1 parent 7e9f0fe commit b742d08
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions OpenHABCore/Sources/OpenHABCore/Util/NetworkTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 2 additions & 0 deletions openHAB/OpenHABSitemapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions openHAB/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ 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
@State private var showUselastPathAlert = false

@State private var hasBeenLoaded = false

@State private var sitemaps: [OpenHABSitemap] = []

@Environment(\.dismiss) private var dismiss

var appData: OpenHABDataObject? {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"))) {
Expand Down Expand Up @@ -344,6 +356,7 @@ struct SettingsView: View {
settingsSortSitemapsBy = SortSitemapsOrder(rawValue: Preferences.sortSitemapsby) ?? .label
settingsDefaultMainUIPath = Preferences.defaultMainUIPath
settingsAlwaysAllowWebRTC = Preferences.alwaysAllowWebRTC
settingsSitemapForWatch = Preferences.sitemapForWatch
}

func saveSettings() {
Expand All @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion openHABWatch/Domain/UserData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit b742d08

Please sign in to comment.