Skip to content

Commit 8787da3

Browse files
authored
Merge pull request #5810 from woocommerce/feat/5743-time-range-formatting
Update time range date text
2 parents 673f32b + 35bd252 commit 8787da3

File tree

4 files changed

+513
-33
lines changed

4 files changed

+513
-33
lines changed

WooCommerce/Classes/Extensions/DateFormatter+Helpers.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ extension DateFormatter {
1010

1111
// MARK: - Chark axis formatters
1212

13+
/// Date formatter used for creating the date for a selected date displayed on the time range bar for **hour** granularity.
14+
///
15+
public static let chartSelectedDateHourFormatter: DateFormatter = {
16+
let formatter = DateFormatter()
17+
formatter.setLocalizedDateFormatFromTemplate("EEEE, MMM d, h:mm a")
18+
return formatter
19+
}()
20+
21+
/// Date formatter used for creating the date for a selected date displayed on the time range bar for **hour** granularity.
22+
///
23+
public static let legacyChartSelectedDateHourFormatter: DateFormatter = {
24+
let formatter = DateFormatter()
25+
formatter.setLocalizedDateFormatFromTemplate("ha")
26+
return formatter
27+
}()
28+
1329
/// Date formatter used for creating the date displayed on a chart axis for **hour** granularity.
1430
///
1531
public static let chartAxisHourFormatter: DateFormatter = {
@@ -53,6 +69,14 @@ extension DateFormatter {
5369
/// Date formatter used for displaying the full month on a chart axis.
5470
///
5571
public static let chartAxisFullMonthFormatter: DateFormatter = {
72+
let formatter = DateFormatter()
73+
formatter.setLocalizedDateFormatFromTemplate("MMMM yyyy")
74+
return formatter
75+
}()
76+
77+
/// Date formatter used for displaying the full month on a chart axis.
78+
///
79+
public static let legacyChartAxisFullMonthFormatter: DateFormatter = {
5680
let formatter = DateFormatter()
5781
formatter.setLocalizedDateFormatFromTemplate("MMMM")
5882
return formatter
Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
1+
import Experiments
12
import Yosemite
23

34
private extension StatsTimeRangeV4 {
4-
func timeRangeText(startDate: Date, endDate: Date, selectedDate: Date, timezone: TimeZone) -> String {
5-
let selectedDateString = timeRangeSelectedDateFormatter(timezone: timezone).string(from: selectedDate)
6-
switch self {
7-
case .today, .thisYear:
8-
let dateBreadcrumbFormat = NSLocalizedString("%1$@ › %2$@", comment: "Displays a time range followed by a specific date/time")
9-
let timeRangeString = timeRangeText(startDate: startDate, endDate: endDate, timezone: timezone)
10-
return String.localizedStringWithFormat(dateBreadcrumbFormat, timeRangeString, selectedDateString)
11-
case .thisWeek, .thisMonth:
5+
func timeRangeText(startDate: Date, endDate: Date, selectedDate: Date, timezone: TimeZone, isMyStoreTabUpdatesEnabled: Bool) -> String {
6+
let selectedDateString = timeRangeSelectedDateFormatter(timezone: timezone,
7+
isMyStoreTabUpdatesEnabled: isMyStoreTabUpdatesEnabled)
8+
.string(from: selectedDate)
9+
10+
if isMyStoreTabUpdatesEnabled {
1211
return selectedDateString
12+
} else {
13+
switch self {
14+
case .today, .thisYear:
15+
let dateBreadcrumbFormat = NSLocalizedString("%1$@ › %2$@", comment: "Displays a time range followed by a specific date/time")
16+
let timeRangeString = timeRangeText(startDate: startDate,
17+
endDate: endDate,
18+
timezone: timezone,
19+
isMyStoreTabUpdatesEnabled: isMyStoreTabUpdatesEnabled)
20+
return String.localizedStringWithFormat(dateBreadcrumbFormat, timeRangeString, selectedDateString)
21+
case .thisWeek, .thisMonth:
22+
return selectedDateString
23+
}
1324
}
1425
}
1526

16-
func timeRangeText(startDate: Date, endDate: Date, timezone: TimeZone) -> String {
17-
let dateFormatter = timeRangeDateFormatter(timezone: timezone)
27+
func timeRangeText(startDate: Date, endDate: Date, timezone: TimeZone, isMyStoreTabUpdatesEnabled: Bool) -> String {
28+
let dateFormatter = timeRangeDateFormatter(timezone: timezone, isMyStoreTabUpdatesEnabled: isMyStoreTabUpdatesEnabled)
1829
switch self {
1930
case .today, .thisMonth, .thisYear:
2031
return dateFormatter.string(from: startDate)
2132
case .thisWeek:
2233
let startDateString = dateFormatter.string(from: startDate)
2334
let endDateString = dateFormatter.string(from: endDate)
24-
let format = NSLocalizedString("%1$@-%2$@", comment: "Displays a date range for a stats interval")
35+
let format = isMyStoreTabUpdatesEnabled ?
36+
NSLocalizedString("%1$@ - %2$@", comment: "Displays a date range for a stats interval"):
37+
NSLocalizedString("%1$@-%2$@", comment: "Displays a date range for a stats interval in the legacy version")
2538
return String.localizedStringWithFormat(format, startDateString, endDateString)
2639
}
2740
}
2841

29-
func timeRangeDateFormatter(timezone: TimeZone) -> DateFormatter {
42+
func timeRangeDateFormatter(timezone: TimeZone, isMyStoreTabUpdatesEnabled: Bool) -> DateFormatter {
3043
let dateFormatter: DateFormatter
3144
switch self {
3245
case .today:
@@ -36,7 +49,9 @@ private extension StatsTimeRangeV4 {
3649
case .thisWeek:
3750
dateFormatter = DateFormatter.Charts.chartAxisDayFormatter
3851
case .thisMonth:
39-
dateFormatter = DateFormatter.Charts.chartAxisFullMonthFormatter
52+
dateFormatter = isMyStoreTabUpdatesEnabled ?
53+
DateFormatter.Charts.chartAxisFullMonthFormatter:
54+
DateFormatter.Charts.legacyChartAxisFullMonthFormatter
4055
case .thisYear:
4156
dateFormatter = DateFormatter.Charts.chartAxisYearFormatter
4257
}
@@ -45,15 +60,18 @@ private extension StatsTimeRangeV4 {
4560
}
4661

4762
/// Date formatter for a selected date for a time range.
48-
func timeRangeSelectedDateFormatter(timezone: TimeZone) -> DateFormatter {
63+
func timeRangeSelectedDateFormatter(timezone: TimeZone, isMyStoreTabUpdatesEnabled: Bool) -> DateFormatter {
4964
let dateFormatter: DateFormatter
5065
switch self {
5166
case .today:
52-
dateFormatter = DateFormatter.Charts.chartAxisHourFormatter
67+
dateFormatter = isMyStoreTabUpdatesEnabled ?
68+
DateFormatter.Charts.chartSelectedDateHourFormatter:
69+
DateFormatter.Charts.legacyChartSelectedDateHourFormatter
5370
case .thisWeek, .thisMonth:
5471
dateFormatter = DateFormatter.Charts.chartAxisDayFormatter
5572
case .thisYear:
56-
dateFormatter = DateFormatter.Charts.chartAxisFullMonthFormatter
73+
dateFormatter = isMyStoreTabUpdatesEnabled ? DateFormatter.Charts.chartAxisFullMonthFormatter:
74+
DateFormatter.Charts.legacyChartAxisFullMonthFormatter
5775
}
5876
dateFormatter.timeZone = timezone
5977
return dateFormatter
@@ -67,20 +85,24 @@ struct StatsTimeRangeBarViewModel: Equatable {
6785
init(startDate: Date,
6886
endDate: Date,
6987
timeRange: StatsTimeRangeV4,
70-
timezone: TimeZone) {
88+
timezone: TimeZone,
89+
isMyStoreTabUpdatesEnabled: Bool = ServiceLocator.featureFlagService.isFeatureFlagEnabled(.myStoreTabUpdates)) {
7190
timeRangeText = timeRange.timeRangeText(startDate: startDate,
7291
endDate: endDate,
73-
timezone: timezone)
92+
timezone: timezone,
93+
isMyStoreTabUpdatesEnabled: isMyStoreTabUpdatesEnabled)
7494
}
7595

7696
init(startDate: Date,
7797
endDate: Date,
7898
selectedDate: Date,
7999
timeRange: StatsTimeRangeV4,
80-
timezone: TimeZone) {
100+
timezone: TimeZone,
101+
isMyStoreTabUpdatesEnabled: Bool = ServiceLocator.featureFlagService.isFeatureFlagEnabled(.myStoreTabUpdates)) {
81102
timeRangeText = timeRange.timeRangeText(startDate: startDate,
82103
endDate: endDate,
83104
selectedDate: selectedDate,
84-
timezone: timezone)
105+
timezone: timezone,
106+
isMyStoreTabUpdatesEnabled: isMyStoreTabUpdatesEnabled)
85107
}
86108
}

0 commit comments

Comments
 (0)