diff --git a/Sources/PediFoods/Views/Main/CartView.swift b/Sources/PediFoods/Views/Main/CartView.swift index 4ba0420..d104ae3 100644 --- a/Sources/PediFoods/Views/Main/CartView.swift +++ b/Sources/PediFoods/Views/Main/CartView.swift @@ -296,6 +296,21 @@ struct CartView: View { selectedCustomerAddress = nil } + // id can be nil for some address book entries — lat/lng is set + // immediately and reliably at selection time (AddressesView. + // selectAddress), so it's a stronger signal than the label match + // below, which silently collides whenever two addresses share an + // empty/duplicate label. Without this, an id-less address falls + // through to addresses.first and never actually "changes". + if selectedCustomerAddress == nil, + let lat = appState.address.latitude, let lng = appState.address.longitude { + selectedCustomerAddress = addresses.first { address in + guard let addrLat = address.latLong?.first, + let addrLng = address.latLong?.dropFirst().first else { return false } + return abs(addrLat - lat) < 0.00001 && abs(addrLng - lng) < 0.00001 + } + } + if selectedCustomerAddress == nil { let display = appState.address.display .trimmingCharacters(in: .whitespacesAndNewlines) diff --git a/Sources/PediFoods/Views/Main/CheckoutView+Logic.swift b/Sources/PediFoods/Views/Main/CheckoutView+Logic.swift index 2d67163..035c256 100644 --- a/Sources/PediFoods/Views/Main/CheckoutView+Logic.swift +++ b/Sources/PediFoods/Views/Main/CheckoutView+Logic.swift @@ -92,6 +92,22 @@ extension CheckoutView { selectedCustomerAddress = nil } + // id can be nil for some address book entries — lat/lng is set + // immediately and reliably at selection time (AddressesView. + // selectAddress), so it's a stronger signal than the label match + // below, which silently collides whenever two addresses share an + // empty/duplicate label. Without this ordered first, an id-less + // address falls through to addresses.first and never actually + // "changes" even though delivery to it is allowed. + if selectedCustomerAddress == nil, + let lat = appState.address.latitude, let lng = appState.address.longitude { + selectedCustomerAddress = addresses.first { address in + guard let addrLat = address.latLong?.first, + let addrLng = address.latLong?.dropFirst().first else { return false } + return abs(addrLat - lat) < 0.00001 && abs(addrLng - lng) < 0.00001 + } + } + if selectedCustomerAddress == nil { let display = appState.address.display .trimmingCharacters(in: .whitespacesAndNewlines) @@ -103,15 +119,6 @@ extension CheckoutView { } } - if selectedCustomerAddress == nil, - let lat = appState.address.latitude, let lng = appState.address.longitude { - selectedCustomerAddress = addresses.first { address in - guard let addrLat = address.latLong?.first, - let addrLng = address.latLong?.dropFirst().first else { return false } - return abs(addrLat - lat) < 0.00001 && abs(addrLng - lng) < 0.00001 - } - } - if selectedCustomerAddress == nil { selectedCustomerAddress = addresses.first }