// // TimerCountView.swift // XuXuCheck // // Created by henseycai on 2025/7/25. // import SwiftUI struct TimerCountView: View { @State var timeRemaining: Int = 60 @State var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() @Binding var canStartScan: Bool @Environment(\.dismiss) private var dissmiss @Environment(CameraViewModel.self) private var cameraViewModel var body: some View { let bgcolor = Color(hex: 0x2449f1) let bgcolor2 = Color(hex: 0x1638d9) VStack{ HStack { Image(systemName: "arrow.backward") .frame(width: 50, height: 50) .foregroundColor(.primary) .strokeStyle(cornerRadius: 15) .onTapGesture { dissmiss() } Text("Countdown Timer").font(.headline).frame(maxWidth: .infinity, alignment: .center) } ZStack { // Digit Counter Text( "\(timeRemaining)s" ) .font(.system(size: 56, weight: .bold)) // Circle Counter Group { Circle() .rotation(.degrees(-90)) .stroke( bgcolor.opacity(0.3), style: StrokeStyle(lineWidth: 12, dash: [CGFloat.pi / 2, CGFloat.pi * 3.5]) ) Circle() .trim( from: 0, to: (CGFloat(timeRemaining).truncatingRemainder(dividingBy: 60) - 0.25) / 60 ) .rotation(.degrees(-90)) .stroke( bgcolor2.opacity(0.8), style: StrokeStyle(lineWidth: 12, dash: [CGFloat.pi / 2, CGFloat.pi * 3.5]) ) } .frame(width: 240, height: 240) }.onReceive(timer){ _ in if timeRemaining > 0 { // if timeremaining is greater than zero subtract 1 timeRemaining -= 1 } else { //else reset it to 5 if its equal to 0. this will save it from a negative num timeRemaining = 60 self.timer.upstream.connect().cancel() cameraViewModel.startSession() canStartScan = true //dissmiss() } } Text("Urine chemical reaction needs one minute about. Please put strip on white paper towel.").foregroundStyle(.primary) .font(.title2).frame(maxWidth: .infinity, alignment: .center) .offset(x:20,y:100) ///----------------------------- Spacer() HStack{ Button(action: { timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() timeRemaining = 60 }, label: { Text("Reset Timer") .frame(width: 120) .padding() .foregroundColor(.white) .background(Color.gray) .cornerRadius(10) }) Spacer() Button(action: { self.timer.upstream.connect().cancel() cameraViewModel.startSession() canStartScan = true }, label: { Text("Scan Now") .frame(width: 120) .padding() .foregroundColor(.white) .background(Color.blue) .cornerRadius(10) }) } }.padding(50) } }