Skip to content

Commit 9d41a44

Browse files
committed
Adds new AnalyticsHubTimeRageAdapter type to help communicate between the business logic ranges and the ui ranges
1 parent 2ef0e41 commit 9d41a44

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import Foundation
2+
3+
/// Type that help us converting and querying common data from the ranges supported for the Analytics Hub.
4+
/// Currently we have two range enum types, One for the list UI and one for the supported ranges in the business layer.
5+
/// This adapter help us sharing some common meta data between those two ranges.
6+
///
7+
struct AnalyticsHubRangeAdapter {
8+
9+
/// Converts an `AnalyticsHubTimeRangeSelection.SelectionType` range into a `AnalyticsTimeRangeCard.Range`.
10+
///
11+
fileprivate static func timeCardRange(from analyticsHubRange: AnalyticsHubTimeRangeSelection.SelectionType) -> AnalyticsTimeRangeCard.Range {
12+
switch analyticsHubRange {
13+
case .custom:
14+
return .custom
15+
case .today:
16+
return .today
17+
case .yesterday:
18+
return .yesterday
19+
case .lastWeek:
20+
return .lastWeek
21+
case .lastMonth:
22+
return .lastMonth
23+
case .lastQuarter:
24+
return .lastQuarter
25+
case .lastYear:
26+
return .lastYear
27+
case .weekToDate:
28+
return .weekToDate
29+
case .monthToDate:
30+
return .monthToDate
31+
case .quarterToDate:
32+
return .quarterToDate
33+
case .yearToDate:
34+
return .yearToDate
35+
}
36+
}
37+
38+
/// Converts an `AnalyticsTimeRangeCard.Range` into a `AnalyticsHubTimeRangeSelection.SelectionType` range.
39+
///
40+
fileprivate static func analyticsHubRange(from timeCardRange: AnalyticsTimeRangeCard.Range) -> AnalyticsHubTimeRangeSelection.SelectionType {
41+
switch timeCardRange {
42+
case .custom:
43+
return .custom(start: Date(), end: Date())
44+
case .today:
45+
return .today
46+
case .yesterday:
47+
return .yesterday
48+
case .lastWeek:
49+
return .lastWeek
50+
case .lastMonth:
51+
return .lastMonth
52+
case .lastQuarter:
53+
return .lastQuarter
54+
case .lastYear:
55+
return .lastYear
56+
case .weekToDate:
57+
return .weekToDate
58+
case .monthToDate:
59+
return .monthToDate
60+
case .quarterToDate:
61+
return .quarterToDate
62+
case .yearToDate:
63+
return .yearToDate
64+
}
65+
}
66+
67+
/// Returns the desciption of the provided `AnalyticsHubTimeRangeSelection.SelectionType`range.
68+
///
69+
static func description(from analyticsHubRange: AnalyticsHubTimeRangeSelection.SelectionType) -> String {
70+
switch analyticsHubRange {
71+
case .custom:
72+
return Localization.custom
73+
case .today:
74+
return Localization.today
75+
case .yesterday:
76+
return Localization.yesterday
77+
case .lastWeek:
78+
return Localization.lastWeek
79+
case .lastMonth:
80+
return Localization.lastMonth
81+
case .lastQuarter:
82+
return Localization.lastQuarter
83+
case .lastYear:
84+
return Localization.lastYear
85+
case .weekToDate:
86+
return Localization.weekToDate
87+
case .monthToDate:
88+
return Localization.monthToDate
89+
case .quarterToDate:
90+
return Localization.quarterToDate
91+
case .yearToDate:
92+
return Localization.yearToDate
93+
}
94+
}
95+
96+
/// Returns the desciption of the provided `AnalyticsTimeRangeCard.Range`.
97+
///
98+
static func description(from timeCardRange: AnalyticsTimeRangeCard.Range) -> String {
99+
switch timeCardRange {
100+
case .custom:
101+
return Localization.custom
102+
case .today:
103+
return Localization.today
104+
case .yesterday:
105+
return Localization.yesterday
106+
case .lastWeek:
107+
return Localization.lastWeek
108+
case .lastMonth:
109+
return Localization.lastMonth
110+
case .lastQuarter:
111+
return Localization.lastQuarter
112+
case .lastYear:
113+
return Localization.lastYear
114+
case .weekToDate:
115+
return Localization.weekToDate
116+
case .monthToDate:
117+
return Localization.monthToDate
118+
case .quarterToDate:
119+
return Localization.quarterToDate
120+
case .yearToDate:
121+
return Localization.yearToDate
122+
}
123+
}
124+
125+
/// Returns the tracks identifier of the provided `AnalyticsHubTimeRangeSelection.SelectionType`.
126+
///
127+
static func tracksIdentifier(from analyticsHubRange: AnalyticsHubTimeRangeSelection.SelectionType) -> String {
128+
switch analyticsHubRange {
129+
case .custom:
130+
return TracksIdentifier.custom
131+
case .today:
132+
return TracksIdentifier.today
133+
case .yesterday:
134+
return TracksIdentifier.yesterday
135+
case .lastWeek:
136+
return TracksIdentifier.lastWeek
137+
case .lastMonth:
138+
return TracksIdentifier.lastMonth
139+
case .lastQuarter:
140+
return TracksIdentifier.lastQuarter
141+
case .lastYear:
142+
return TracksIdentifier.lastYear
143+
case .weekToDate:
144+
return TracksIdentifier.weekToDate
145+
case .monthToDate:
146+
return TracksIdentifier.monthToDate
147+
case .quarterToDate:
148+
return TracksIdentifier.quarterToDate
149+
case .yearToDate:
150+
return TracksIdentifier.yearToDate
151+
}
152+
}
153+
154+
/// Returns the tracks identifier of the provided `AnalyticsTimeRangeCard.Range`.
155+
///
156+
static func tracksIdentifier(from timeCardRange: AnalyticsTimeRangeCard.Range) -> String {
157+
switch timeCardRange {
158+
case .custom:
159+
return TracksIdentifier.custom
160+
case .today:
161+
return TracksIdentifier.today
162+
case .yesterday:
163+
return TracksIdentifier.yesterday
164+
case .lastWeek:
165+
return TracksIdentifier.lastWeek
166+
case .lastMonth:
167+
return TracksIdentifier.lastMonth
168+
case .lastQuarter:
169+
return TracksIdentifier.lastQuarter
170+
case .lastYear:
171+
return TracksIdentifier.lastYear
172+
case .weekToDate:
173+
return TracksIdentifier.weekToDate
174+
case .monthToDate:
175+
return TracksIdentifier.monthToDate
176+
case .quarterToDate:
177+
return TracksIdentifier.quarterToDate
178+
case .yearToDate:
179+
return TracksIdentifier.yearToDate
180+
}
181+
}
182+
}
183+
184+
// MARK: Constants
185+
186+
private extension AnalyticsHubRangeAdapter {
187+
enum TracksIdentifier {
188+
static let custom = "Custom"
189+
static let today = "Today"
190+
static let yesterday = "Yesterday"
191+
static let lastWeek = "Last Week"
192+
static let lastMonth = "Last Month"
193+
static let lastQuarter = "Last Quarter"
194+
static let lastYear = "Last Year"
195+
static let weekToDate = "Week to Date"
196+
static let monthToDate = "Month to Date"
197+
static let quarterToDate = "Quarter to Date"
198+
static let yearToDate = "Year to Date"
199+
}
200+
201+
enum Localization {
202+
static let custom = NSLocalizedString("Custom", comment: "Title of the Analytics Hub Custom selection range")
203+
static let today = NSLocalizedString("Today", comment: "Title of the Analytics Hub Today's selection range")
204+
static let yesterday = NSLocalizedString("Yesterday", comment: "Title of the Analytics Hub Yesterday selection range")
205+
static let lastWeek = NSLocalizedString("Last Week", comment: "Title of the Analytics Hub Last Week selection range")
206+
static let lastMonth = NSLocalizedString("Last Month", comment: "Title of the Analytics Hub Last Month selection range")
207+
static let lastQuarter = NSLocalizedString("Last Quarter", comment: "Title of the Analytics Hub Last Quarter selection range")
208+
static let lastYear = NSLocalizedString("Last Year", comment: "Title of the Analytics Hub Last Year selection range")
209+
static let weekToDate = NSLocalizedString("Week to Date", comment: "Title of the Analytics Hub Week to Date selection range")
210+
static let monthToDate = NSLocalizedString("Month to Date", comment: "Title of the Analytics Hub Month to Date selection range")
211+
static let quarterToDate = NSLocalizedString("Quarter to Date", comment: "Title of the Analytics Hub Quarter to Date selection range")
212+
static let yearToDate = NSLocalizedString("Year to Date", comment: "Title of the Analytics Hub Year to Date selection range")
213+
}
214+
}
215+
216+
// MARK: Convenience Extensitons
217+
extension AnalyticsTimeRangeCard.Range {
218+
219+
var description: String {
220+
AnalyticsHubRangeAdapter.description(from: self)
221+
}
222+
223+
var tracksIdentifier: String {
224+
AnalyticsHubRangeAdapter.tracksIdentifier(from: self)
225+
}
226+
227+
var asAnalyticsHubRange: AnalyticsHubTimeRangeSelection.SelectionType {
228+
AnalyticsHubRangeAdapter.analyticsHubRange(from: self)
229+
}
230+
}
231+
232+
extension AnalyticsHubTimeRangeSelection.SelectionType {
233+
var description: String {
234+
AnalyticsHubRangeAdapter.description(from: self)
235+
}
236+
237+
var tracksIdentifier: String {
238+
AnalyticsHubRangeAdapter.tracksIdentifier(from: self)
239+
}
240+
241+
var asTimeCardRange: AnalyticsTimeRangeCard.Range {
242+
AnalyticsHubRangeAdapter.timeCardRange(from: self)
243+
}
244+
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@
657657
26CCBE0B2523B3650073F94D /* RefundProductsTotalTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26CCBE0A2523B3650073F94D /* RefundProductsTotalTableViewCell.swift */; };
658658
26CCBE0D2523C2560073F94D /* RefundProductsTotalTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 26CCBE0C2523C2560073F94D /* RefundProductsTotalTableViewCell.xib */; };
659659
26CFDB2727357E8000AB940B /* SimplePaymentsSummary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26CFDB2627357E8000AB940B /* SimplePaymentsSummary.swift */; };
660+
26D1E9E82949818B00A7DC62 /* AnalyticsHubTimeRageAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26D1E9E72949818B00A7DC62 /* AnalyticsHubTimeRageAdapter.swift */; };
660661
26D9E54428C107F80098DF26 /* WooFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 26D9E54328C107F80098DF26 /* WooFoundation.framework */; };
661662
26D9E54828C10A3B0098DF26 /* Experiments.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 26D9E54728C10A3B0098DF26 /* Experiments.framework */; };
662663
26DB7E3528636D2200506173 /* NonEditableOrderBanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26DB7E3428636D2200506173 /* NonEditableOrderBanner.swift */; };
@@ -2678,6 +2679,7 @@
26782679
26CCBE0A2523B3650073F94D /* RefundProductsTotalTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefundProductsTotalTableViewCell.swift; sourceTree = "<group>"; };
26792680
26CCBE0C2523C2560073F94D /* RefundProductsTotalTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = RefundProductsTotalTableViewCell.xib; sourceTree = "<group>"; };
26802681
26CFDB2627357E8000AB940B /* SimplePaymentsSummary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimplePaymentsSummary.swift; sourceTree = "<group>"; };
2682+
26D1E9E72949818B00A7DC62 /* AnalyticsHubTimeRageAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubTimeRageAdapter.swift; sourceTree = "<group>"; };
26812683
26D9E54328C107F80098DF26 /* WooFoundation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = WooFoundation.framework; sourceTree = BUILT_PRODUCTS_DIR; };
26822684
26D9E54728C10A3B0098DF26 /* Experiments.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Experiments.framework; sourceTree = BUILT_PRODUCTS_DIR; };
26832685
26DB7E3428636D2200506173 /* NonEditableOrderBanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonEditableOrderBanner.swift; sourceTree = "<group>"; };
@@ -7535,6 +7537,7 @@
75357537
B6F379672937836700718561 /* AnalyticsHubTimeRange.swift */,
75367538
B66D6CEB29396A3E0075D4AF /* AnalyticsHubTimeRangeData.swift */,
75377539
B6440FB5292E72DA0012D506 /* AnalyticsHubTimeRangeSelection.swift */,
7540+
26D1E9E72949818B00A7DC62 /* AnalyticsHubTimeRageAdapter.swift */,
75387541
);
75397542
path = "Time Range";
75407543
sourceTree = "<group>";
@@ -10045,6 +10048,7 @@
1004510048
025678C125773236009D7E6C /* Collection+ShippingLabel.swift in Sources */,
1004610049
456931842653E9F2009ED69D /* ShippingLabelCarrierRow.swift in Sources */,
1004710050
09BE3A8E27C91E730070B69D /* BulkUpdatePriceSettingsViewModel.swift in Sources */,
10051+
26D1E9E82949818B00A7DC62 /* AnalyticsHubTimeRageAdapter.swift in Sources */,
1004810052
CE32B11A20BF8E32006FBCF4 /* UIButton+Helpers.swift in Sources */,
1004910053
45BBFBC5274FDCE900213001 /* HubMenu.swift in Sources */,
1005010054
02A9BCD62737F73C00159C79 /* JetpackBenefitItem.swift in Sources */,

0 commit comments

Comments
 (0)