Skip to content

Commit cc0e7bd

Browse files
authored
Merge pull request #8325 from woocommerce/issue/8146-introduce-quarter-ranges
2 parents a166a9f + 90382fa commit cc0e7bd

File tree

6 files changed

+216
-34
lines changed

6 files changed

+216
-34
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/Time Range/AnalyticsHubTimeRangeSelection.swift

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,16 @@ public class AnalyticsHubTimeRangeSelection {
3333
currentDate: Date = Date(),
3434
timezone: TimeZone = TimeZone.current,
3535
calendar: Calendar = Locale.current.calendar) {
36-
var selectionData: AnalyticsHubTimeRangeData
37-
38-
switch selectionType {
39-
case .today:
40-
selectionData = AnalyticsHubTodayRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
41-
case .yesterday:
42-
selectionData = AnalyticsHubYesterdayRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
43-
case .lastWeek:
44-
selectionData = AnalyticsHubLastWeekRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
45-
case .lastMonth:
46-
selectionData = AnalyticsHubLastMonthRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
47-
case .lastYear:
48-
selectionData = AnalyticsHubLastYearRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
49-
case .weekToDate:
50-
selectionData = AnalyticsHubWeekToDateRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
51-
case .monthToDate:
52-
selectionData = AnalyticsHubMonthToDateRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
53-
case .yearToDate:
54-
selectionData = AnalyticsHubYearToDateRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
55-
}
56-
36+
let selectionData = selectionType.toRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
5737
let currentTimeRange = selectionData.currentTimeRange
5838
let previousTimeRange = selectionData.previousTimeRange
39+
let useShortFormat = selectionType == .today || selectionType == .yesterday
40+
5941
self.currentTimeRange = currentTimeRange
6042
self.previousTimeRange = previousTimeRange
61-
62-
let simplifiedDescription = selectionType == .today || selectionType == .yesterday
63-
self.formattedCurrentRangeText = currentTimeRange?.formatToString(simplified: simplifiedDescription,
64-
timezone: timezone,
65-
calendar: calendar)
66-
self.formattedPreviousRangeText = previousTimeRange?.formatToString(simplified: simplifiedDescription,
67-
timezone: timezone,
68-
calendar: calendar)
6943
self.rangeSelectionDescription = selectionType.description
44+
self.formattedCurrentRangeText = currentTimeRange?.formatToString(simplified: useShortFormat, timezone: timezone, calendar: calendar)
45+
self.formattedPreviousRangeText = previousTimeRange?.formatToString(simplified: useShortFormat, timezone: timezone, calendar: calendar)
7046
}
7147

