@@ -9,8 +9,14 @@ struct AnalyticsTimeRangeCard: View {
99 let previousRangeDescription : String
1010 @Binding var selectionType : AnalyticsHubTimeRangeSelection . SelectionType
1111
12+ /// Determines if the time range selection should be shown.
13+ ///
1214 @State private var showTimeRangeSelectionView : Bool = false
1315
16+ /// Determines if the custom range selection should be shown.
17+ ///
18+ @State private var showCustomRangeSelectionView : Bool = false
19+
1420 init ( viewModel: AnalyticsTimeRangeCardViewModel , selectionType: Binding < AnalyticsHubTimeRangeSelection . SelectionType > ) {
1521 self . timeRangeTitle = viewModel. selectedRangeTitle
1622 self . currentRangeDescription = viewModel. currentRangeSubtitle
@@ -24,7 +30,13 @@ struct AnalyticsTimeRangeCard: View {
2430 SelectionList ( title: Localization . timeRangeSelectionTitle,
2531 items: AnalyticsHubTimeRangeSelection . SelectionType. allCases,
2632 contentKeyPath: \. description,
27- selected: $selectionType)
33+ selected: internalSelectionBinding ( ) )
34+ . sheet ( isPresented: $showCustomRangeSelectionView) {
35+ RangedDatePicker ( ) { start, end in
36+ showTimeRangeSelectionView = false // Dismiss the initial sheet for a smooth transition
37+ self . selectionType = . custom( start: start, end: end)
38+ }
39+ }
2840 }
2941 }
3042
@@ -71,6 +83,27 @@ struct AnalyticsTimeRangeCard: View {
7183 . padding ( [ . top, . bottom] )
7284 . frame ( maxWidth: . infinity)
7385 }
86+
87+ /// Tracks the range selection internally to determine if the custom range selection should be presented or not.
88+ /// If custom range selection is not needed, the internal selection is forwarded to `selectionType`.
89+ ///
90+ private func internalSelectionBinding( ) -> Binding < AnalyticsHubTimeRangeSelection . SelectionType > {
91+ . init(
92+ get: {
93+ selectionType
94+ } ,
95+ set: { newValue in
96+ switch newValue {
97+ // If we get a `custom` case with nil dates it is because we need to present the custom range selection
98+ case . custom( start: nil , end: nil ) :
99+ showCustomRangeSelectionView = true
100+ default :
101+ // Any other selection should be forwarded to our parent binding.
102+ selectionType = newValue
103+ }
104+ }
105+ )
106+ }
74107}
75108
76109// MARK: Constants
0 commit comments