Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use .task instead of .onAppear/.onDisappear #836

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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