@@ -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
6979extension 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
123144private 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