11import Foundation
2+ import Yosemite
23
34/// Main source of time ranges of the Analytics Hub, responsible for providing the current and previous dates
45/// for a given Date and range Type alongside their UI descriptions
@@ -9,7 +10,6 @@ public class AnalyticsHubTimeRangeSelection {
910 private let formattedCurrentRangeText : String ?
1011 private let formattedPreviousRangeText : String ?
1112 let rangeSelectionDescription : String
12-
1313 /// Provide a date description of the current time range set internally.
1414 /// - Returns the Time range in a UI friendly format. If the current time range is not available,
1515 /// then returns an presentable error message.
@@ -19,7 +19,6 @@ public class AnalyticsHubTimeRangeSelection {
1919 }
2020 return currentTimeRangeDescription
2121 }
22-
2322 /// Generates a date description of the previous time range set internally.
2423 /// - Returns the Time range in a UI friendly format. If the previous time range is not available,
2524 /// then returns an presentable error message.
@@ -31,24 +30,36 @@ public class AnalyticsHubTimeRangeSelection {
3130 }
3231
3332 //TODO: abandon usage of the ISO 8601 Calendar and build one based on the Site calendar configuration
34- init ( selectionType: AnalyticsHubTimeRangeSelectionType ,
33+ init ( selectionType: SelectionType ,
3534 currentDate: Date = Date ( ) ,
3635 timezone: TimeZone = TimeZone . autoupdatingCurrent,
3736 calendar: Calendar = Calendar ( identifier: . iso8601) ) {
38- let selectionData = selectionType. mapToRangeData ( referenceDate: currentDate, timezone: timezone, calendar: calendar)
37+ var selectionData : AnalyticsHubTimeRangeData
38+
39+ switch selectionType {
40+ case . today:
41+ selectionData = AnalyticsHubDayRangeData ( referenceDate: currentDate, timezone: timezone, calendar: calendar)
42+ case . weekToDate:
43+ selectionData = AnalyticsHubWeekRangeData ( referenceDate: currentDate, timezone: timezone, calendar: calendar)
44+ case . monthToDate:
45+ selectionData = AnalyticsHubMonthRangeData ( referenceDate: currentDate, timezone: timezone, calendar: calendar)
46+ case . yearToDate:
47+ selectionData = AnalyticsHubYearRangeData ( referenceDate: currentDate, timezone: timezone, calendar: calendar)
48+ }
49+
3950 let currentTimeRange = selectionData. currentTimeRange
4051 let previousTimeRange = selectionData. previousTimeRange
41- let simplifiedDescription = selectionType == . today || selectionType == . yesterday
42-
4352 self . currentTimeRange = currentTimeRange
4453 self . previousTimeRange = previousTimeRange
45- self . rangeSelectionDescription = selectionType. description
54+
55+ let simplifiedDescription = selectionType == . today
4656 self . formattedCurrentRangeText = currentTimeRange? . formatToString ( simplified: simplifiedDescription,
4757 timezone: timezone,
4858 calendar: calendar)
4959 self . formattedPreviousRangeText = previousTimeRange? . formatToString ( simplified: simplifiedDescription,
5060 timezone: timezone,
5161 calendar: calendar)
62+ self . rangeSelectionDescription = selectionType. description
5263 }
5364
5465 /// Unwrap the generated selected `AnalyticsHubTimeRange` based on the `selectedTimeRange`
@@ -60,7 +71,6 @@ public class AnalyticsHubTimeRangeSelection {
6071 }
6172 return currentTimeRange
6273 }
63-
6474 /// Unwrap the generated previous `AnalyticsHubTimeRange`relative to the selected one
6575 /// based on the `selectedTimeRange` provided during initialization.
6676 /// - throws a `.previousRangeGenerationFailed` error if the unwrap fails.
@@ -72,33 +82,53 @@ public class AnalyticsHubTimeRangeSelection {
7282 }
7383}
7484
75- // MARK: - Data creation utility
76- private extension AnalyticsHubTimeRangeSelectionType {
77- func mapToRangeData( referenceDate: Date , timezone: TimeZone , calendar: Calendar ) -> AnalyticsHubTimeRangeData {
78- switch self {
79- case . today:
80- return AnalyticsHubTodayRangeData ( referenceDate: referenceDate, timezone: timezone, calendar: calendar)
81- case . yesterday:
82- return AnalyticsHubYesterdayRangeData ( referenceDate: referenceDate, timezone: timezone, calendar: calendar)
83- case . weekToDate:
84- return AnalyticsHubWeekToDateRangeData ( referenceDate: referenceDate, timezone: timezone, calendar: calendar)
85- case . monthToDate:
86- return AnalyticsHubMonthToDateRangeData ( referenceDate: referenceDate, timezone: timezone, calendar: calendar)
87- case . yearToDate:
88- return AnalyticsHubYearToDateRangeData ( referenceDate: referenceDate, timezone: timezone, calendar: calendar)
85+ // MARK: - Time Range Selection Type
86+ extension AnalyticsHubTimeRangeSelection {
87+ enum SelectionType : CaseIterable {
88+ case today
89+ case weekToDate
90+ case monthToDate
91+ case yearToDate
92+
93+ var description : String {
94+ switch self {
95+ case . today:
96+ return Localization . today
97+ case . weekToDate:
98+ return Localization . weekToDate
99+ case . monthToDate:
100+ return Localization . monthToDate
101+ case . yearToDate:
102+ return Localization . yearToDate
103+ }
104+ }
105+
106+ init ( _ statsTimeRange: StatsTimeRangeV4 ) {
107+ switch statsTimeRange {
108+ case . today:
109+ self = . today
110+ case . thisWeek:
111+ self = . weekToDate
112+ case . thisMonth:
113+ self = . monthToDate
114+ case . thisYear:
115+ self = . yearToDate
116+ }
89117 }
90118 }
91119}
92-
93120// MARK: - Constants
94121extension AnalyticsHubTimeRangeSelection {
95-
96122 enum TimeRangeGeneratorError : Error {
97123 case selectedRangeGenerationFailed
98124 case previousRangeGenerationFailed
99125 }
100126
101127 enum Localization {
128+ static let today = NSLocalizedString ( " Today " , comment: " Title of the Analytics Hub Today's selection range " )
129+ static let weekToDate = NSLocalizedString ( " Week to Date " , comment: " Title of the Analytics Hub Week to Date selection range " )
130+ static let monthToDate = NSLocalizedString ( " Month to Date " , comment: " Title of the Analytics Hub Month to Date selection range " )
131+ static let yearToDate = NSLocalizedString ( " Year to Date " , comment: " Title of the Analytics Hub Year to Date selection range " )
102132 static let selectionTitle = NSLocalizedString ( " Date Range " , comment: " Title of the range selection list " )
103133 static let noCurrentPeriodAvailable = NSLocalizedString ( " No current period available " ,
104134 comment: " A error message when it's not possible to acquire "
0 commit comments