Skip to content

Commit d0369b1

Browse files
committed
Update state management in OrderCustomerSection, slice out content view
1 parent ad61ab0 commit d0369b1

File tree

2 files changed

+67
-64
lines changed

2 files changed

+67
-64
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Creation/CustomerSection/OrderCustomerSection.swift

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,70 @@ import SwiftUI
44
///
55
struct OrderCustomerSection: View {
66

7-
/// View model to drive the view content
8-
let viewModel: NewOrderViewModel.CustomerDataViewModel
7+
/// Parent view model to access all data
8+
@ObservedObject var viewModel: NewOrderViewModel
99

10-
/// View model for Address Form
11-
let addressFormViewModel: CreateOrderAddressFormViewModel
10+
/// View model to drive the view content
11+
private var customerDataViewModel: NewOrderViewModel.CustomerDataViewModel {
12+
viewModel.customerDataViewModel
13+
}
1214

1315
@State private var showAddressForm: Bool = false
1416

17+
var body: some View {
18+
OrderCustomerSectionContent(viewModel: viewModel.customerDataViewModel, showAddressForm: $showAddressForm)
19+
.sheet(isPresented: $showAddressForm) {
20+
NavigationView {
21+
EditOrderAddressForm(dismiss: {
22+
showAddressForm.toggle()
23+
}, viewModel: viewModel.createOrderAddressFormViewModel())
24+
}
25+
}
26+
}
27+
}
28+
29+
private struct OrderCustomerSectionContent: View {
30+
31+
/// View model to drive the view content
32+
var viewModel: NewOrderViewModel.CustomerDataViewModel
33+
34+
@Binding var showAddressForm: Bool
35+
1536
@Environment(\.safeAreaInsets) var safeAreaInsets: EdgeInsets
1637

1738
var body: some View {
18-
Group {
19-
Divider()
39+
Divider()
2040

21-
VStack(alignment: .leading, spacing: .zero) {
22-
HStack(alignment: .top) {
23-
Text(Localization.customer)
24-
.headlineStyle()
25-
26-
Spacer()
27-
28-
if viewModel.isDataAvailable {
29-
Button(Localization.editButton) {
30-
showAddressForm.toggle()
31-
}
32-
.buttonStyle(LinkButtonStyle())
33-
.fixedSize(horizontal: true, vertical: true)
34-
.padding(.top, -Layout.linkButtonTopPadding) // remove padding to align button title to the top
35-
.padding(.trailing, -Layout.linkButtonTrailingPadding) // remove padding to align button title to the side
36-
}
37-
}
38-
.padding([.leading, .top, .trailing])
41+
VStack(alignment: .leading, spacing: .zero) {
42+
HStack(alignment: .top) {
43+
Text(Localization.customer)
44+
.headlineStyle()
3945

40-
Spacer(minLength: Layout.verticalHeadlineSpacing)
46+
Spacer()
4147

42-
if !viewModel.isDataAvailable {
43-
createCustomerView
44-
} else {
45-
customerDataView
48+
if viewModel.isDataAvailable {
49+
Button(Localization.editButton) {
50+
showAddressForm.toggle()
51+
}
52+
.buttonStyle(LinkButtonStyle())
53+
.fixedSize(horizontal: true, vertical: true)
54+
.padding(.top, -Layout.linkButtonTopPadding) // remove padding to align button title to the top
55+
.padding(.trailing, -Layout.linkButtonTrailingPadding) // remove padding to align button title to the side
4656
}
4757
}
48-
.padding(.horizontal, insets: safeAreaInsets)
49-
.background(Color(.listForeground))
58+
.padding([.leading, .top, .trailing])
5059

51-
Divider()
52-
}
53-
.sheet(isPresented: $showAddressForm) {
54-
NavigationView {
55-
EditOrderAddressForm(dismiss: {
56-
showAddressForm.toggle()
57-
}, viewModel: addressFormViewModel)
60+
if !viewModel.isDataAvailable {
61+
Spacer(minLength: Layout.verticalHeadlineSpacing)
62+
createCustomerView
63+
} else {
64+
customerDataView
5865
}
5966
}
67+
.padding(.horizontal, insets: safeAreaInsets)
68+
.background(Color(.listForeground))
69+
70+
Divider()
6071
}
6172

6273
private var createCustomerView: some View {
@@ -69,39 +80,31 @@ struct OrderCustomerSection: View {
6980

7081
private var customerDataView: some View {
7182
Group {
72-
VStack(alignment: .leading, spacing: Layout.verticalEmailSpacing) {
73-
if let fullName = viewModel.fullName {
74-
Text(fullName)
75-
.bodyStyle()
76-
}
77-
if let email = viewModel.email {
78-
Text(email)
79-
.footnoteStyle()
80-
}
81-
}
82-
.padding([.leading, .bottom, .trailing])
83-
.padding(.top, -Layout.linkButtonTopPadding)
84-
if let shippingAddressFormatted = viewModel.shippingAddressFormatted {
85-
addressDetails(title: Localization.shippingTitle, formattedAddress: shippingAddressFormatted)
86-
}
83+
addressDetails(title: Localization.shippingTitle, formattedAddress: viewModel.shippingAddressFormatted)
84+
Divider()
85+
.padding(.leading)
86+
addressDetails(title: Localization.billingTitle, formattedAddress: viewModel.billingAddressFormatted)
8787
}
8888
}
8989

90-
@ViewBuilder private func addressDetails(title: String, formattedAddress: String) -> some View {
91-
Divider()
92-
.padding(.leading)
90+
@ViewBuilder private func addressDetails(title: String, formattedAddress: String?) -> some View {
9391
VStack(alignment: .leading, spacing: Layout.verticalAddressSpacing) {
9492
Text(title)
9593
.headlineStyle()
96-
Text(formattedAddress)
97-
.bodyStyle()
94+
if let formattedAddress = formattedAddress, formattedAddress.isNotEmpty {
95+
Text(formattedAddress)
96+
.bodyStyle()
97+
} else {
98+
Text(Localization.noAddress)
99+
.bodyStyle()
100+
}
98101
}
99102
.padding()
100103
}
101104
}
102105

103106
// MARK: Constants
104-
private extension OrderCustomerSection {
107+
private extension OrderCustomerSectionContent {
105108
enum Layout {
106109
static let verticalHeadlineSpacing: CGFloat = 22.0
107110
static let verticalEmailSpacing: CGFloat = 4.0
@@ -117,13 +120,14 @@ private extension OrderCustomerSection {
117120

118121
static let billingTitle = NSLocalizedString("Billing Address", comment: "Title for the Billing Address section in order customer data")
119122
static let shippingTitle = NSLocalizedString("Shipping Address", comment: "Title for the Edit Shipping Address section in order customer data")
123+
124+
static let noAddress = NSLocalizedString("No address specified.", comment: "Placeholder for empty address in order customer data")
120125
}
121126
}
122127

123128
@available(iOS 15.0, *)
124129
struct OrderCustomerSection_Previews: PreviewProvider {
125130
static var previews: some View {
126-
let orderAdressFormViewModel = NewOrderViewModel(siteID: 123).createOrderAddressFormViewModel()
127131
let emptyViewModel = NewOrderViewModel.CustomerDataViewModel(billingAddress: nil, shippingAddress: nil)
128132
let addressViewModel = NewOrderViewModel.CustomerDataViewModel(fullName: "Johnny Appleseed",
129133
@@ -136,8 +140,8 @@ struct OrderCustomerSection_Previews: PreviewProvider {
136140
""")
137141

138142
ScrollView {
139-
OrderCustomerSection(viewModel: emptyViewModel, addressFormViewModel: orderAdressFormViewModel)
140-
OrderCustomerSection(viewModel: addressViewModel, addressFormViewModel: orderAdressFormViewModel)
143+
OrderCustomerSectionContent(viewModel: emptyViewModel, showAddressForm: .constant(false))
144+
OrderCustomerSectionContent(viewModel: addressViewModel, showAddressForm: .constant(false))
141145
}
142146
}
143147
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ struct NewOrder: View {
6666
Spacer(minLength: Layout.sectionSpacing)
6767
}
6868

69-
OrderCustomerSection(viewModel: viewModel.customerDataViewModel,
70-
addressFormViewModel: viewModel.createOrderAddressFormViewModel())
69+
OrderCustomerSection(viewModel: viewModel)
7170
}
7271
}
7372
.background(Color(.listBackground).ignoresSafeArea())

0 commit comments

Comments
 (0)