0

In my SwiftUI view below, I would like my alert to update with the countdown variable which controls whether or not the Proceed button is active & also shows up in the text of the alert itself.

The current implementation does not update the value of countdown - it always remains as 5.

struct RedemptionView: View { @Environment(\.dismiss) var dismiss @State private var showQRCodeScanner = false @State private var showPasscodeView = false @State private var billAmount: String = "" @State private var isShowingRedemptionStepView = false @State private var showAlert = false @State fileprivate var countdown: Int = 5 var allowsDelitePointCollection: Bool var voucherTitle: String var discountId: Int var body: some View { VStack { DismissButton() VStack(spacing: 16) { Text("Scan the QR code provided by the staff, or pass your phone to them to enter the staff code.") .font(.subheadline) .multilineTextAlignment(.center) .padding(.horizontal) .fixedSize(horizontal: false, vertical: true) Button(action: { if !billAmount.isEmpty { showAlert = true startCountdown() } else { showQRCodeScanner = true } }) { Text("Scan QR Code") .font(.headline) .padding() .frame(maxWidth: .infinity) .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) } .padding(.horizontal) } .padding(.bottom) Spacer() } .onTapGesture { hideKeyboard() } .padding(.top, 20) .sheet(isPresented: $showQRCodeScanner) { QRCodeScannerView(discountId: discountId, billAmount: billAmount, discountTitle: voucherTitle, isPresented: $isShowingRedemptionStepView) } .sheet(isPresented: $showPasscodeView) { PasscodeView(billAmount: billAmount, discountId: discountId, discountTitle: voucherTitle, isPresented: $isShowingRedemptionStepView) } .onChange(of: isShowingRedemptionStepView) { oldValue, newValue in if !newValue && oldValue { dismiss() } } .alert("Verify Bill Amount - \(priceToStrFormat(price: (Int(billAmount) ?? 0) * 100, useComma: false))", isPresented: $showAlert) { Button("Proceed\(countdown > 0 ? " In \(countdown)s" : "")") { if countdown == 0 { showQRCodeScanner = true } }.disabled(countdown > 0) Button("Cancel", role: .cancel) { } } message: { Text("Show Phone to Staff to verify bill amount. Proceed in \(countdown) seconds") } } private func startCountdown() { Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in if countdown > 0 { countdown -= 1 } else { timer.invalidate() } } } } 
2

1 Answer 1

0

The previous API (Alert) for alerts in SwiftUI used to support this, but that is now deprecated. The new modifier(alert(_:isPresented:presenting:actions:message:)) added to replace Alert doesn't have capability to update presented alert's content.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.