Skip to content

Commit a166a9f

Browse files
authored
Merge pull request #8330 from woocommerce/issue/8200-prototype
Analytics Hub: Custom Date Selector UI
2 parents 7a75434 + 6a0ca6d commit a166a9f

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import Foundation
2+
import SwiftUI
3+
4+
/// View to select a custom date range.
5+
/// Consists of two date pickers laid out vertically.
6+
///
7+
struct RangedDatePicker: View {
8+
9+
@Environment(\.presentationMode) var presentation
10+
11+
/// Closure invoked when the custom date range has been confirmed.
12+
///
13+
var datesSelected: ((_ start: Date, _ end: Date) -> Void)?
14+
15+
/// Start date binding variable
16+
///
17+
@State private var startDate = Date()
18+
19+
/// End date binding variable
20+
///
21+
@State private var endDate = Date()
22+
23+
var body: some View {
24+
NavigationView {
25+
ScrollView {
26+
VStack(alignment: .leading) {
27+
28+
// Start Picker
29+
Text(Localization.startDate)
30+
.foregroundColor(Color(.accent))
31+
.headlineStyle()
32+
33+
Divider()
34+
35+
DatePicker("", selection: $startDate, in: ...Date(), displayedComponents: [.date])
36+
.datePickerStyle(.graphical)
37+
.accentColor(Color(.brand))
38+
39+
// End Picker
40+
Text(Localization.endDate)
41+
.foregroundColor(Color(.accent))
42+
.headlineStyle()
43+
44+
Divider()
45+
46+
DatePicker("", selection: $endDate, in: ...Date(), displayedComponents: [.date])
47+
.datePickerStyle(.graphical)
48+
.accentColor(Color(.brand))
49+
}
50+
.padding()
51+
}
52+
.navigationBarTitleDisplayMode(.inline)
53+
.toolbar(content: {
54+
ToolbarItem(placement: .principal) {
55+
// Navigation Bar title
56+
VStack(spacing: Layout.titleSpacing) {
57+
Text(Localization.title)
58+
.headlineStyle()
59+
60+
// TODO: Properly format date ranges outside the view
61+
Text("\(DateFormatter.monthAndDayFormatter.string(from: startDate)) - \(DateFormatter.monthAndDayFormatter.string(from: endDate))")
62+
.captionStyle()
63+
}
64+
}
65+
ToolbarItem(placement: .confirmationAction) {
66+
Button(action: {
67+
presentation.wrappedValue.dismiss()
68+
datesSelected?(startDate, endDate)
69+
}, label: {
70+
Text(Localization.apply)
71+
})
72+
}
73+
ToolbarItem(placement: .cancellationAction) {
74+
Button(action: {
75+
presentation.wrappedValue.dismiss()
76+
}, label: {
77+
Image(uiImage: .closeButton)
78+
})
79+
}
80+
})
81+
}
82+
.navigationViewStyle(.stack)
83+
.wooNavigationBarStyle()
84+
}
85+
}
86+
87+
// MARK: Constant
88+
89+
private extension RangedDatePicker {
90+
enum Localization {
91+
static let title = NSLocalizedString("Custom Date Range", comment: "Title in custom range date picker")
92+
static let apply = NSLocalizedString("Apply", comment: "Apply navigation button in custom range date picker")
93+
static let startDate = NSLocalizedString("Start Date", comment: "Start Date label in custom range date picker")
94+
static let endDate = NSLocalizedString("End Date", comment: "End Date label in custom range date picker")
95+
}
96+
enum Layout {
97+
static let titleSpacing: CGFloat = 4.0
98+
}
99+
}
100+
101+
// MARK: Previews
102+
103+
struct RangedDatePickerPreview: PreviewProvider {
104+
static var previews: some View {
105+
RangedDatePicker()
106+
}
107+
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@
638638
26B3D8A0252235C50054C319 /* RefundShippingDetailsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B3D89F252235C50054C319 /* RefundShippingDetailsViewModel.swift */; };
639639
26B3EC622744772A0075EAE6 /* SimplePaymentsSummaryViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B3EC612744772A0075EAE6 /* SimplePaymentsSummaryViewModelTests.swift */; };
640640
26B3EC642745916F0075EAE6 /* BindableTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B3EC632745916F0075EAE6 /* BindableTextField.swift */; };
641+
26B71DB6293FE490004D8052 /* RangedDatePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B71DB5293FE490004D8052 /* RangedDatePicker.swift */; };
641642
26B98758273C5BE30090E8CA /* EditCustomerNoteViewModelProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B98757273C5BE30090E8CA /* EditCustomerNoteViewModelProtocol.swift */; };
642643
26B9875D273C6A830090E8CA /* SimplePaymentsNoteViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B9875C273C6A830090E8CA /* SimplePaymentsNoteViewModel.swift */; };
643644
26B9875F273CB6AA0090E8CA /* SimplePaymentsNoteViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B9875E273CB6AA0090E8CA /* SimplePaymentsNoteViewModelTests.swift */; };
@@ -2652,6 +2653,7 @@
26522653
26B3D89F252235C50054C319 /* RefundShippingDetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefundShippingDetailsViewModel.swift; sourceTree = "<group>"; };
26532654
26B3EC612744772A0075EAE6 /* SimplePaymentsSummaryViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimplePaymentsSummaryViewModelTests.swift; sourceTree = "<group>"; };
26542655
26B3EC632745916F0075EAE6 /* BindableTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BindableTextField.swift; sourceTree = "<group>"; };
2656+
26B71DB5293FE490004D8052 /* RangedDatePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RangedDatePicker.swift; sourceTree = "<group>"; };
26552657
26B98757273C5BE30090E8CA /* EditCustomerNoteViewModelProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditCustomerNoteViewModelProtocol.swift; sourceTree = "<group>"; };
26562658
26B9875C273C6A830090E8CA /* SimplePaymentsNoteViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimplePaymentsNoteViewModel.swift; sourceTree = "<group>"; };
26572659
26B9875E273CB6AA0090E8CA /* SimplePaymentsNoteViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimplePaymentsNoteViewModelTests.swift; sourceTree = "<group>"; };
@@ -6217,6 +6219,7 @@
62176219
DE2FE5872925DD950018040A /* JetpackInstallHeaderView.swift */,
62186220
26E7EE6F29300F6200793045 /* DeltaTag.swift */,
62196221
AEE9A87F293A3E5500227C92 /* RefreshablePlainList.swift */,
6222+
26B71DB5293FE490004D8052 /* RangedDatePicker.swift */,
62206223
);
62216224
path = "SwiftUI Components";
62226225
sourceTree = "<group>";
@@ -9964,6 +9967,7 @@
99649967
7459A6C621B0680300F83A78 /* RequirementsChecker.swift in Sources */,
99659968
CE1D5A55228A0AD200DF3715 /* TwoColumnTableViewCell.swift in Sources */,
99669969
02759B9128FFA09600918176 /* StoreCreationWebViewModel.swift in Sources */,
9970+
26B71DB6293FE490004D8052 /* RangedDatePicker.swift in Sources */,
99679971
74460D4222289C7A00D7316A /* StorePickerCoordinator.swift in Sources */,
99689972
FEDD70AF26A7223500194C3A /* StorageEligibilityErrorInfo+Woo.swift in Sources */,
99699973
AE9E04752776213E003FA09E /* OrderCustomerSection.swift in Sources */,

0 commit comments

Comments
 (0)