Skip to content

Commit 316bb7e

Browse files
committed
Merge branch 'trunk' into issue/6129-auto-draft-support
2 parents 213b28d + bc43663 commit 316bb7e

File tree

16 files changed

+160
-41
lines changed

16 files changed

+160
-41
lines changed

Experiments/Experiments/FeatureFlag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public enum FeatureFlag: Int {
5454
///
5555
case orderCreation
5656

57-
/// Allows new orders to be manually created
57+
/// Allows new orders to be created and synced as drafts
5858
///
5959
case orderCreationRemoteSynchronizer
6060

WooCommerce/Classes/View Modifiers/WooStyleModifiers.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ struct LinkStyle: ViewModifier {
101101
}
102102
}
103103

104+
struct HeadlineLinkStyle: ViewModifier {
105+
func body(content: Content) -> some View {
106+
content
107+
.font(.headline)
108+
.foregroundColor(Color(.accent))
109+
}
110+
}
111+
104112
// MARK: View extensions
105113
extension View {
106114
/// - Parameters:
@@ -143,4 +151,8 @@ extension View {
143151
func linkStyle() -> some View {
144152
self.modifier(LinkStyle())
145153
}
154+
155+
func headlineLinkStyle() -> some View {
156+
self.modifier(HeadlineLinkStyle())
157+
}
146158
}

WooCommerce/Classes/ViewRelated/Coupons/CouponDetails/CouponDetails.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct CouponDetails: View {
122122
ForEach(detailRows) { row in
123123
TitleAndValueRow(title: row.title,
124124
value: .content(row.content),
125-
selectable: false,
125+
selectionStyle: .none,
126126
action: row.action)
127127
.padding(.vertical, Constants.verticalSpacing)
128128
.padding(.horizontal, insets: geometry.safeAreaInsets)

WooCommerce/Classes/ViewRelated/Orders/Order Creation/NewOrder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct NewOrder: View {
4040
Spacer(minLength: Layout.sectionSpacing)
4141

4242
if viewModel.shouldShowPaymentSection {
43-
OrderPaymentSection(viewModel: viewModel.paymentDataViewModel)
43+
OrderPaymentSection(viewModel: viewModel.paymentDataViewModel, saveShippingLineClosure: viewModel.saveShippingLine)
4444

4545
Spacer(minLength: Layout.sectionSpacing)
4646
}

WooCommerce/Classes/ViewRelated/Orders/Order Creation/NewOrderViewModel.swift

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ final class NewOrderViewModel: ObservableObject {
123123
orderSynchronizer.order.items.isNotEmpty
124124
}
125125

126+
/// Representation of payment data display properties
127+
///
128+
@Published private(set) var paymentDataViewModel = PaymentDataViewModel()
129+
130+
/// Saves a shipping line.
131+
///
132+
/// - Parameter shippingLine: Optional shipping line object to save. `nil` will remove existing shipping line.
133+
func saveShippingLine(_ shippingLine: ShippingLine?) {
134+
orderSynchronizer.setShipping.send(shippingLine)
135+
}
136+
137+
// MARK: -
138+
126139
/// Defines if the view should be disabled.
127140
/// Currently `true` while performing a network request.
128141
///
@@ -136,10 +149,6 @@ final class NewOrderViewModel: ObservableObject {
136149
orderSynchronizer.order.status
137150
}
138151

139-
/// Representation of payment data display properties
140-
///
141-
@Published private(set) var paymentDataViewModel = PaymentDataViewModel()
142-
143152
/// Analytics engine.
144153
///
145154
private let analytics: Analytics
@@ -331,10 +340,17 @@ extension NewOrderViewModel {
331340
let itemsTotal: String
332341
let orderTotal: String
333342

343+
let shouldShowShippingTotal: Bool
344+
let shippingTotal: String
345+
334346
init(itemsTotal: String = "",
347+
shouldShowShippingTotal: Bool = false,
348+
shippingTotal: String = "",
335349
orderTotal: String = "",
336350
currencyFormatter: CurrencyFormatter = CurrencyFormatter(currencySettings: ServiceLocator.currencySettings)) {
337351
self.itemsTotal = currencyFormatter.formatAmount(itemsTotal) ?? ""
352+
self.shouldShowShippingTotal = shouldShowShippingTotal
353+
self.shippingTotal = currencyFormatter.formatAmount(shippingTotal) ?? ""
338354
self.orderTotal = currencyFormatter.formatAmount(orderTotal) ?? ""
339355
}
340356
}
@@ -440,10 +456,19 @@ private extension NewOrderViewModel {
440456
.map { $0.subtotal }
441457
.compactMap { self.currencyFormatter.convertToDecimal(from: $0) }
442458
.reduce(NSDecimalNumber(value: 0), { $0.adding($1) })
443-
.stringValue
444459

445-
// For now, the order total is the same as the items total
446-
return PaymentDataViewModel(itemsTotal: itemsTotal, orderTotal: itemsTotal, currencyFormatter: self.currencyFormatter)
460+
let shippingTotal = order.shippingLines
461+
.map { $0.total }
462+
.compactMap { self.currencyFormatter.convertToDecimal(from: $0) }
463+
.reduce(NSDecimalNumber(value: 0), { $0.adding($1) })
464+
465+
let orderTotal = itemsTotal.adding(shippingTotal)
466+
467+
return PaymentDataViewModel(itemsTotal: itemsTotal.stringValue,
468+
shouldShowShippingTotal: order.shippingLines.isNotEmpty,
469+
shippingTotal: shippingTotal.stringValue,
470+
orderTotal: orderTotal.stringValue,
471+
currencyFormatter: self.currencyFormatter)
447472
}
448473
.assign(to: &$paymentDataViewModel)
449474
}

WooCommerce/Classes/ViewRelated/Orders/Order Creation/PaymentSection/OrderPaymentSection.swift

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import SwiftUI
2+
import Yosemite
23

34
/// Represents the Payment section in an order
45
///
56
struct OrderPaymentSection: View {
67
/// View model to drive the view content
78
let viewModel: NewOrderViewModel.PaymentDataViewModel
89

10+
/// Closure to create/update the shipping line object
11+
let saveShippingLineClosure: (ShippingLine?) -> Void
12+
913
/// Environment safe areas
1014
///
1115
@Environment(\.safeAreaInsets) var safeAreaInsets: EdgeInsets
@@ -18,9 +22,13 @@ struct OrderPaymentSection: View {
1822
.headlineStyle()
1923
.padding()
2024

21-
TitleAndValueRow(title: Localization.productsTotal, value: .content(viewModel.itemsTotal), selectable: false) {}
25+
TitleAndValueRow(title: Localization.productsTotal, value: .content(viewModel.itemsTotal), selectionStyle: .none) {}
26+
27+
if ServiceLocator.featureFlagService.isFeatureFlagEnabled(.orderCreation) {
28+
shippingRow
29+
}
2230

23-
TitleAndValueRow(title: Localization.orderTotal, value: .content(viewModel.orderTotal), bold: true, selectable: false) {}
31+
TitleAndValueRow(title: Localization.orderTotal, value: .content(viewModel.orderTotal), bold: true, selectionStyle: .none) {}
2432

2533
Text(Localization.taxesInfo)
2634
.footnoteStyle()
@@ -31,6 +39,26 @@ struct OrderPaymentSection: View {
3139

3240
Divider()
3341
}
42+
43+
@ViewBuilder private var shippingRow: some View {
44+
if viewModel.shouldShowShippingTotal {
45+
TitleAndValueRow(title: Localization.shippingTotal, value: .content(viewModel.shippingTotal), selectionStyle: .highlight) {
46+
saveShippingLineClosure(nil)
47+
}
48+
} else {
49+
Button(Localization.addShipping) {
50+
let testShippingLine = ShippingLine(shippingID: 0,
51+
methodTitle: "Flat Rate",
52+
methodID: "other",
53+
total: "10",
54+
totalTax: "",
55+
taxes: [])
56+
saveShippingLineClosure(testShippingLine)
57+
}
58+
.buttonStyle(PlusButtonStyle())
59+
.padding()
60+
}
61+
}
3462
}
3563

3664
// MARK: Constants
@@ -41,14 +69,16 @@ private extension OrderPaymentSection {
4169
static let orderTotal = NSLocalizedString("Order Total", comment: "Label for the the row showing the total cost of the order")
4270
static let taxesInfo = NSLocalizedString("Taxes will be automatically calculated based on your store settings.",
4371
comment: "Information about taxes and the order total when creating a new order")
72+
static let addShipping = NSLocalizedString("Add shipping", comment: "Title text of the button that adds shipping line when creating a new order")
73+
static let shippingTotal = NSLocalizedString("Shipping", comment: "Label for the row showing the cost of shipping in the order")
4474
}
4575
}
4676

4777
struct OrderPaymentSection_Previews: PreviewProvider {
4878
static var previews: some View {
4979
let viewModel = NewOrderViewModel.PaymentDataViewModel(itemsTotal: "20.00", orderTotal: "20.00")
5080

51-
OrderPaymentSection(viewModel: viewModel)
81+
OrderPaymentSection(viewModel: viewModel, saveShippingLineClosure: { _ in })
5282
.previewLayout(.sizeThatFits)
5383
}
5484
}

WooCommerce/Classes/ViewRelated/Orders/Order Creation/Synchronizer/LocalOrderSynchronizer.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ private extension LocalOrderSynchronizer {
8181
}
8282
.assign(to: &$order)
8383

84-
// TODO: Bind shipping & fees input
84+
setShipping.withLatestFrom(orderPublisher)
85+
.map { shippingLineInput, order in
86+
order.copy(shippingLines: shippingLineInput.flatMap { [$0] } ?? [])
87+
}
88+
.assign(to: &$order)
89+
90+
// TODO: Bind fees input
8591
}
8692
}
8793

WooCommerce/Classes/ViewRelated/Orders/Order Details/Address Edit/EditOrderAddressForm.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ struct SingleAddressForm: View {
302302

303303
TitleAndValueRow(title: Localization.countryField,
304304
value: .init(placeHolder: Localization.placeholderSelectOption, content: fields.country),
305-
selectable: true) {
305+
selectionStyle: .disclosure) {
306306
showCountrySelector = true
307307
}
308308
Divider()
@@ -321,7 +321,7 @@ struct SingleAddressForm: View {
321321
if showStateFieldAsSelector {
322322
TitleAndValueRow(title: Localization.stateField,
323323
value: .init(placeHolder: Localization.placeholderSelectOption, content: fields.state),
324-
selectable: true) {
324+
selectionStyle: .disclosure) {
325325
showStateSelector = true
326326
}
327327
} else {

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Customs/ItemDetails/ShippingLabelCustomsFormItemDetails.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ struct ShippingLabelCustomsFormItemDetails: View {
125125
// Origin country
126126
VStack(spacing: 0) {
127127
VStack(spacing: 0) {
128-
TitleAndValueRow(title: Localization.originTitle, value: .placeholder(viewModel.originCountry.name), selectable: true) {
128+
TitleAndValueRow(title: Localization.originTitle, value: .placeholder(viewModel.originCountry.name), selectionStyle: .disclosure) {
129129
isShowingCountries.toggle()
130130
}
131131
.sheet(isPresented: $isShowingCountries, content: {

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Customs/ShippingLabelCustomsFormInput.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct ShippingLabelCustomsFormInput: View {
6666

6767
private var contentRows: some View {
6868
VStack(spacing: 0) {
69-
TitleAndValueRow(title: Localization.contentTypeTitle, value: .placeholder(viewModel.contentsType.localizedName), selectable: true) {
69+
TitleAndValueRow(title: Localization.contentTypeTitle, value: .placeholder(viewModel.contentsType.localizedName), selectionStyle: .disclosure) {
7070
showingContentTypes.toggle()
7171
}
7272
.sheet(isPresented: $showingContentTypes, content: {
@@ -99,7 +99,9 @@ struct ShippingLabelCustomsFormInput: View {
9999

100100
private var restrictionRows: some View {
101101
VStack(spacing: 0) {
102-
TitleAndValueRow(title: Localization.restrictionTypeTitle, value: .placeholder(viewModel.restrictionType.localizedName), selectable: true) {
102+
TitleAndValueRow(title: Localization.restrictionTypeTitle,
103+
value: .placeholder(viewModel.restrictionType.localizedName),
104+
selectionStyle: .disclosure) {
103105
showingRestrictionTypes.toggle()
104106
}
105107
.sheet(isPresented: $showingRestrictionTypes, content: {

0 commit comments

Comments
 (0)