Skip to content

Commit f9366ee

Browse files
committed
Set and unset notice label in view model
1 parent 02a0572 commit f9366ee

File tree

2 files changed

+63
-46
lines changed

2 files changed

+63
-46
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,10 @@ private extension WooShippingCreateLabelsView {
148148
Text(Localization.BottomSheet.shipmentDetails)
149149
.foregroundStyle(Color(.primary))
150150
.bold()
151-
if viewModel.showAddressVerificationNotice {
152-
addressVerificationNotice
153-
.onTapGesture {
154-
// TODO: Start address editing/verification flow if needed (if destination address is unverified).
155-
}
156-
}
151+
addressVerificationNotice(with: viewModel.destinationAddressStatusNoticeLabel)
152+
.onTapGesture {
153+
// TODO: Start address editing/verification flow if needed (if destination address is unverified).
154+
}
157155
}
158156
}
159157

@@ -314,26 +312,29 @@ private extension WooShippingCreateLabelsView {
314312
}
315313

316314
/// View showing a notice about the destination address verification status.
317-
var addressVerificationNotice: some View {
318-
HStack(spacing: 8) {
319-
Image(systemName: isDestinationAddressVerified ? "checkmark.circle" : "exclamationmark.circle")
320-
Text(Localization.AddressVerification.noticeLabel(for: viewModel.destinationAddressStatus))
321-
.frame(maxWidth: .infinity, alignment: .leading)
322-
Button {
323-
withAnimation {
324-
viewModel.showAddressVerificationNotice = false
315+
@ViewBuilder
316+
func addressVerificationNotice(with label: String?) -> some View {
317+
if let label = viewModel.destinationAddressStatusNoticeLabel {
318+
HStack(spacing: 8) {
319+
Image(systemName: isDestinationAddressVerified ? "checkmark.circle" : "exclamationmark.circle")
320+
Text(label)
321+
.frame(maxWidth: .infinity, alignment: .leading)
322+
Button {
323+
withAnimation {
324+
viewModel.destinationAddressStatusNoticeLabel = nil
325+
}
326+
} label: {
327+
Image(systemName: "xmark")
328+
.renderedIf(!isDestinationAddressVerified)
325329
}
326-
} label: {
327-
Image(systemName: "xmark")
328-
.renderedIf(!isDestinationAddressVerified)
329330
}
331+
.font(.subheadline)
332+
.foregroundStyle(isDestinationAddressVerified ? Layout.green : Layout.red)
333+
.padding(.horizontal, 16)
334+
.padding(.vertical, 12)
335+
.background(RoundedRectangle(cornerRadius: Layout.cornerRadius)
336+
.fill(Color(uiColor: isDestinationAddressVerified ? .withColorStudio(.green, shade: .shade0) : .withColorStudio(.red, shade: .shade0))))
330337
}
331-
.font(.subheadline)
332-
.foregroundStyle(isDestinationAddressVerified ? Layout.green : Layout.red)
333-
.padding(.horizontal, 16)
334-
.padding(.vertical, 12)
335-
.background(RoundedRectangle(cornerRadius: Layout.cornerRadius)
336-
.fill(Color(uiColor: isDestinationAddressVerified ? .withColorStudio(.green, shade: .shade0) : .withColorStudio(.red, shade: .shade0))))
337338
}
338339
}
339340

@@ -449,25 +450,6 @@ private extension WooShippingCreateLabelsView {
449450
static let missing = NSLocalizedString("wooShipping.createLabels.addressVerification.missing",
450451
value: "Missing address",
451452
comment: "Label when an address is missing on the shipping label creation screen")
452-
static func noticeLabel(for status: WooShippingCreateLabelsViewModel.DestinationAddressStatus) -> String {
453-
switch status {
454-
case .verified:
455-
return destinationVerified
456-
case .unverified:
457-
return destinationUnverified
458-
case .missing:
459-
return destinationMissing
460-
}
461-
}
462-
static let destinationVerified = NSLocalizedString("wooShipping.createLabels.addressVerification.destinationVerified",
463-
value: "Verified destination address",
464-
comment: "Notice when a destination address is verified on the shipping label creation screen")
465-
static let destinationUnverified = NSLocalizedString("wooShipping.createLabels.addressVerification.destinationUnverified",
466-
value: "Destination address unverified",
467-
comment: "Notice when a destination address is unverified on the shipping label creation screen")
468-
static let destinationMissing = NSLocalizedString("wooShipping.createLabels.addressVerification.destinationMissing",
469-
value: "Destination address missing",
470-
comment: "Notice when a destination address is missing on the shipping label creation screen")
471453
}
472454
}
473455
}

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ final class WooShippingCreateLabelsViewModel: ObservableObject {
7676
/// The current destination address status.
7777
@Published private(set) var destinationAddressStatus: DestinationAddressStatus = .unverified
7878

79-
// TODO: Set to false if the destination address is already verified.
80-
// TODO: Set to true for a couple seconds after the destination address is verified.
81-
/// Whether the destination address verification notice is displayed.
82-
@Published var showAddressVerificationNotice = true
79+
/// This property can be set to display a notice with the provided label about the destination address status.
80+
@Published var destinationAddressStatusNoticeLabel: String?
8381

8482
/// Shipping lines for the order, with formatted amount.
8583
let shippingLines: [WooShipping_ShippingLineViewModel]
@@ -191,6 +189,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject {
191189
destinationAddressStatus = destinationAddressLines == nil ? .missing : .unverified
192190

193191
observeSelectedOriginAddress()
192+
observeDestinationAddressStatus()
194193
observeSelectedPackage()
195194
observeForLabelRates()
196195
loadStoreOptions()
@@ -341,6 +340,30 @@ private extension WooShippingCreateLabelsViewModel {
341340
.store(in: &subscriptions)
342341
}
343342

343+
/// Observes the destination address status and updates the notice label.
344+
func observeDestinationAddressStatus() {
345+
/// Set the notice when the destination address status changes.
346+
$destinationAddressStatus
347+
.map { status in
348+
switch status {
349+
case .verified:
350+
return Localization.DestinationAddressStatus.verified
351+
case .unverified:
352+
return Localization.DestinationAddressStatus.unverified
353+
case .missing:
354+
return Localization.DestinationAddressStatus.missing
355+
}
356+
}
357+
.assign(to: &$destinationAddressStatusNoticeLabel)
358+
359+
/// Clear the notice after a delay when the address is verified.
360+
$destinationAddressStatusNoticeLabel
361+
.filter { $0 == Localization.DestinationAddressStatus.verified }
362+
.delay(for: .seconds(2), scheduler: RunLoop.current)
363+
.map { _ in nil }
364+
.assign(to: &$destinationAddressStatusNoticeLabel)
365+
}
366+
344367
/// Observes the selected package and shipment weight and requests the available shipping rates.
345368
func observeForLabelRates() {
346369
$shipmentWeight
@@ -461,6 +484,18 @@ private extension WooShippingCreateLabelsViewModel {
461484
value: "Adult Signature Required",
462485
comment: "Label for row showing the additional cost to require an adult signature " +
463486
"on the shipping label creation screen")
487+
488+
enum DestinationAddressStatus {
489+
static let verified = NSLocalizedString("wooShipping.createLabels.addressVerification.destinationVerified",
490+
value: "Verified destination address",
491+
comment: "Notice when a destination address is verified on the shipping label creation screen")
492+
static let unverified = NSLocalizedString("wooShipping.createLabels.addressVerification.destinationUnverified",
493+
value: "Destination address unverified",
494+
comment: "Notice when a destination address is unverified on the shipping label creation screen")
495+
static let missing = NSLocalizedString("wooShipping.createLabels.addressVerification.destinationMissing",
496+
value: "Destination address missing",
497+
comment: "Notice when a destination address is missing on the shipping label creation screen")
498+
}
464499
}
465500

466501
enum Constants {

0 commit comments

Comments
 (0)