84 lines
2.6 KiB
Swift
84 lines
2.6 KiB
Swift
//
|
|
// TransactionView.swift
|
|
// meterios
|
|
//
|
|
// (c) 2025 Martin "maride" Dessauer
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct TransactionView: View {
|
|
@AppStorage("meteHostAddr") private var meteHostAddr = ""
|
|
@AppStorage("meteUserID") private var meteUserID = -1
|
|
|
|
var drinkID: Int
|
|
var drinkAmount: Int
|
|
|
|
@State var statusLine = String(localized: "CONTACTING_BACKEND")
|
|
|
|
@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(drinkID: Int, drinkAmount: Int) {
|
|
self.drinkID = drinkID
|
|
self.drinkAmount = drinkAmount
|
|
}
|
|
|
|
// 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() {
|
|
Task {
|
|
do {
|
|
// Try to book the drink(s)
|
|
for _ in 1...drinkAmount {
|
|
try await BackendConnector().BookDrink(baseAddr: meteHostAddr, userID: meteUserID, drinkID: drinkID)
|
|
}
|
|
statusLine = SalutManager.getSalute()
|
|
backendTimerReady = true
|
|
maybeDismiss()
|
|
} catch {
|
|
// Error occurred
|
|
statusLine = String(localized: "ERROR_IN_COMMUNICATION")
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
var body: some View {
|
|
// Green checkmark or progress indicator
|
|
if backendTimerReady {
|
|
Image(systemName: "checkmark.circle.fill")
|
|
.font(.title)
|
|
.foregroundStyle(.green)
|
|
} else {
|
|
ProgressView()
|
|
}
|
|
|
|
// Status line, on success with salute
|
|
Text(statusLine)
|
|
.font(.title)
|
|
.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()
|
|
}
|
|
})
|
|
.sensoryFeedback(.success, trigger: backendTimerReady)
|
|
}
|
|
}
|