-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Shipping labels] Display actual shipment data in shipment details bo…
…ttom sheet (#14101)
- Loading branch information
Showing
9 changed files
with
278 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 48 additions & 1 deletion
49
...Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,60 @@ | ||
import Foundation | ||
import Yosemite | ||
import WooFoundation | ||
|
||
/// Provides view data for `WooShippingCreateLabelsView`. | ||
/// | ||
final class WooShippingCreateLabelsViewModel: ObservableObject { | ||
/// View model for the items to ship. | ||
@Published private(set) var items: WooShippingItemsViewModel | ||
|
||
init(order: Order) { | ||
/// Address to ship from (store address), formatted for display. | ||
let originAddress: String | ||
|
||
/// Address to ship to (customer address), formatted for display and split into separate lines to allow additional formatting. | ||
let destinationAddressLines: [String] | ||
|
||
/// Shipping lines for the order, with formatted amount. | ||
let shippingLines: [WooShipping_ShippingLineViewModel] | ||
|
||
/// Whether to mark the order as complete after the label is purchased. | ||
@Published var markOrderComplete: Bool = false | ||
|
||
/// Closure to execute after the label is successfully purchased. | ||
let onLabelPurchase: ((_ markOrderComplete: Bool) -> Void)? | ||
|
||
init(order: Order, | ||
siteAddress: SiteAddress = SiteAddress(), | ||
onLabelPurchase: ((Bool) -> Void)? = nil) { | ||
self.items = WooShippingItemsViewModel(dataSource: DefaultWooShippingItemsDataSource(order: order)) | ||
self.onLabelPurchase = onLabelPurchase | ||
self.originAddress = Self.formatOriginAddress(siteAddress: siteAddress) | ||
self.destinationAddressLines = (order.shippingAddress?.formattedPostalAddress ?? "").components(separatedBy: .newlines) | ||
self.shippingLines = order.shippingLines.map({ WooShipping_ShippingLineViewModel(shippingLine: $0) }) | ||
} | ||
|
||
/// Purchases a shipping label with the provided label details and settings. | ||
func purchaseLabel() { | ||
// TODO: 13556 - Add action to purchase label remotely | ||
onLabelPurchase?(markOrderComplete) // TODO: 13556 - Only call this closure if the remote purchase is successful | ||
} | ||
} | ||
|
||
private extension WooShippingCreateLabelsViewModel { | ||
/// Formats the origin address from the provided `SiteAddress`. | ||
static func formatOriginAddress(siteAddress: SiteAddress) -> String { | ||
let address = Address(firstName: "", | ||
lastName: "", | ||
company: nil, | ||
address1: siteAddress.address, | ||
address2: siteAddress.address2, | ||
city: siteAddress.city, | ||
state: siteAddress.state, | ||
postcode: siteAddress.postalCode, | ||
country: siteAddress.countryCode.rawValue, | ||
phone: nil, | ||
email: nil) | ||
let formattedPostalAddress = address.formattedPostalAddress?.replacingOccurrences(of: "\n", with: ", ") | ||
return formattedPostalAddress ?? "" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...hipping Labels/WooShipping Create Shipping Labels/WooShipping_ShippingLineViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Yosemite | ||
import WooFoundation | ||
|
||
/// Represents a shipping line in the Woo Shipping label creation flow. | ||
struct WooShipping_ShippingLineViewModel: Identifiable { | ||
/// Unique ID for the shipping line. | ||
let id: Int64 | ||
|
||
/// Title for the shipping line. | ||
let title: String | ||
|
||
/// Formatted total amount for the shipping line. | ||
let formattedTotal: String | ||
|
||
init(shippingLine: ShippingLine, | ||
currencyFormatter: CurrencyFormatter = CurrencyFormatter(currencySettings: ServiceLocator.currencySettings)) { | ||
id = shippingLine.shippingID | ||
title = shippingLine.methodTitle | ||
formattedTotal = currencyFormatter.formatAmount(shippingLine.total) ?? shippingLine.total | ||
} | ||
} |
Oops, something went wrong.