Skip to content

Commit cda4510

Browse files
committed
Extract date formatter into a protocol for easier reuse
1 parent 65f4379 commit cda4510

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct AnalyticsTimeRangeCard: View {
3838
ServiceLocator.analytics.track(event: .AnalyticsHub.dateRangeOptionSelected(selection.tracksIdentifier))
3939
}
4040
.sheet(isPresented: $showCustomRangeSelectionView) {
41-
RangedDatePicker(startDate: selectionType.startDate, endDate: selectionType.endDate) { start, end in
41+
RangedDatePicker(startDate: selectionType.startDate, endDate: selectionType.endDate, datesFormatter: DatesFormatter()) { start, end in
4242
showTimeRangeSelectionView = false // Dismiss the initial sheet for a smooth transition
4343
self.selectionType = .custom(start: start, end: end)
4444
}
@@ -115,6 +115,16 @@ struct AnalyticsTimeRangeCard: View {
115115
}
116116
}
117117

118+
private extension AnalyticsTimeRangeCard {
119+
/// Specific `DatesFormatter` for the `RangedDatePicker` when presented in the analytics hub module.
120+
///
121+
struct DatesFormatter: RangedDateTextFormatter {
122+
func format(start: Date, end: Date) -> String {
123+
AnalyticsHubTimeRange(start: start, end: end).formatToString(simplified: false, timezone: .current, calendar: Locale.current.calendar)
124+
}
125+
}
126+
}
127+
118128
// MARK: Constants
119129
private extension AnalyticsTimeRangeCard {
120130
enum Layout {

WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/RangedDatePicker.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import Foundation
22
import SwiftUI
33

4+
/// Defines a decoupled way to format selected dates
5+
///
6+
protocol RangedDateTextFormatter {
7+
func format(start: Date, end: Date) -> String
8+
}
9+
410
/// View to select a custom date range.
511
/// Consists of two date pickers laid out vertically.
612
///
@@ -20,11 +26,19 @@ struct RangedDatePicker: View {
2026
///
2127
@State private var endDate: Date
2228

29+
/// Type to format the subtitle range.
30+
///
31+
private let datesFormatter: RangedDateTextFormatter
32+
2333
/// Custom `init` to provide intial start and end dates.
2434
///
25-
init(startDate: Date? = nil, endDate: Date? = nil, datesSelected: ((_ start: Date, _ end: Date) -> Void)? = nil) {
35+
init(startDate: Date? = nil,
36+
endDate: Date? = nil,
37+
datesFormatter: RangedDateTextFormatter,
38+
datesSelected: ((_ start: Date, _ end: Date) -> Void)? = nil) {
2639
self._startDate = State(initialValue: startDate ?? Date())
2740
self._endDate = State(initialValue: endDate ?? Date())
41+
self.datesFormatter = datesFormatter
2842
self.datesSelected = datesSelected
2943
}
3044

@@ -65,8 +79,7 @@ struct RangedDatePicker: View {
6579
Text(Localization.title)
6680
.headlineStyle()
6781

68-
// TODO: Properly format date ranges outside the view
69-
Text("\(DateFormatter.monthAndDayFormatter.string(from: startDate)) - \(DateFormatter.monthAndDayFormatter.string(from: endDate))")
82+
Text(datesFormatter.format(start: startDate, end: endDate))
7083
.captionStyle()
7184
}
7285
}
@@ -109,7 +122,14 @@ private extension RangedDatePicker {
109122
// MARK: Previews
110123

111124
struct RangedDatePickerPreview: PreviewProvider {
125+
126+
private struct PreviewFormatter: RangedDateTextFormatter {
127+
func format(start: Date, end: Date) -> String {
128+
"\(start.description) - \(end.description)"
129+
}
130+
}
131+
112132
static var previews: some View {
113-
RangedDatePicker()
133+
RangedDatePicker(datesFormatter: PreviewFormatter())
114134
}
115135
}

0 commit comments

Comments
 (0)