Add watchOS support

This commit is contained in:
2025-04-04 18:23:12 +02:00
parent 96b0466deb
commit 384ee97cb7
20 changed files with 857 additions and 48 deletions
@@ -0,0 +1,80 @@
//
// TransactionView.swift
// meterios-watch
//
// (c) 2025 Martin "maride" Dessauer
//
import SwiftUI
struct TransactionView: View {
private var wcMgr: WCManager
private var drinkID: Int
@State var statusLine = "Booking..."
@Environment(\.dismiss) private var dismiss
// If the backend reacts too fast (we hate that, huh?), the user is confused because of the short life of this view
// closeTimer takes care of that confusion and gives the user the good old 56k modem feeling.
@State var closeTimer: Timer?
@State var closeTimerReady: Bool = false
@State var backendBookTimer: Timer?
@State var backendTimerReady: Bool = false
init(wcMgr: WCManager, drinkID: Int) {
self.wcMgr = wcMgr
self.drinkID = drinkID
}
// maybeDismiss dismisses this view after the backend call(s) finished AND the timer(s) ran out
func maybeDismiss() {
if closeTimerReady && backendTimerReady {
dismiss()
}
}
// Books the drinks handed over to this view
func book() {
// Try to book the drink(s)
wcMgr.bookDrink(drinkID: drinkID) { salut in
statusLine = salut
backendTimerReady = true
maybeDismiss()
} errorHandler: { error in
statusLine = error!.localizedDescription
print("Error booking drinks: \(error)")
}
}
var body: some View {
// Green checkmark or progress indicator
VStack {
if backendTimerReady {
Image(systemName: "checkmark.circle.fill")
.foregroundStyle(.green)
} else {
ProgressView()
}
// Status line, on success with salute
Text(statusLine)
.font(.title)
// Let watch vibrate on success
if #available(watchOS 10.0, *) {
Text("")
.opacity(0)
.sensoryFeedback(.success, trigger: backendTimerReady)
}
}.onAppear(perform: {
closeTimer = Timer.scheduledTimer(withTimeInterval: 1.5, repeats: false) { timer in
closeTimerReady = true
maybeDismiss()
}
backendBookTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { timer in
book()
}
})
}
}