Skip to content

Commit

Permalink
Clean up tracker a bit more, remove empty test, add tacker indicator …
Browse files Browse the repository at this point in the history
…to sidemenu

Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan committed Sep 18, 2024
1 parent ff8bfb2 commit f9e1eea
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 150 deletions.
26 changes: 12 additions & 14 deletions OpenHABCore/Sources/OpenHABCore/Util/NetworkTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public struct ConnectionObject: Equatable {
self.url = url
self.priority = priority
}

public static func == (lhs: ConnectionObject, rhs: ConnectionObject) -> Bool {
lhs.url == rhs.url && lhs.priority == rhs.priority
}
}

public final class NetworkTracker: ObservableObject {
Expand All @@ -49,6 +45,9 @@ public final class NetworkTracker: ObservableObject {
private var retryTimer: DispatchSourceTimer?
private let timerQueue = DispatchQueue(label: "com.openhab.networktracker.timerQueue")

private let connectedRetryInterval: TimeInterval = 60 // amount of time we scan for better connections when connected
private let disconnectedRetryInterval: TimeInterval = 30 // amount of time we scan when not connected

private init() {
monitor = NWPathMonitor()
monitor.pathUpdateHandler = { [weak self] path in
Expand Down Expand Up @@ -201,17 +200,16 @@ public final class NetworkTracker: ObservableObject {
}
}

private func setActiveServer(_ server: ConnectionObject? = nil) {
private func setActiveServer(_ server: ConnectionObject?) {
os_log("Network status: setActiveServer: %{PUBLIC}@", log: OSLog.default, type: .info, server?.url ?? "no server")
if activeServer != server {
activeServer = server
if let activeServer {
updateStatus(.connected)
startRetryTimer(60) // check every 60 seconds to see if a better server is available.
} else {
updateStatus(.notConnected)
startRetryTimer(30) // check every 30 seconds to see if a server is available.
}
guard activeServer != server else { return }
activeServer = server
if activeServer != nil {
updateStatus(.connected)
startRetryTimer(connectedRetryInterval)
} else {
updateStatus(.notConnected)
startRetryTimer(disconnectedRetryInterval)
}
}

Expand Down
69 changes: 0 additions & 69 deletions OpenHABCore/Tests/OpenHABCoreTests/ItemCacheTests.swift

This file was deleted.

156 changes: 89 additions & 67 deletions openHAB/DrawerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//
// SPDX-License-Identifier: EPL-2.0

import Combine
import Kingfisher
import OpenHABCore
import os.log
Expand Down Expand Up @@ -72,6 +73,7 @@ struct DrawerView: View {
@State private var sitemaps: [OpenHABSitemap] = []
@State private var uiTiles: [OpenHABUiTile] = []
@State private var selectedSection: Int?
@State private var connectedUrl: String = "Not connected" // Default label text

var openHABUsername = ""
var openHABPassword = ""
Expand All @@ -88,98 +90,126 @@ struct DrawerView: View {
@ScaledMetric var tilesIconwidth = 20.0
@ScaledMetric var sitemapIconwidth = 20.0

var body: some View {
List {
Section(header: Text("Main")) {
HStack {
Image("openHABIcon")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: openHABIconwidth)
Text("Home")
}
.onTapGesture {
dismiss()
onDismiss(.webview)
}
}
// Combine cancellable
@State private var trackerCancellable: AnyCancellable?

Section(header: Text("Tiles")) {
ForEach(uiTiles, id: \.url) { tile in
var body: some View {
VStack {
List {
Section(header: Text("Main")) {
HStack {
ImageView(url: tile.imageUrl)
Image("openHABIcon")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: tilesIconwidth)
Text(tile.name)
.frame(width: openHABIconwidth)
Text("Home")
}
.onTapGesture {
dismiss()
onDismiss(.tile(tile.url))
onDismiss(.webview)
}
}
}

Section(header: Text("Sitemaps")) {
ForEach(sitemaps, id: \.name) { sitemap in
HStack {
let url = Endpoint.iconForDrawer(rootUrl: appData?.openHABRootUrl ?? "", icon: sitemap.icon).url
KFImage(url).placeholder { Image("openHABIcon").resizable() }
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: sitemapIconwidth)
Text(sitemap.label)
}
.onTapGesture {
dismiss()
onDismiss(.sitemap(sitemap.name))
Section(header: Text("Tiles")) {
ForEach(uiTiles, id: \.url) { tile in
HStack {
ImageView(url: tile.imageUrl)
.aspectRatio(contentMode: .fit)
.frame(width: tilesIconwidth)
Text(tile.name)
}
.onTapGesture {
dismiss()
onDismiss(.tile(tile.url))
}
}
}
}

Section(header: Text("System")) {
HStack {
Image(systemSymbol: .gear)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: openHABIconwidth)
Text(LocalizedStringKey("settings"))
}
.onTapGesture {
dismiss()
onDismiss(.settings)
Section(header: Text("Sitemaps")) {
ForEach(sitemaps, id: \.name) { sitemap in
HStack {
let url = Endpoint.iconForDrawer(rootUrl: appData?.openHABRootUrl ?? "", icon: sitemap.icon).url
KFImage(url).placeholder { Image("openHABIcon").resizable() }
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: sitemapIconwidth)
Text(sitemap.label)
}
.onTapGesture {
dismiss()
onDismiss(.sitemap(sitemap.name))
}
}
}

// check if we are using my.openHAB, add notifications menu item then
// Actually this should better test whether the host of the remoteUrl is on openhab.org
if Preferences.remoteUrl.contains("openhab.org"), !Preferences.demomode {
Section(header: Text("System")) {
HStack {
Image(systemSymbol: .bell)
Image(systemSymbol: .gear)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: openHABIconwidth)
Text(LocalizedStringKey("notifications"))
Text(LocalizedStringKey("settings"))
}
.onTapGesture {
dismiss()
onDismiss(.notifications)
onDismiss(.settings)
}

if Preferences.remoteUrl.contains("openhab.org"), !Preferences.demomode {
HStack {
Image(systemSymbol: .bell)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: openHABIconwidth)
Text(LocalizedStringKey("notifications"))
}
.onTapGesture {
dismiss()
onDismiss(.notifications)
}
}
}
}
.listStyle(.inset)
.onAppear(perform: loadData)

Spacer()

// Display the connected URL
HStack {
Image(systemName: "cloud.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
Text(connectedUrl)
.font(.footnote)
}
.padding(.bottom, 5)
.onAppear(perform: trackActiveServer)
.onDisappear {
trackerCancellable?.cancel()
}
}
.listStyle(.inset)
.onAppear(perform: loadData)
}

private func trackActiveServer() {
trackerCancellable = NetworkTracker.shared.$activeServer
.receive(on: DispatchQueue.main)
.sink { activeServer in
if let activeServer {
connectedUrl = activeServer.url
} else {
connectedUrl = NSLocalizedString("connecting", comment: "")
}
}
}

private func loadData() {
// TODO: Replace network calls with appropriate @EnvironmentObject or other state management
loadSitemaps()
loadUiTiles()
}

private func loadSitemaps() {
// Perform network call to load sitemaps and decode
// Update the sitemaps state

NetworkConnection.sitemaps(openHABRootUrl: appData?.openHABRootUrl ?? "") { response in
switch response.result {
case let .success(data):
Expand All @@ -191,7 +221,6 @@ struct DrawerView: View {
sitemaps = Array(sitemaps.dropLast())
}

// Sort the sitemaps according to Settings selection.
switch SortSitemapsOrder(rawValue: Preferences.sortSitemapsby) ?? .label {
case .label: sitemaps.sort { $0.label < $1.label }
case .name: sitemaps.sort { $0.name < $1.name }
Expand All @@ -203,8 +232,6 @@ struct DrawerView: View {
}

private func loadUiTiles() {
// Perform network call to load UI Tiles and decode
// Update the uiTiles state
NetworkConnection.uiTiles(openHABRootUrl: appData?.openHABRootUrl ?? "") { response in
switch response.result {
case .success:
Expand All @@ -223,11 +250,6 @@ struct DrawerView: View {
}
}
}

mutating func loadSettings() {
openHABUsername = Preferences.username
openHABPassword = Preferences.password
}
}

#Preview {
Expand Down

0 comments on commit f9e1eea

Please sign in to comment.