Skip to content

Commit

Permalink
PoC - Option B
Browse files Browse the repository at this point in the history
  • Loading branch information
novinfard committed Jan 24, 2025
1 parent c77c999 commit f8b4800
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 208 deletions.
2 changes: 2 additions & 0 deletions Backpack-SwiftUI/Calendar/Classes/BPKCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import SwiftUI
/// the `Calendar` struct in Swift.
/// - validRange: The range of dates that the calendar should allow the user to select.
/// This is specified as a`ClosedRange<Date>`.
/// - onSelectHandler: A callback function that handles calendar selection.
/// Without Providing this handler the selection would NOT perform
/// - initialMonthScroll: The initial scrolling to the month using `MonthScroll`
///
/// The `BPKCalendar` view also allows you to specify an accessory action. This is a closure that takes a string and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// A custom callback function that handles calendar selection.
/// It gives the flexibility to use different selection behaviour after a date is tapped by the user.
/// A callback function that handles calendar selection.
/// - Parameters:
/// - CalendarSelectionSimpleType: The type of calendar selection (range or single).
/// - Date: The date which is tapped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ struct CalendarTypeContainerFactory<MonthHeader: View, DayAccessoryView: View>:
dateFormatter: accessibilityDateFormatter
),
month: month,
selectionHandler: DefaultRangeCalendarSelectionHandler(
instructionAfterSelectingDate: accessibilityConfigurations.returnDatePrompt
),
monthHeader: { monthHeader(month) },
dayAccessoryView: dayAccessoryView,
onSelectHandler: onSelectHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,12 @@

import SwiftUI

protocol RangeCalendarSelectionHandler {
func newStateFor(
selection date: Date,
currentSelection: CalendarRangeSelectionState?
) -> CalendarRangeSelectionState
}

struct DefaultRangeCalendarSelectionHandler: RangeCalendarSelectionHandler {
let instructionAfterSelectingDate: String

func newStateFor(
selection date: Date,
currentSelection: CalendarRangeSelectionState?
) -> CalendarRangeSelectionState {
switch currentSelection {
case .intermediate(let initialDateSelection):
if date < initialDateSelection {
UIAccessibility.post(
notification: .announcement,
argument: instructionAfterSelectingDate
)
return .intermediate(date)
} else {
return .range(initialDateSelection...date)
}
default:
UIAccessibility.post(
notification: .announcement,
argument: instructionAfterSelectingDate
)
return .intermediate(date)
}
}
}

struct RangeCalendarMonthContainer<MonthHeader: View, DayAccessoryView: View>: View {
@Binding var selectionState: CalendarRangeSelectionState?
let calendar: Calendar
let validRange: ClosedRange<Date>
let accessibilityProvider: RangeDayAccessibilityProvider
let month: Date
let selectionHandler: RangeCalendarSelectionHandler
@ViewBuilder let monthHeader: MonthHeader
@ViewBuilder let dayAccessoryView: (Date) -> DayAccessoryView
var onSelectHandler: CalendarSelectionHandler?
Expand All @@ -70,11 +34,6 @@ struct RangeCalendarMonthContainer<MonthHeader: View, DayAccessoryView: View>: V
.range(selectionState),
date
)
} else {
selectionState = selectionHandler.newStateFor(
selection: date,
currentSelection: selectionState
)
}
}

