Skip to content

Commit 0f09cb5

Browse files
committed
Adds custom range case support to AnalyticsHubTimeRangeSelection
1 parent faebb98 commit 0f09cb5

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,18 @@ private extension AnalyticsHubViewModel {
188188
.removeDuplicates()
189189
.sink { [weak self] newSelectionType in
190190
guard let self else { return }
191-
self.timeRangeSelection = AnalyticsHubTimeRangeSelection(selectionType: newSelectionType)
191+
192+
// Temporary while the Custom Range Selection integration
193+
let curatedSelection: AnalyticsHubTimeRangeSelection.SelectionType = {
194+
switch newSelectionType {
195+
case .custom(start: nil, end: nil):
196+
return .custom(start: Date().startOfWeek(timezone: .current), end: Date().endOfDay(timezone: .current))
197+
default:
198+
return newSelectionType
199+
}
200+
}()
201+
202+
self.timeRangeSelection = AnalyticsHubTimeRangeSelection(selectionType: curatedSelection)
192203
self.timeRangeCard = AnalyticsHubViewModel.timeRangeCard(timeRangeSelection: self.timeRangeSelection)
193204
Task.init {
194205
await self.updateData()

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ public class AnalyticsHubTimeRangeSelection {
3333
currentDate: Date = Date(),
3434
timezone: TimeZone = TimeZone.current,
3535
calendar: Calendar = Locale.current.calendar) {
36-
let selectionData = selectionType.toRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar)
36+
37+
// Exit early if we can't generate a selection Data.
38+
guard let selectionData = selectionType.toRangeData(referenceDate: currentDate, timezone: timezone, calendar: calendar) else {
39+
self.currentTimeRange = nil
40+
self.previousTimeRange = nil
41+
self.formattedCurrentRangeText = nil
42+
self.formattedPreviousRangeText = nil
43+
self.rangeSelectionDescription = ""
44+
return
45+
}
46+
3747
let currentTimeRange = selectionData.currentTimeRange
3848
let previousTimeRange = selectionData.previousTimeRange
3949
let useShortFormat = selectionType == .today || selectionType == .yesterday
@@ -67,7 +77,16 @@ public class AnalyticsHubTimeRangeSelection {
6777

6878
// MARK: - Time Range Selection Type
6979
extension AnalyticsHubTimeRangeSelection {
70-
enum SelectionType: CaseIterable {
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+
[.custom(start: nil, end: nil), .today, .yesterday, .lastWeek, .lastMonth, .lastQuarter, .lastYear, .weekToDate, .monthToDate, .quarterToDate,
85+
.yearToDate]
86+
}
87+
88+
// When adding a new case, remember to add it to `allCases`.
89+
case custom(start: Date?, end: Date?)
7190
case today
7291
case yesterday
7392
case lastWeek
@@ -81,6 +100,8 @@ extension AnalyticsHubTimeRangeSelection {
81100

82101
var description: String {
83102
switch self {
103+
case .custom:
104+
return Localization.custom
84105
case .today:
85106
return Localization.today
86107
case .yesterday:
@@ -121,8 +142,14 @@ extension AnalyticsHubTimeRangeSelection {
121142

122143
// MARK: - SelectionType helper functions
123144
private extension AnalyticsHubTimeRangeSelection.SelectionType {
124-
func toRangeData(referenceDate: Date, timezone: TimeZone, calendar: Calendar) -> AnalyticsHubTimeRangeData {
145+
func toRangeData(referenceDate: Date, timezone: TimeZone, calendar: Calendar) -> AnalyticsHubTimeRangeData? {
125146
switch self {
147+
case let .custom(start?, end?):
148+
return AnalyticsHubCustomRangeData(start: start, end: end, timezone: timezone, calendar: calendar)
149+
case .custom:
150+
// Nil custom dates are not supported but can exists when the user has selected the custom range option but hasn't choosen dates yet.
151+
// To properly fix this, we should decouple UI selection types, from ranges selection types.
152+
return nil
126153
case .today:
127154
return AnalyticsHubTodayRangeData(referenceDate: referenceDate, timezone: timezone, calendar: calendar)
128155
case .yesterday:
@@ -155,6 +182,7 @@ extension AnalyticsHubTimeRangeSelection {
155182
}
156183

157184
enum Localization {
185+
static let custom = NSLocalizedString("Custom", comment: "Title of the Analytics Hub Custom selection range")
158186
static let today = NSLocalizedString("Today", comment: "Title of the Analytics Hub Today's selection range")
159187
static let yesterday = NSLocalizedString("Yesterday", comment: "Title of the Analytics Hub Yesterday selection range")
160188
static let lastWeek = NSLocalizedString("Last Week", comment: "Title of the Analytics Hub Last Week selection range")

0 commit comments

Comments
 (0)