Add watchOS support
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// meterios-watch
|
||||
//
|
||||
// (c) 2025 Martin "maride" Dessauer
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
private var wcMgr: WCManager
|
||||
|
||||
@State private var drinks: Array<Drink> = []
|
||||
|
||||
@State private var showUpdateError: Bool = false
|
||||
@State private var updateError: String = ""
|
||||
@State private var updateErrorCount: Int = 0
|
||||
|
||||
init(_ wcMgr: WCManager) {
|
||||
self.wcMgr = wcMgr
|
||||
}
|
||||
|
||||
// update performs a full update from the paired device, covering connection info (user ID and backend address) and list of drinks
|
||||
func update() {
|
||||
if updateErrorCount >= 3 {
|
||||
showUpdateError = true
|
||||
updateErrorCount = 0
|
||||
return
|
||||
}
|
||||
|
||||
// Update drinks list from paired device
|
||||
wcMgr.requestDrinksList(
|
||||
replyHandler: { drinks in
|
||||
// Drinks received from backend
|
||||
self.drinks = drinks
|
||||
showUpdateError = false
|
||||
}, errorHandler: { error in
|
||||
// Error receiving drinks
|
||||
print("Error receiving drinks: \(error)")
|
||||
updateError = error?.localizedDescription ?? "Unknown error"
|
||||
updateErrorCount += 1
|
||||
update()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
if drinks.count > 0 {
|
||||
NavigationView {
|
||||
List() {
|
||||
// Drinks
|
||||
Section("Drinks") {
|
||||
ForEach(drinks) { drink in
|
||||
NavigationLink(drink.name) {
|
||||
DrinkDetailView(wcMgr: wcMgr, drink: drink)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Controls
|
||||
Section {
|
||||
Button("Refresh") {
|
||||
drinks = []
|
||||
update()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ProgressView()
|
||||
}
|
||||
}
|
||||
.onAppear(perform: update)
|
||||
// Alert to communicate an error in communication with the paired device
|
||||
.alert("Connection Error", isPresented: $showUpdateError) {
|
||||
Text(updateError).tint(.red).font(.footnote)
|
||||
Button("Retry", systemImage: "arrow.clockwise", action: update)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user