Expand Down Expand Up @@ -234,9 +193,6 @@ struct RangeCalendarContainer_Previews: PreviewProvider {
dateFormatter: Self.formatter
),
month: start,
selectionHandler: DefaultRangeCalendarSelectionHandler(
instructionAfterSelectingDate: ""
),
monthHeader: {
BPKText("\(Self.formatter.string(from: start))")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ struct SingleCalendarMonthContainer<MonthHeader: View, DayAccessoryView: View>:
} onSelection: {
if let onSelectHandler {
onSelectHandler(.single(selection), dayDate)
} else {
selection = .single(dayDate)
}
}
.accessibilityAddTraits(.isButton)
Expand Down
4 changes: 0 additions & 4 deletions Example/Backpack.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
25006AB02C60B1740038D7CA /* SearchControlInputExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25006AAF2C60B1740038D7CA /* SearchControlInputExampleView.swift */; };
256F76152BB479850047AD1C /* SearchInputSummaryExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 256F76142BB479850047AD1C /* SearchInputSummaryExampleView.swift */; };
25D1337D2C73EFD7002C9562 /* SearchControlInputUITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25D1337C2C73EFD7002C9562 /* SearchControlInputUITest.swift */; };
281E51CC2D43C0C4002E7BF5 /* CalendarExampleOnSelectionHandlerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 281E51CB2D43C0C4002E7BF5 /* CalendarExampleOnSelectionHandlerView.swift */; };
2832E45B2CDE1561000B6DF4 /* CalendarExampleWholeMonthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2832E45A2CDE1561000B6DF4 /* CalendarExampleWholeMonthView.swift */; };
2A62FDDD2AB89F4500D545E5 /* TextAreaExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A62FDDC2AB89F4500D545E5 /* TextAreaExampleView.swift */; };
3A7D2D47214AB9F400ECBD5B /* BPKButtonsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A7D2D46214AB9F400ECBD5B /* BPKButtonsViewController.m */; };
Expand Down Expand Up @@ -260,7 +259,6 @@
25006AAF2C60B1740038D7CA /* SearchControlInputExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchControlInputExampleView.swift; sourceTree = "<group>"; };
256F76142BB479850047AD1C /* SearchInputSummaryExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchInputSummaryExampleView.swift; sourceTree = "<group>"; };
25D1337C2C73EFD7002C9562 /* SearchControlInputUITest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchControlInputUITest.swift; sourceTree = "<group>"; };
281E51CB2D43C0C4002E7BF5 /* CalendarExampleOnSelectionHandlerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarExampleOnSelectionHandlerView.swift; sourceTree = "<group>"; };
2832E45A2CDE1561000B6DF4 /* CalendarExampleWholeMonthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarExampleWholeMonthView.swift; sourceTree = "<group>"; };
2A62FDDC2AB89F4500D545E5 /* TextAreaExampleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextAreaExampleView.swift; sourceTree = "<group>"; };
3A7D2D45214AB9F400ECBD5B /* BPKButtonsViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BPKButtonsViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -609,7 +607,6 @@
isa = PBXGroup;
children = (
537AA67E2B050D1000D97B42 /* CalendarExampleRangeView.swift */,
281E51CB2D43C0C4002E7BF5 /* CalendarExampleOnSelectionHandlerView.swift */,
5318E3332AF506FA00C66D18 /* CalendarExampleSingleView.swift */,
2832E45A2CDE1561000B6DF4 /* CalendarExampleWholeMonthView.swift */,
530E84BE2D28461400009A4F /* CalendarExampleDynamicView.swift */,
Expand Down Expand Up @@ -1894,7 +1891,6 @@
5390DB5D29098CE400F0F790 /* RadiusTokensViewController.swift in Sources */,
79CE553529BEF26400CC89D3 /* PriceGroupsProvider.swift in Sources */,
E38947D422F5F0E900A357DB /* BottomSheetViewController.swift in Sources */,
281E51CC2D43C0C4002E7BF5 /* CalendarExampleOnSelectionHandlerView.swift in Sources */,
D2B8A01B2147FF6A002290DE /* PreviewCollectionViewHeader.swift in Sources */,
53C6621729EA0DAB00BF1A62 /* NudgerExampleView.swift in Sources */,
1750EB6329915230005226DF /* CardButtonsViewController.swift in Sources */,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,7 @@ struct CalendarExampleRangeView: View {
),
calendar: calendar,
validRange: validRange,
onSelectHandler: { state, date in
switch state {
// swiftlint:disable switch_case_alignment
case .range(let rangeState):
switch rangeState {
case .intermediate(let initialDateSelection):
if date < initialDateSelection {
selection = .intermediate(date)
} else {
selection = .range(initialDateSelection...date)
}
case .range(let range):
// M1C requirement can be handled here for the consumer
// via using a feature flag
if date > range.lowerBound && date < range.upperBound {
selection = .range(range.lowerBound...date)
} else {
selection = .intermediate(date)
}
default:
selection = .intermediate(date)
}
default:
break
}
},
onSelectHandler: handleSelection,
dayAccessoryView: { _ in
BPKIconView(.search, size: .small)
.foregroundColor(.accentColor)
Expand All @@ -135,10 +110,48 @@ struct CalendarExampleRangeView: View {
),
calendar: calendar,
validRange: validRange,
initialMonthScroll: monthScroll
initialMonthScroll: monthScroll,
onSelectHandler: handleSelection
)
}
}

// without providing the selection handler by consumer,
// range selection does not work
private func handleSelection(state: CalendarSelectionSimpleType, date: Date) {
switch state {
// swiftlint:disable switch_case_alignment
case .range(let rangeState):
switch rangeState {
case .intermediate(let initialDateSelection):
if date < initialDateSelection {
selection = .intermediate(date)
} else {
selection = .range(initialDateSelection...date)
}
case .range(let range):
// M1C requirement can be handled here for the consumer
// via using a feature flag
let m1cEnabled = false
if m1cEnabled {
if date >= range.lowerBound {
selection = .range(range.lowerBound...date)

} else {
selection = .intermediate(date)
}
} else {
selection = .intermediate(date)
}
default:
selection = .intermediate(date)
}
case .single(let singleState):
// Not applicable for the consumer
// who only uses the range selector
break
}
}
}

struct CalendarExampleRangeView_Previews: PreviewProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import SwiftUI
import Backpack_SwiftUI

// swiftlint:disable closure_body_length
struct CalendarExampleSingleView: View {
@State var selection: CalendarSingleSelectionState?
private var monthScroll: MonthScroll?
Expand Down Expand Up @@ -71,7 +72,12 @@ struct CalendarExampleSingleView: View {
),
calendar: calendar,
validRange: validRange,
initialMonthScroll: monthScroll
initialMonthScroll: monthScroll,
onSelectHandler: { _, date in // unused state
// without providing the selection handler by consumer,
// single selection does not work
selection = .single(date)
}
)
.monthAccessoryAction { _ in
return CalendarMonthAccessoryAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ struct CalendarGroupsProvider {
presentableCalendar("Range Selection", view: CalendarExampleRangeView(showAccessoryViews: false)),
presentableCalendar("Single Selection", view: CalendarExampleSingleView()),
presentableCalendar("With Accessory Views", view: CalendarExampleRangeView(showAccessoryViews: true)),
presentableCalendar("With Custom Selection Handler", view: CalendarExampleOnSelectionHandlerView()),
presentableCalendar("With Whole Month Selection", view: CalendarExampleWholeMonthView()),
presentableCalendar(
"With Initial Month Scrolling",
Expand Down

0 comments on commit f8b4800

Please sign in to comment.