7248
/// Unwrap the generated selected `AnalyticsHubTimeRange` based on the `selectedTimeRange`
@@ -96,9 +72,11 @@ extension AnalyticsHubTimeRangeSelection {
9672
case yesterday
9773
case lastWeek
9874
case lastMonth
75+
case lastQuarter
9976
case lastYear
10077
case weekToDate
10178
case monthToDate
79+
case quarterToDate
10280
case yearToDate
10381

10482
var description: String {
@@ -111,12 +89,16 @@ extension AnalyticsHubTimeRangeSelection {
11189
return Localization.lastWeek
11290
case .lastMonth:
11391
return Localization.lastMonth
92+
case .lastQuarter:
93+
return Localization.lastQuarter
11494
case .lastYear:
11595
return Localization.lastYear
11696
case .weekToDate:
11797
return Localization.weekToDate
11898
case .monthToDate:
11999
return Localization.monthToDate
100+
case .quarterToDate:
101+
return Localization.quarterToDate
120102
case .yearToDate:
121103
return Localization.yearToDate
122104
}
@@ -136,6 +118,35 @@ extension AnalyticsHubTimeRangeSelection {
136118
}
137119
}
138120
}
121+
122+
// MARK: - SelectionType helper functions
123+
private extension AnalyticsHubTimeRangeSelection.SelectionType {
124+
func toRangeData(referenceDate: Date, timezone: TimeZone, calendar: Calendar) -> AnalyticsHubTimeRangeData {
125+
switch self {
126+
case .today:
127+
return AnalyticsHubTodayRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
128+
case .yesterday:
129+
return AnalyticsHubYesterdayRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
130+
case .lastWeek:
131+
return AnalyticsHubLastWeekRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
132+
case .lastMonth:
133+
return AnalyticsHubLastMonthRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
134+
case .lastQuarter:
135+
return AnalyticsHubLastQuarterRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
136+
case .lastYear:
137+
return AnalyticsHubLastYearRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
138+
case .weekToDate:
139+
return AnalyticsHubWeekToDateRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
140+
case .monthToDate:
141+
return AnalyticsHubMonthToDateRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
142+
case .quarterToDate:
143+
return AnalyticsHubQuarterToDateRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
144+
case .yearToDate:
145+
return AnalyticsHubYearToDateRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
146+
}
147+
}
148+
}
149+
139150
// MARK: - Constants
140151
extension AnalyticsHubTimeRangeSelection {
141152
enum TimeRangeGeneratorError: Error {
@@ -148,9 +159,11 @@ extension AnalyticsHubTimeRangeSelection {
148159
static let yesterday = NSLocalizedString("Yesterday", comment: "Title of the Analytics Hub Yesterday selection range")
149160
static let lastWeek = NSLocalizedString("Last Week", comment: "Title of the Analytics Hub Last Week selection range")
150161
static let lastMonth = NSLocalizedString("Last Month", comment: "Title of the Analytics Hub Last Month selection range")
162+
static let lastQuarter = NSLocalizedString("Last Quarter", comment: "Title of the Analytics Hub Last Quarter selection range")
151163
static let lastYear = NSLocalizedString("Last Year", comment: "Title of the Analytics Hub Last Year selection range")
152164
static let weekToDate = NSLocalizedString("Week to Date", comment: "Title of the Analytics Hub Week to Date selection range")
153165
static let monthToDate = NSLocalizedString("Month to Date", comment: "Title of the Analytics Hub Month to Date selection range")
166+
static let quarterToDate = NSLocalizedString("Quarter to Date", comment: "Title of the Analytics Hub Quarter to Date selection range")
154167
static let yearToDate = NSLocalizedString("Year to Date", comment: "Title of the Analytics Hub Year to Date selection range")
155168
static let selectionTitle = NSLocalizedString("Date Range", comment: "Title of the range selection list")
156169
static let noCurrentPeriodAvailable = NSLocalizedString("No current period available",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
3+
/// Responsible for defining two ranges of data, one starting from the first day of the last quarter
4+
/// until the final day of that same quarter, and the previous one as two quarters ago, also starting
5+
/// from the first day until the final day of that quarter. E. g.
6+
///
7+
/// Today: 29 Jul 2022
8+
/// Current range: Apr 1 until Jun 30, 2022
9+
/// Previous range: Jan 1 until Mar 31, 2022
10+
///
11+
struct AnalyticsHubLastQuarterRangeData: AnalyticsHubTimeRangeData {
12+
let currentDateStart: Date?
13+
let currentDateEnd: Date?
14+
let previousDateStart: Date?
15+
let previousDateEnd: Date?
16+
17+
init(referenceDate: Date, timezone: TimeZone, calendar: Calendar) {
18+
let oneQuarterAgo = calendar.date(byAdding: .month, value: -3, to: referenceDate)
19+
self.currentDateEnd = oneQuarterAgo?.endOfQuarter(timezone: timezone, calendar: calendar)
20+
self.currentDateStart = oneQuarterAgo?.startOfQuarter(timezone: timezone, calendar: calendar)
21+
22+
let twoQuartersAgo = calendar.date(byAdding: .month, value: -6, to: referenceDate)
23+
self.previousDateEnd = twoQuartersAgo?.endOfQuarter(timezone: timezone, calendar: calendar)
24+
self.previousDateStart = twoQuartersAgo?.startOfQuarter(timezone: timezone, calendar: calendar)
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
3+
/// Responsible for defining two ranges of data, one starting from the first day of the current quarter
4+
/// until the current date and the previous one, starting from the first day of the previous quarter
5+
/// until the same relative day of the previous quarter. E. g.
6+
///
7+
/// Today: 15 Feb 2022
8+
/// Current range: Jan 1 until Feb 15, 2022
9+
/// Previous range: Oct 1 until Nov 15, 2021
10+
///
11+
struct AnalyticsHubQuarterToDateRangeData: AnalyticsHubTimeRangeData {
12+
let currentDateStart: Date?
13+
let currentDateEnd: Date?
14+
let previousDateStart: Date?
15+
let previousDateEnd: Date?
16+
17+
init(referenceDate: Date, timezone: TimeZone, calendar: Calendar) {
18+
self.currentDateEnd = referenceDate
19+
self.currentDateStart = referenceDate.startOfQuarter(timezone: timezone, calendar: calendar)
20+
let previousDateEnd = calendar.date(byAdding: .month, value: -3, to: referenceDate)
21+
self.previousDateEnd = previousDateEnd
22+
self.previousDateStart = previousDateEnd?.startOfQuarter(timezone: timezone, calendar: calendar)
23+
}
24+
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,11 +1360,13 @@
13601360
B622BC74289CF19400B10CEC /* WaitingTimeTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B622BC73289CF19400B10CEC /* WaitingTimeTrackerTests.swift */; };
13611361
B626C71B287659D60083820C /* OrderCustomFieldsDetails.swift in Sources */ = {isa = PBXBuildFile; fileRef = B626C71A287659D60083820C /* OrderCustomFieldsDetails.swift */; };
13621362
B63AAF4B254AD2C6000B28A2 /* URL+SurveyViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B63AAF4A254AD2C6000B28A2 /* URL+SurveyViewControllerTests.swift */; };
1363+
B63D9009293E56E300BB5C9D /* AnalyticsHubQuarterToDateRangeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B63D9008293E56E300BB5C9D /* AnalyticsHubQuarterToDateRangeData.swift */; };
13631364
B6440FB6292E72DA0012D506 /* AnalyticsHubTimeRangeSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6440FB5292E72DA0012D506 /* AnalyticsHubTimeRangeSelection.swift */; };
13641365
B6440FB9292E74230012D506 /* AnalyticsHubTimeRangeSelectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6440FB8292E74230012D506 /* AnalyticsHubTimeRangeSelectionTests.swift */; };
13651366
B651474527D644FF00C9C4E6 /* CustomerNoteSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B651474427D644FF00C9C4E6 /* CustomerNoteSection.swift */; };
13661367
B66D6CEC29396A3E0075D4AF /* AnalyticsHubTimeRangeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B66D6CEB29396A3E0075D4AF /* AnalyticsHubTimeRangeData.swift */; };
13671368
B687940C27699D420092BCA0 /* RefundFeesCalculationUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = B687940B27699D410092BCA0 /* RefundFeesCalculationUseCase.swift */; };
1369+
B6930BDA293FD1EE00C6FFDB /* AnalyticsHubLastQuarterRangeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6930BD9293FD1EE00C6FFDB /* AnalyticsHubLastQuarterRangeData.swift */; };
13681370
B6A10E9C292E5DEE00790797 /* AnalyticsTimeRangeCardViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6A10E9B292E5DEE00790797 /* AnalyticsTimeRangeCardViewModel.swift */; };
13691371
B6C78B8C293BADAA008934A1 /* AnalyticsHubLastWeekRangeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C78B8B293BADAA008934A1 /* AnalyticsHubLastWeekRangeData.swift */; };
13701372
B6C78B8E293BAE68008934A1 /* AnalyticsHubLastMonthRangeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C78B8D293BAE68008934A1 /* AnalyticsHubLastMonthRangeData.swift */; };
@@ -3384,11 +3386,13 @@
33843386
B622BC73289CF19400B10CEC /* WaitingTimeTrackerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaitingTimeTrackerTests.swift; sourceTree = "<group>"; };
33853387
B626C71A287659D60083820C /* OrderCustomFieldsDetails.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderCustomFieldsDetails.swift; sourceTree = "<group>"; };
33863388
B63AAF4A254AD2C6000B28A2 /* URL+SurveyViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+SurveyViewControllerTests.swift"; sourceTree = "<group>"; };
3389+
B63D9008293E56E300BB5C9D /* AnalyticsHubQuarterToDateRangeData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubQuarterToDateRangeData.swift; sourceTree = "<group>"; };
33873390
B6440FB5292E72DA0012D506 /* AnalyticsHubTimeRangeSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubTimeRangeSelection.swift; sourceTree = "<group>"; };
33883391
B6440FB8292E74230012D506 /* AnalyticsHubTimeRangeSelectionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubTimeRangeSelectionTests.swift; sourceTree = "<group>"; };
33893392
B651474427D644FF00C9C4E6 /* CustomerNoteSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerNoteSection.swift; sourceTree = "<group>"; };
33903393
B66D6CEB29396A3E0075D4AF /* AnalyticsHubTimeRangeData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubTimeRangeData.swift; sourceTree = "<group>"; };
33913394
B687940B27699D410092BCA0 /* RefundFeesCalculationUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefundFeesCalculationUseCase.swift; sourceTree = "<group>"; };
3395+
B6930BD9293FD1EE00C6FFDB /* AnalyticsHubLastQuarterRangeData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubLastQuarterRangeData.swift; sourceTree = "<group>"; };
33923396
B6A10E9B292E5DEE00790797 /* AnalyticsTimeRangeCardViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsTimeRangeCardViewModel.swift; sourceTree = "<group>"; };
33933397
B6C78B8B293BADAA008934A1 /* AnalyticsHubLastWeekRangeData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubLastWeekRangeData.swift; sourceTree = "<group>"; };
33943398
B6C78B8D293BAE68008934A1 /* AnalyticsHubLastMonthRangeData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubLastMonthRangeData.swift; sourceTree = "<group>"; };
@@ -7494,25 +7498,27 @@
74947498
B66D6CEF293A4D990075D4AF /* Range Data Generation */ = {
74957499
isa = PBXGroup;
74967500
children = (
7497-
B6F3796929378B3900718561 /* AnalyticsHubMonthToDateRangeData.swift */,
7498-
B6F3796B293794A000718561 /* AnalyticsHubYearToDateRangeData.swift */,
7499-
B6F3796D293796BC00718561 /* AnalyticsHubWeekToDateRangeData.swift */,
75007501
B6F3796F293798ED00718561 /* AnalyticsHubTodayRangeData.swift */,
75017502
B6E7DB63293A7C390049B001 /* AnalyticsHubYesterdayRangeData.swift */,
75027503
B6C78B8B293BADAA008934A1 /* AnalyticsHubLastWeekRangeData.swift */,
75037504
B6C78B8D293BAE68008934A1 /* AnalyticsHubLastMonthRangeData.swift */,
7505+
B6930BD9293FD1EE00C6FFDB /* AnalyticsHubLastQuarterRangeData.swift */,
75047506
B6C78B8F293BAF37008934A1 /* AnalyticsHubLastYearRangeData.swift */,
7507+
B6F3796D293796BC00718561 /* AnalyticsHubWeekToDateRangeData.swift */,
7508+
B6F3796929378B3900718561 /* AnalyticsHubMonthToDateRangeData.swift */,
7509+
B63D9008293E56E300BB5C9D /* AnalyticsHubQuarterToDateRangeData.swift */,
7510+
B6F3796B293794A000718561 /* AnalyticsHubYearToDateRangeData.swift */,
75057511
);
75067512
path = "Range Data Generation";
75077513
sourceTree = "<group>";
75087514
};
75097515
B6F379662937831D00718561 /* Time Range */ = {
75107516
isa = PBXGroup;
75117517
children = (
7512-
B66D6CEB29396A3E0075D4AF /* AnalyticsHubTimeRangeData.swift */,
75137518
B66D6CEF293A4D990075D4AF /* Range Data Generation */,
7514-
B6440FB5292E72DA0012D506 /* AnalyticsHubTimeRangeSelection.swift */,
75157519
B6F379672937836700718561 /* AnalyticsHubTimeRange.swift */,
7520+
B66D6CEB29396A3E0075D4AF /* AnalyticsHubTimeRangeData.swift */,
7521+
B6440FB5292E72DA0012D506 /* AnalyticsHubTimeRangeSelection.swift */,
75167522
);
75177523
path = "Time Range";
75187524
sourceTree = "<group>";
@@ -10739,6 +10745,7 @@
1073910745
57EBC92024EEE61800C1D45B /* WooAnalyticsEvent.swift in Sources */,
1074010746
57CDABB9252E9BEB00BED88C /* ButtonTableFooterView.swift in Sources */,
1074110747
021940E8291FDBF90090354E /* StoreCreationSummaryView.swift in Sources */,
10748+
B63D9009293E56E300BB5C9D /* AnalyticsHubQuarterToDateRangeData.swift in Sources */,
1074210749
02E4FD7E2306A8180049610C /* StatsTimeRangeBarViewModel.swift in Sources */,
1074310750
45D875D22611EA2100226C3F /* ListHeaderView.swift in Sources */,
1074410751
DE2FE58D292617C30018040A /* SiteCredentialLoginViewModel.swift in Sources */,
@@ -10980,6 +10987,7 @@
1098010987
02C88775245036D400E4470F /* FilterProductListViewModel.swift in Sources */,
1098110988
4590B6A8261F0F8300A6FCE0 /* SegmentedView.swift in Sources */,
1098210989
DE77889826FCA39B008DFF44 /* TitleAndSubtitleRow.swift in Sources */,
10990+
B6930BDA293FD1EE00C6FFDB /* AnalyticsHubLastQuarterRangeData.swift in Sources */,
1098310991
02DE5CA9279F857D007CBEF3 /* Double+Rounding.swift in Sources */,
1098410992
02A65301246AA63600755A01 /* ProductDetailsFactory.swift in Sources */,
1098510993
D449C51D26DE6B5000D75B02 /* LargeTitle.swift in Sources */,

0 commit comments

Comments
 (0)