Skip to content

Commit ac86b24

Browse files
committed
Adds a new Range enum only meant for ui consumption
1 parent 9d41a44 commit ac86b24

File tree

2 files changed

+46
-103
lines changed

2 files changed

+46
-103
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/AnalyticsTimeRangeCard.swift

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ struct AnalyticsTimeRangeCard: View {
3131
createTimeRangeContent()
3232
.sheet(isPresented: $showTimeRangeSelectionView) {
3333
SelectionList(title: Localization.timeRangeSelectionTitle,
34-
items: AnalyticsHubTimeRangeSelection.SelectionType.allCases,
34+
items: Range.allCases,
3535
contentKeyPath: \.description,
3636
selected: internalSelectionBinding()) { selection in
3737
usageTracksEventEmitter.interacted()
3838
ServiceLocator.analytics.track(event: .AnalyticsHub.dateRangeOptionSelected(selection.tracksIdentifier))
3939
}
4040
.sheet(isPresented: $showCustomRangeSelectionView) {
41+
// TODO: Pass real dates here
4142
RangedDatePicker() { start, end in
4243
showTimeRangeSelectionView = false // Dismiss the initial sheet for a smooth transition
4344
self.selectionType = .custom(start: start, end: end)
@@ -96,26 +97,19 @@ struct AnalyticsTimeRangeCard: View {
9697
/// Tracks the range selection internally to determine if the custom range selection should be presented or not.
9798
/// If custom range selection is not needed, the internal selection is forwarded to `selectionType`.
9899
///
99-
private func internalSelectionBinding() -> Binding<AnalyticsHubTimeRangeSelection.SelectionType> {
100+
private func internalSelectionBinding() -> Binding<Range> {
100101
.init(
101102
get: {
102-
// Temporary
103-
switch selectionType {
104-
// If a `custom` case is set return one with nil values so the Custom row is selected
105-
case .custom:
106-
return .custom(start: nil, end: nil)
107-
default:
108-
return selectionType
109-
}
103+
return selectionType.asTimeCardRange
110104
},
111105
set: { newValue in
112106
switch newValue {
113-
// If we get a `custom` case with nil dates it is because we need to present the custom range selection
114-
case .custom(start: nil, end: nil):
107+
// If we get a `custom` case it is because we need to present the custom range selection
108+
case .custom:
115109
showCustomRangeSelectionView = true
116110
default:
117111
// Any other selection should be forwarded to our parent binding.
118-
selectionType = newValue
112+
selectionType = newValue.asAnalyticsHubRange
119113
}
120114
}
121115
)
@@ -154,3 +148,39 @@ struct TimeRangeCard_Previews: PreviewProvider {
154148
AnalyticsTimeRangeCard(viewModel: viewModel, selectionType: .constant(.monthToDate))
155149
}
156150
}
151+
152+
153+
extension AnalyticsTimeRangeCard {
154+
enum Range: CaseIterable {
155+
case custom
156+
case today
157+
case yesterday
158+
case lastWeek
159+
case lastMonth
160+
case lastQuarter
161+
case lastYear
162+
case weekToDate
163+
case monthToDate
164+
case quarterToDate
165+
case yearToDate
166+
167+
/// Wee need to provide a custom `allCases` in order to evict `.custom` while the feature flag is active.
168+
/// We should delete this once the feature flag has been removed.
169+
///
170+
static var allCases: [Range] {
171+
[
172+
ServiceLocator.featureFlagService.isFeatureFlagEnabled(.analyticsHub) ? .custom : nil,
173+
.today,
174+
.yesterday,
175+
.lastWeek,
176+
.lastMonth,
177+
.lastQuarter,
178+
.lastYear,
179+
.weekToDate,
180+
.monthToDate,
181+
.quarterToDate,
182+
yearToDate
183+
].compactMap { $0 }
184+
}
185+
}
186+
}

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

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,9 @@ public class AnalyticsHubTimeRangeSelection {
7777

7878
// MARK: - Time Range Selection Type
7979
extension AnalyticsHubTimeRangeSelection {
80-
enum SelectionType: CaseIterable, Equatable, Hashable {
81-
/// Wee need to provide a custom `allCases` because the `.custom(Date?, Date?)`case disables its synthetization.
82-
///
83-
static var allCases: [AnalyticsHubTimeRangeSelection.SelectionType] {
84-
[
85-
ServiceLocator.featureFlagService.isFeatureFlagEnabled(.analyticsHub) ? .custom(start: nil, end: nil) : nil,
86-
.today,
87-
.yesterday,
88-
.lastWeek,
89-
.lastMonth,
90-
.lastQuarter,
91-
.lastYear,
92-
.weekToDate,
93-
.monthToDate,
94-
.quarterToDate,
95-
yearToDate
96-
].compactMap { $0 }
97-
}
80+
enum SelectionType: Equatable {
9881

99-
// When adding a new case, remember to add it to `allCases`.
100-
case custom(start: Date?, end: Date?)
82+
case custom(start: Date, end: Date)
10183
case today
10284
case yesterday
10385
case lastWeek
@@ -109,33 +91,6 @@ extension AnalyticsHubTimeRangeSelection {
10991
case quarterToDate
11092
case yearToDate
11193

112-
var description: String {
113-
switch self {
114-
case .custom:
115-
return Localization.custom
116-
case .today:
117-
return Localization.today
118-
case .yesterday:
119-
return Localization.yesterday
120-
case .lastWeek:
121-
return Localization.lastWeek
122-
case .lastMonth:
123-
return Localization.lastMonth
124-
case .lastQuarter:
125-
return Localization.lastQuarter
126-
case .lastYear:
127-
return Localization.lastYear
128-
case .weekToDate:
129-
return Localization.weekToDate
130-
case .monthToDate:
131-
return Localization.monthToDate
132-
case .quarterToDate:
133-
return Localization.quarterToDate
134-
case .yearToDate:
135-
return Localization.yearToDate
136-
}
137-
}
138-
13994
/// The granularity that should be used to request stats from the given SelectedType
14095
///
14196
var granularity: StatsGranularityV4 {
@@ -171,33 +126,6 @@ extension AnalyticsHubTimeRangeSelection {
171126
}
172127
}
173128

174-
var tracksIdentifier: String {
175-
switch self {
176-
case .custom:
177-
return "Custom"
178-
case .today:
179-
return "Today"
180-
case .yesterday:
181-
return "Yesterday"
182-
case .lastWeek:
183-
return "Last Week"
184-
case .lastMonth:
185-
return "Last Month"
186-
case .lastQuarter:
187-
return "Last Quarter"
188-
case .lastYear:
189-
return "Last Year"
190-
case .weekToDate:
191-
return "Week to Date"
192-
case .monthToDate:
193-
return "Month to Date"
194-
case .quarterToDate:
195-
return "Quarter to Date"
196-
case .yearToDate:
197-
return "Year to Date"
198-
}
199-
}
200-
201129
init(_ statsTimeRange: StatsTimeRangeV4) {
202130
switch statsTimeRange {
203131
case .today:
@@ -217,12 +145,8 @@ extension AnalyticsHubTimeRangeSelection {
217145
private extension AnalyticsHubTimeRangeSelection.SelectionType {
218146
func toRangeData(referenceDate: Date, timezone: TimeZone, calendar: Calendar) -> AnalyticsHubTimeRangeData? {
219147
switch self {
220-
case let .custom(start?, end?):
148+
case let .custom(start, end):
221149
return AnalyticsHubCustomRangeData(start: start, end: end, timezone: timezone, calendar: calendar)
222-
case .custom:
223-
// Nil custom dates are not supported but can exists when the user has selected the custom range option but hasn't choosen dates yet.
224-
// To properly fix this, we should decouple UI selection types, from ranges selection types.
225-
return nil
226150
case .today:
227151
return AnalyticsHubTodayRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
228152
case .yesterday:
@@ -255,17 +179,6 @@ extension AnalyticsHubTimeRangeSelection {
255179
}
256180

257181
enum Localization {
258-
static let custom = NSLocalizedString("Custom", comment: "Title of the Analytics Hub Custom selection range")
259-
static let today = NSLocalizedString("Today", comment: "Title of the Analytics Hub Today's selection range")
260-
static let yesterday = NSLocalizedString("Yesterday", comment: "Title of the Analytics Hub Yesterday selection range")
261-
static let lastWeek = NSLocalizedString("Last Week", comment: "Title of the Analytics Hub Last Week selection range")
262-
static let lastMonth = NSLocalizedString("Last Month", comment: "Title of the Analytics Hub Last Month selection range")
263-
static let lastQuarter = NSLocalizedString("Last Quarter", comment: "Title of the Analytics Hub Last Quarter selection range")
264-
static let lastYear = NSLocalizedString("Last Year", comment: "Title of the Analytics Hub Last Year selection range")
265-
static let weekToDate = NSLocalizedString("Week to Date", comment: "Title of the Analytics Hub Week to Date selection range")
266-
static let monthToDate = NSLocalizedString("Month to Date", comment: "Title of the Analytics Hub Month to Date selection range")
267-
static let quarterToDate = NSLocalizedString("Quarter to Date", comment: "Title of the Analytics Hub Quarter to Date selection range")
268-
static let yearToDate = NSLocalizedString("Year to Date", comment: "Title of the Analytics Hub Year to Date selection range")
269182
static let selectionTitle = NSLocalizedString("Date Range", comment: "Title of the range selection list")
270183
static let noCurrentPeriodAvailable = NSLocalizedString("No current period available",
271184
comment: "A error message when it's not possible to acquire"

0 commit comments

Comments
 (0)