Compare commits

...

3 commits

Author SHA1 Message Date
d5d1c35b55 fix: gate debug print() statements behind IS_DEV flag
Wrapped all debug print() calls in APIService (avatar debugging),
BeaconScanner (scan/resolve logging), TaskDetailScreen (beacon state),
and AboutScreen (error logging) with IS_DEV checks so they are silent
in production builds. Preview-only prints in RatingDialog left as-is.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 22:12:13 +00:00
54923ba341 fix: add hasCompleted guard to prevent double-completion race condition
If a user confirms cash payment AND a beacon triggers auto-complete at the
same time, two completion calls could fire. Added @State hasCompleted flag
that gates all completion paths (manual complete, beacon auto-complete, and
cash collection). Resets on error/cancel so user can retry.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 22:10:56 +00:00
db7fe31b8a fix: standardize task polling interval from 2s to 5s (Android parity)
TaskListScreen, MyTasksScreen, and BusinessSelectionScreen all had 2-second
refresh timers. Changed to 5 seconds to match Android and reduce server load.
Chat polling (3s) left unchanged.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 22:10:07 +00:00
7 changed files with 32 additions and 23 deletions

View file

@ -691,14 +691,14 @@ actor APIService {
func getAvatarUrl() async throws -> String? { func getAvatarUrl() async throws -> String? {
let json = try await getJSON("/auth/avatar.php") let json = try await getJSON("/auth/avatar.php")
print("[Avatar] Response: \(json)") if IS_DEV { print("[Avatar] Response: \(json)") }
guard ok(json) else { guard ok(json) else {
print("[Avatar] Response not OK") if IS_DEV { print("[Avatar] Response not OK") }
return nil return nil
} }
let data = json["DATA"] as? [String: Any] ?? json let data = json["DATA"] as? [String: Any] ?? json
print("[Avatar] Data: \(data)") if IS_DEV { print("[Avatar] Data: \(data)") }
// Try all possible key variations for avatar URL // Try all possible key variations for avatar URL
let keys = ["AVATAR_URL", "AVATARURL", "AvatarUrl", "avatarUrl", "avatar_url", let keys = ["AVATAR_URL", "AVATARURL", "AvatarUrl", "avatarUrl", "avatar_url",
@ -707,16 +707,16 @@ actor APIService {
for key in keys { for key in keys {
if let url = data[key] as? String, !url.isEmpty { if let url = data[key] as? String, !url.isEmpty {
let resolved = Self.resolvePhotoUrl(url) let resolved = Self.resolvePhotoUrl(url)
print("[Avatar] Found key '\(key)' with value: \(url) -> \(resolved)") if IS_DEV { print("[Avatar] Found key '\(key)' with value: \(url) -> \(resolved)") }
return resolved return resolved
} }
if let url = json[key] as? String, !url.isEmpty { if let url = json[key] as? String, !url.isEmpty {
let resolved = Self.resolvePhotoUrl(url) let resolved = Self.resolvePhotoUrl(url)
print("[Avatar] Found key '\(key)' in json with value: \(url) -> \(resolved)") if IS_DEV { print("[Avatar] Found key '\(key)' in json with value: \(url) -> \(resolved)") }
return resolved return resolved
} }
} }
print("[Avatar] No avatar URL found in response") if IS_DEV { print("[Avatar] No avatar URL found in response") }
return nil return nil
} }
@ -724,7 +724,7 @@ actor APIService {
func getUserAvatarUrl(userId: Int) async throws -> String? { func getUserAvatarUrl(userId: Int) async throws -> String? {
// Try the avatar endpoint with UserID // Try the avatar endpoint with UserID
let json = try await postJSON("/auth/avatar.php", payload: ["UserID": userId]) let json = try await postJSON("/auth/avatar.php", payload: ["UserID": userId])
print("[Avatar] getUserAvatarUrl(\(userId)) Response: \(json)") if IS_DEV { print("[Avatar] getUserAvatarUrl(\(userId)) Response: \(json)") }
let data = json["DATA"] as? [String: Any] ?? json let data = json["DATA"] as? [String: Any] ?? json
@ -733,15 +733,15 @@ actor APIService {
"UserPhotoUrl", "USERPHOTOURL", "userPhotoUrl"] "UserPhotoUrl", "USERPHOTOURL", "userPhotoUrl"]
for key in keys { for key in keys {
if let url = data[key] as? String, !url.isEmpty { if let url = data[key] as? String, !url.isEmpty {
print("[Avatar] Found avatar for userId \(userId): \(url)") if IS_DEV { print("[Avatar] Found avatar for userId \(userId): \(url)") }
return Self.resolvePhotoUrl(url) return Self.resolvePhotoUrl(url)
} }
if let url = json[key] as? String, !url.isEmpty { if let url = json[key] as? String, !url.isEmpty {
print("[Avatar] Found avatar for userId \(userId): \(url)") if IS_DEV { print("[Avatar] Found avatar for userId \(userId): \(url)") }
return Self.resolvePhotoUrl(url) return Self.resolvePhotoUrl(url)
} }
} }
print("[Avatar] No avatar found for userId \(userId), all keys: \(json.keys.sorted())") if IS_DEV { print("[Avatar] No avatar found for userId \(userId), all keys: \(json.keys.sorted())") }
return nil return nil
} }

View file

@ -89,7 +89,7 @@ final class BeaconScanner: NSObject, ObservableObject {
} }
} }
print("[BeaconScanner] Started scanning for \(BeaconShardPool.uuids.count) shard UUIDs, target ServicePointId: \(targetServicePointId)") if IS_DEV { print("[BeaconScanner] Started scanning for \(BeaconShardPool.uuids.count) shard UUIDs, target ServicePointId: \(targetServicePointId)") }
} }
func stopScanning() { func stopScanning() {
@ -137,12 +137,12 @@ final class BeaconScanner: NSObject, ObservableObject {
await MainActor.run { await MainActor.run {
self.resolvedBeacons[key] = servicePointId self.resolvedBeacons[key] = servicePointId
self.pendingResolutions.remove(key) self.pendingResolutions.remove(key)
print("[BeaconScanner] Resolved \(key) -> ServicePointId \(servicePointId)") if IS_DEV { print("[BeaconScanner] Resolved \(key) -> ServicePointId \(servicePointId)") }
} }
} catch { } catch {
await MainActor.run { await MainActor.run {
self.pendingResolutions.remove(key) self.pendingResolutions.remove(key)
print("[BeaconScanner] Failed to resolve \(key): \(error)") if IS_DEV { print("[BeaconScanner] Failed to resolve \(key): \(error)") }
} }
} }
} }
@ -181,7 +181,7 @@ extension BeaconScanner: CLLocationManagerDelegate {
if rssiSamples.count >= minSamplesToConfirm { if rssiSamples.count >= minSamplesToConfirm {
let avg = Double(rssiSamples.reduce(0, +)) / Double(rssiSamples.count) let avg = Double(rssiSamples.reduce(0, +)) / Double(rssiSamples.count)
print("[BeaconScanner] Target beacon confirmed! Avg RSSI: \(avg)") if IS_DEV { print("[BeaconScanner] Target beacon confirmed! Avg RSSI: \(avg)") }
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
self?.onBeaconDetected(avg) self?.onBeaconDetected(avg)
} }
@ -210,6 +210,6 @@ extension BeaconScanner: CLLocationManagerDelegate {
} }
func locationManager(_ manager: CLLocationManager, didFailRangingFor constraint: CLBeaconIdentityConstraint, error: Error) { func locationManager(_ manager: CLLocationManager, didFailRangingFor constraint: CLBeaconIdentityConstraint, error: Error) {
print("[BeaconScanner] Ranging failed: \(error)") if IS_DEV { print("[BeaconScanner] Ranging failed: \(error)") }
} }
} }

