Skip to content

Commit

Permalink
Update of SwiftUI (#836)
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Bert <[email protected]>
Co-authored-by: Dan Cunningham <[email protected]>
  • Loading branch information
timbms and digitaldan authored Oct 3, 2024
1 parent c69b28b commit e9676dc
Showing 1 changed file with 26 additions and 37 deletions.
63 changes: 26 additions & 37 deletions openHAB/DrawerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//
// SPDX-License-Identifier: EPL-2.0

import Combine
import Kingfisher
import OpenHABCore
import os.log
Expand All @@ -36,12 +35,6 @@ func deriveSitemaps(_ response: Data?) -> [OpenHABSitemap] {
return sitemaps
}

struct UiTile: Decodable {
var name: String
var url: String
var imageUrl: String
}

struct ImageView: View {
let url: String

Expand Down Expand Up @@ -69,11 +62,35 @@ struct ImageView: View {
}
}

// Display the connected URL
struct ConnectionView: View {
@ObservedObject private var networkTracker = NetworkTracker.shared

var body: some View {
HStack {
if let activeConnection = networkTracker.activeConnection {
Image(systemSymbol: .cloudFill)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
Text(activeConnection.configuration.url).font(.footnote)
} else {
Image(systemSymbol: .exclamationmarkIcloudFill)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
Text("connecting").font(.footnote)
}
}
}
}

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
@ObservedObject private var networkTracker = NetworkTracker.shared

var openHABUsername = ""
var openHABPassword = ""
Expand All @@ -90,9 +107,6 @@ struct DrawerView: View {
@ScaledMetric var tilesIconwidth = 20.0
@ScaledMetric var sitemapIconwidth = 20.0

// Combine cancellable
@State private var trackerCancellable: AnyCancellable?

var body: some View {
VStack {
List {
Expand Down Expand Up @@ -174,36 +188,11 @@ struct DrawerView: View {
.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()
}
ConnectionView()
.padding(.bottom, 5)
}
}

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

private func loadData() {
loadSitemaps()
loadUiTiles()
Expand Down

0 comments on commit e9676dc

Please sign in to comment.