Skip to content

Commit d231fef

Browse files
[Shipping Labels] Show alert when printing shipping label fails (#15279)
2 parents f5e4de8 + 87237aa commit d231fef

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Post-Purchase/WooShippingPostPurchaseView.swift

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ struct WooShippingPostPurchaseView: View {
44
@ObservedObject private(set) var viewModel: WooShippingPostPurchaseViewModel
55

66
@State private var isPrintingLabel = false
7+
@State private var showingPrintingError = false
78

89
var body: some View {
910
VStack(alignment: .leading, spacing: 8) {
@@ -37,10 +38,8 @@ struct WooShippingPostPurchaseView: View {
3738
.roundedBorder(cornerRadius: 8, lineColor: Color(.separator), lineWidth: 1)
3839
}
3940
Button {
40-
isPrintingLabel = true
4141
Task { @MainActor in
42-
await viewModel.printLabel()
43-
isPrintingLabel = false
42+
await printLabel()
4443
}
4544
} label: {
4645
Text(Localization.printButton)
@@ -105,6 +104,33 @@ struct WooShippingPostPurchaseView: View {
105104
.footnoteStyle()
106105
}
107106
.padding(.vertical)
107+
.alert(Localization.PrintingLabelError.title, isPresented: $showingPrintingError, actions: {
108+
Button(role: .cancel) {} label: {
109+
Text(Localization.PrintingLabelError.cancel)
110+
}
111+
Button {
112+
Task { @MainActor in
113+
await printLabel()
114+
}
115+
} label: {
116+
Text(Localization.PrintingLabelError.retry)
117+
}
118+
}, message: {
119+
Text(Localization.PrintingLabelError.message)
120+
})
121+
}
122+
}
123+
124+
private extension WooShippingPostPurchaseView {
125+
func printLabel() async {
126+
isPrintingLabel = true
127+
do {
128+
try await viewModel.printLabel()
129+
} catch {
130+
showingPrintingError = true
131+
DDLogError("Error generating shipping label document for printing: \(error)")
132+
}
133+
isPrintingLabel = false
108134
}
109135
}
110136

@@ -131,9 +157,9 @@ private extension WooShippingPostPurchaseView {
131157
value: "Learn how to print from your mobile device",
132158
comment: "Link for more information about how to print a purchased shipping label on the shipping label screen")
133159
static let infoTitle =
134-
NSLocalizedString("wooShipping.createLabels.postPurchase.infoTitle",
135-
value: "Print from your mobile device",
136-
comment: "Navigation bar title of shipping label printing instructions screen")
160+
NSLocalizedString("wooShipping.createLabels.postPurchase.infoTitle",
161+
value: "Print from your mobile device",
162+
comment: "Navigation bar title of shipping label printing instructions screen")
137163
static let trackShipment = NSLocalizedString("wooShipping.createLabels.postPurchase.trackShipment",
138164
value: "Track shipment",
139165
comment: "Link to track a shipment for a purchase shipping label on the shipping label screen")
@@ -146,6 +172,30 @@ private extension WooShippingPostPurchaseView {
146172
static let note = NSLocalizedString("wooShipping.createLabels.postPurchase.note",
147173
value: "Note: Reusing a printed label is a violation of our terms of service and may result in criminal charges.",
148174
comment: "Note about reusing a purchased shipping label on the shipping label screen")
175+
enum PrintingLabelError {
176+
static let title = NSLocalizedString(
177+
"wooShipping.createLabels.postPurchase.printingLabelError.title",
178+
value: "Error previewing shipping label",
179+
comment: "Title of the error alert when printing a shipping label fails in the post purchase flow."
180+
)
181+
static let message = NSLocalizedString(
182+
"wooShipping.createLabels.postPurchase.printingLabelError.message",
183+
value: "Do you want to try again?",
184+
comment: "Message of the error alert when printing a shipping label fails in the post purchase flow."
185+
)
186+
static let cancel = NSLocalizedString(
187+
"wooShipping.createLabels.postPurchase.printingLabelError.cancel",
188+
value: "Cancel",
189+
comment: "Button on the error alert when printing a shipping label fails in the post purchase flow." +
190+
"Tapping on this button would cancel the printing."
191+
)
192+
static let retry = NSLocalizedString(
193+
"wooShipping.createLabels.postPurchase.printingLabelError.retry",
194+
value: "Retry",
195+
comment: "Button on the error alert when printing a shipping label fails in the post purchase flow." +
196+
"Tapping on this button would retry printing the shipping label."
197+
)
198+
}
149199
}
150200
}
151201

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Post-Purchase/WooShippingPostPurchaseViewModel.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,9 @@ final class WooShippingPostPurchaseViewModel: ObservableObject {
5656

5757
/// Fetches the shipping label in the selected paper size and presents the print dialog.
5858
@MainActor
59-
func printLabel() async {
60-
do {
61-
let printData = try await requestPrintData()
62-
presentPrintDialog(with: printData)
63-
} catch {
64-
DDLogError("Error generating shipping label document for printing: \(error)")
65-
}
59+
func printLabel() async throws {
60+
let printData = try await requestPrintData()
61+
presentPrintDialog(with: printData)
6662
}
6763
}
6864

WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingPostPurchaseViewModelTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ final class WooShippingPostPurchaseViewModelTests: XCTestCase {
6969
}
7070

7171
@MainActor
72-
func test_printLabel_fetches_label_data_from_remote() async {
72+
func test_printLabel_fetches_label_data_from_remote() async throws {
7373
// Given
7474
var printData: ShippingLabelPrintData?
7575
let stores = MockStoresManager(sessionManager: .testingInstance)
@@ -86,7 +86,7 @@ final class WooShippingPostPurchaseViewModelTests: XCTestCase {
8686
let viewModel = WooShippingPostPurchaseViewModel(shippingLabel: ShippingLabel.fake(), stores: stores)
8787

8888
// When
89-
await viewModel.printLabel()
89+
try await viewModel.printLabel()
9090

9191
// Then
9292
XCTAssertNotNil(printData)

0 commit comments

Comments
 (0)