View file

@ -128,7 +128,7 @@ struct AboutScreen: View {
aboutInfo = try await APIService.shared.getAboutInfo() aboutInfo = try await APIService.shared.getAboutInfo()
} catch { } catch {
// Use fallback on error // Use fallback on error
print("Failed to load about info: \(error)") if IS_DEV { print("Failed to load about info: \(error)") }
} }
isLoading = false isLoading = false
} }

View file

@ -13,7 +13,7 @@ struct BusinessSelectionScreen: View {
@State private var selectedBusiness: Employment? @State private var selectedBusiness: Employment?
@State private var debugText = "" @State private var debugText = ""
private let refreshTimer = Timer.publish(every: 2, on: .main, in: .common).autoconnect() private let refreshTimer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
var body: some View { var body: some View {
NavigationStack { NavigationStack {

View file

@ -20,7 +20,7 @@ struct MyTasksScreen: View {
FilterTab(value: "completed", label: "Done", icon: "checkmark.circle.fill"), FilterTab(value: "completed", label: "Done", icon: "checkmark.circle.fill"),
] ]
private let refreshTimer = Timer.publish(every: 2, on: .main, in: .common).autoconnect() private let refreshTimer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
var body: some View { var body: some View {
VStack(spacing: 0) { VStack(spacing: 0) {

View file

@ -28,6 +28,7 @@ struct TaskDetailScreen: View {
@State private var showCancelOrderAlert = false @State private var showCancelOrderAlert = false
@State private var isCancelingOrder = false @State private var isCancelingOrder = false
@State private var taskAccepted = false // Track if task was just accepted @State private var taskAccepted = false // Track if task was just accepted
@State private var hasCompleted = false // Guard against double-completion
@State private var customerAvatarUrl: String? // Fetched separately if not in task details @State private var customerAvatarUrl: String? // Fetched separately if not in task details
// Rating dialog // Rating dialog
@ -100,6 +101,7 @@ struct TaskDetailScreen: View {
} else if result == "cancelled" || result == "error" { } else if result == "cancelled" || result == "error" {
autoCompleting = false autoCompleting = false
beaconDetected = false beaconDetected = false
hasCompleted = false
beaconScanner?.resetSamples() beaconScanner?.resetSamples()
beaconScanner?.startScanning() beaconScanner?.startScanning()
} }
@ -629,6 +631,7 @@ struct TaskDetailScreen: View {
} }
.buttonStyle(.borderedProminent) .buttonStyle(.borderedProminent)
.tint(Color(red: 0.13, green: 0.55, blue: 0.13)) .tint(Color(red: 0.13, green: 0.55, blue: 0.13))
.disabled(hasCompleted)
} else { } else {
Button { showCompleteAlert = true } label: { Button { showCompleteAlert = true } label: {
Label("Complete Task", systemImage: "checkmark.circle.fill") Label("Complete Task", systemImage: "checkmark.circle.fill")
@ -637,6 +640,7 @@ struct TaskDetailScreen: View {
} }
.buttonStyle(.borderedProminent) .buttonStyle(.borderedProminent)
.tint(.green) .tint(.green)
.disabled(hasCompleted)
} }
} }
} }
@ -706,12 +710,12 @@ struct TaskDetailScreen: View {
} }
} }
print("[Beacon] showCompleteButton=\(showCompleteButton), servicePointId=\(d.servicePointId)") if IS_DEV { print("[Beacon] showCompleteButton=\(showCompleteButton), servicePointId=\(d.servicePointId)") }
if showCompleteButton && d.servicePointId > 0 { if showCompleteButton && d.servicePointId > 0 {
print("[Beacon] Starting beacon scanning for ServicePointId: \(d.servicePointId)") if IS_DEV { print("[Beacon] Starting beacon scanning for ServicePointId: \(d.servicePointId)") }
startBeaconScanning(d.servicePointId) startBeaconScanning(d.servicePointId)
} else { } else {
print("[Beacon] NOT starting scan - showCompleteButton=\(showCompleteButton), servicePointId=\(d.servicePointId)") if IS_DEV { print("[Beacon] NOT starting scan - showCompleteButton=\(showCompleteButton), servicePointId=\(d.servicePointId)") }
} }
} catch { } catch {
self.error = error.localizedDescription self.error = error.localizedDescription
@ -723,9 +727,10 @@ struct TaskDetailScreen: View {
let scanner = BeaconScanner( let scanner = BeaconScanner(
targetServicePointId: servicePointId, targetServicePointId: servicePointId,
onBeaconDetected: { [self] _ in onBeaconDetected: { [self] _ in
if !beaconDetected && !autoCompleting { if !beaconDetected && !autoCompleting && !hasCompleted {
beaconDetected = true beaconDetected = true
autoCompleting = true autoCompleting = true
hasCompleted = true
beaconScanner?.stopScanning() beaconScanner?.stopScanning()
showAutoCompleteDialog = true showAutoCompleteDialog = true
} }
@ -760,6 +765,8 @@ struct TaskDetailScreen: View {
} }
private func completeTask() { private func completeTask() {
guard !hasCompleted else { return }
hasCompleted = true
Task { Task {
do { do {
try await APIService.shared.completeTask(taskId: task.taskId) try await APIService.shared.completeTask(taskId: task.taskId)
@ -768,9 +775,11 @@ struct TaskDetailScreen: View {
if case .ratingRequired = apiError { if case .ratingRequired = apiError {
showRatingDialog = true showRatingDialog = true
} else { } else {
hasCompleted = false
self.error = apiError.localizedDescription self.error = apiError.localizedDescription
} }
} catch { } catch {
hasCompleted = false
self.error = error.localizedDescription self.error = error.localizedDescription
} }
} }

View file

@ -11,7 +11,7 @@ struct TaskListScreen: View {
@State private var selectedTask: WorkTask? @State private var selectedTask: WorkTask?
@State private var showingMyTasks = false @State private var showingMyTasks = false
private let refreshTimer = Timer.publish(every: 2, on: .main, in: .common).autoconnect() private let refreshTimer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
var body: some View { var body: some View {
ZStack(alignment: .bottomTrailing) { ZStack(alignment: .bottomTrailing) {