Skip to content

Commit 6fcc52d

Browse files
authored
Merge pull request #5334 from woocommerce/issue/4787-address-form-dynamic-type
Order Editing: Add Address Form fields dynamic type support
2 parents 22f860f + f9ac4f7 commit 6fcc52d

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import SwiftUI
2+
3+
struct AdaptiveStack<Content: View>: View {
4+
@Environment(\.sizeCategory) var sizeCategory
5+
@Environment(\.horizontalSizeClass) var horizontalSizeClass
6+
7+
let horizontalAlignment: HorizontalAlignment
8+
let verticalAlignment: VerticalAlignment
9+
let spacing: CGFloat?
10+
let content: () -> Content
11+
12+
init(horizontalAlignment: HorizontalAlignment = .center,
13+
verticalAlignment: VerticalAlignment = .center,
14+
spacing: CGFloat? = nil,
15+
@ViewBuilder content: @escaping () -> Content) {
16+
self.horizontalAlignment = horizontalAlignment
17+
self.verticalAlignment = verticalAlignment
18+
self.spacing = spacing
19+
self.content = content
20+
}
21+
22+
var body: some View {
23+
Group {
24+
if sizeCategory.isAccessibilityCategory, horizontalSizeClass == .compact {
25+
VStack(alignment: horizontalAlignment, spacing: spacing, content: content)
26+
} else {
27+
HStack(alignment: verticalAlignment, spacing: spacing, content: content)
28+
}
29+
}
30+
}
31+
}

WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/TitleAndTextFieldRow.swift

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@ struct TitleAndTextFieldRow: View {
2525
}
2626

2727
var body: some View {
28-
HStack {
28+
AdaptiveStack(horizontalAlignment: .leading) {
2929
Text(title)
3030
.bodyStyle()
3131
.lineLimit(1)
3232
.fixedSize()
33-
Spacer()
34-
TextField(placeholder, text: $text, onEditingChanged: onEditingChanged ?? { _ in })
35-
.multilineTextAlignment(.trailing)
36-
.font(.body)
37-
.keyboardType(keyboardType)
38-
if let symbol = symbol {
39-
Text(symbol)
40-
.bodyStyle()
33+
HStack {
34+
TextField(placeholder, text: $text, onEditingChanged: onEditingChanged ?? { _ in })
35+
.multilineTextAlignment(.trailing)
36+
.font(.body)
37+
.keyboardType(keyboardType)
38+
if let symbol = symbol {
39+
Text(symbol)
40+
.bodyStyle()
41+
}
4142
}
4243
}
43-
.frame(height: Constants.height)
44+
.frame(minHeight: Constants.height)
4445
.padding([.leading, .trailing], Constants.padding)
4546
}
4647
}
@@ -77,5 +78,23 @@ struct TitleAndTextFieldRow_Previews: PreviewProvider {
7778
keyboardType: .default)
7879
.previewLayout(.fixed(width: 375, height: 100))
7980
.previewDisplayName("With symbol")
81+
82+
TitleAndTextFieldRow(title: "Add your text",
83+
placeholder: "Start typing",
84+
text: .constant("Hello"),
85+
symbol: nil,
86+
keyboardType: .default)
87+
.environment(\.sizeCategory, .accessibilityExtraLarge)
88+
.previewLayout(.fixed(width: 375, height: 150))
89+
.previewDisplayName("Dynamic Type: Large Font Size")
90+
91+
TitleAndTextFieldRow(title: "Total package weight",
92+
placeholder: "Value",
93+
text: .constant(""),
94+
symbol: "oz",
95+
keyboardType: .default)
96+
.environment(\.sizeCategory, .accessibilityExtraLarge)
97+
.previewLayout(.fixed(width: 375, height: 150))
98+
.previewDisplayName("Dynamic Type: Large Font Size with symbol")
8099
}
81100
}

WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/TitleAndValueRow.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ struct TitleAndValueRow: View {
1717
action()
1818
}, label: {
1919
HStack {
20-
Text(title)
21-
.bodyStyle()
22-
Spacer()
23-
Text(value.text)
24-
.style(for: value)
25-
.padding(.vertical, Constants.verticalPadding)
20+
AdaptiveStack(horizontalAlignment: .leading) {
21+
Text(title)
22+
.bodyStyle()
23+
Text(value.text)
24+
.style(for: value)
25+
.frame(maxWidth: .infinity, alignment: .trailing)
26+
.padding(.vertical, Constants.verticalPadding)
27+
}
2628

2729
Image(uiImage: .chevronImage)
2830
.flipsForRightToLeftLayoutDirection(true)
@@ -96,5 +98,10 @@ struct TitleAndValueRow_Previews: PreviewProvider {
9698
TitleAndValueRow(title: "Package selected", value: .placeholder("Small package 2"), selectable: false, action: { })
9799
.previewLayout(.fixed(width: 375, height: 100))
98100
.previewDisplayName("Row Not Selectable")
101+
102+
TitleAndValueRow(title: "Package selected", value: .placeholder("Small"), selectable: true, action: { })
103+
.environment(\.sizeCategory, .accessibilityExtraLarge)
104+
.previewLayout(.fixed(width: 375, height: 150))
105+
.previewDisplayName("Dynamic Type: Large Font Size")
99106
}
100107
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@
874874
A650BE872578E76600C655E0 /* MockStorageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A650BE852578E76600C655E0 /* MockStorageManager.swift */; };
875875
A6557218258B7510008AE7CA /* OrderListCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6557217258B7510008AE7CA /* OrderListCellViewModel.swift */; };
876876
A655725D258B91AE008AE7CA /* OrderListCellViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A655725C258B91AE008AE7CA /* OrderListCellViewModelTests.swift */; };
877+
AE6DBE3B2732CAAD00957E7A /* AdaptiveStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE6DBE3A2732CAAD00957E7A /* AdaptiveStack.swift */; };
877878
AEB73C0C25CD734200A8454A /* AttributePickerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB73C0B25CD734200A8454A /* AttributePickerViewModel.swift */; };
878879
AEB73C1725CD8E5800A8454A /* AttributePickerViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB73C1625CD8E5800A8454A /* AttributePickerViewModelTests.swift */; };
879880
AECD57D226DFDF7500A3B580 /* EditOrderAddressForm.swift in Sources */ = {isa = PBXBuildFile; fileRef = AECD57D126DFDF7500A3B580 /* EditOrderAddressForm.swift */; };
@@ -2304,6 +2305,7 @@
23042305
A650BE852578E76600C655E0 /* MockStorageManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockStorageManager.swift; path = ../../../Yosemite/YosemiteTests/Mocks/MockStorageManager.swift; sourceTree = "<group>"; };
23052306
A6557217258B7510008AE7CA /* OrderListCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderListCellViewModel.swift; sourceTree = "<group>"; };
23062307
A655725C258B91AE008AE7CA /* OrderListCellViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderListCellViewModelTests.swift; sourceTree = "<group>"; };
2308+
AE6DBE3A2732CAAD00957E7A /* AdaptiveStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveStack.swift; sourceTree = "<group>"; };
23072309
AEB73C0B25CD734200A8454A /* AttributePickerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributePickerViewModel.swift; sourceTree = "<group>"; };
23082310
AEB73C1625CD8E5800A8454A /* AttributePickerViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributePickerViewModelTests.swift; sourceTree = "<group>"; };
23092311
AECD57D126DFDF7500A3B580 /* EditOrderAddressForm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditOrderAddressForm.swift; sourceTree = "<group>"; };
@@ -4540,6 +4542,7 @@
45404542
DEE6437726D8DAD900888A75 /* InProgressView.swift */,
45414543
26C6E8E926E8FD3900C7BB0F /* LazyView.swift */,
45424544
26C6E8EB26E8FF4800C7BB0F /* LazyNavigationLink.swift */,
4545+
AE6DBE3A2732CAAD00957E7A /* AdaptiveStack.swift */,
45434546
);
45444547
path = "SwiftUI Components";
45454548
sourceTree = "<group>";
@@ -7594,6 +7597,7 @@
75947597
028296EC237D28B600E84012 /* TextViewViewController.swift in Sources */,
75957598
02B8650F24A9E2D800265779 /* Product+SwiftUIPreviewHelpers.swift in Sources */,
75967599
454B28BE23BF63C600CD2091 /* DateIntervalFormatter+Helpers.swift in Sources */,
7600+
AE6DBE3B2732CAAD00957E7A /* AdaptiveStack.swift in Sources */,
75977601
024DF3052372ADCD006658FE /* KeyboardScrollable.swift in Sources */,
75987602
0212276324498CDC0042161F /* ProductFormBottomSheetAction.swift in Sources */,
75997603
E1D4E84426776A6B00256B83 /* HeadlineTableViewCell.swift in Sources */,

0 commit comments

Comments
 (0)