Skip to content

Commit

Permalink
Make BPKCalendar accessibilty non mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
Yura Reutskiy committed Jan 3, 2025
1 parent 187d7e8 commit 4aac63d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 46 deletions.
3 changes: 2 additions & 1 deletion Backpack-SwiftUI/Calendar/Classes/BPKCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import SwiftUI
/// - validRange: The range of dates that the calendar should allow the user to select.
/// This is specified as a`ClosedRange<Date>`.
/// - initialMonthScroll: The initial scrolling to the month using `MonthScroll`
/// - calendarAccessibilityConfiguration: The optional configuration to add accessibility strings to the calendar
///
/// The `BPKCalendar` view also allows you to specify an accessory action. This is a closure that takes a string and
/// a date, and is called when the user interacts with an accessory in the calendar.
Expand All @@ -50,7 +51,7 @@ public struct BPKCalendar<DayAccessoryView: View>: View {
calendar: Calendar,
validRange: ClosedRange<Date>,
initialMonthScroll: MonthScroll? = nil,
calendarAccessibilityConfiguration: CalendarAccessibilityConfiguration,
calendarAccessibilityConfiguration: CalendarAccessibilityConfiguration = .init(),
dayAccessoryView: @escaping (Date) -> DayAccessoryView = { _ in EmptyView() }
) {
self.dayAccessoryView = dayAccessoryView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,23 @@ struct CalendarSelectableCell: View {

private var defaultCell: some View {
DefaultCalendarDayCell(calendar: calendar, date: dayDate)
.accessibilityLabel(Text(
accessibilityProvider.rangeSelection.accessibilityLabel(for: dayDate)
))
.if(accessibilityProvider.rangeSelection != nil) { cell in
cell.accessibilityLabel(Text(
accessibilityProvider.rangeSelection?.accessibilityLabel(for: dayDate) ?? ""
))
}
}

private func singleCell(date: Date) -> some View {
SingleSelectedCell(calendar: calendar, date: dayDate)
.accessibilityLabel(
Text(
accessibilityProvider.rangeSelection.accessibilityLabel(
.if(accessibilityProvider.rangeSelection != nil) { cell in
cell.accessibilityLabel(Text(
accessibilityProvider.rangeSelection?.accessibilityLabel(
for: dayDate,
intermediateSelectionDate: date
)
)
)
) ?? ""
))
}
}

private func rangeCell(closedRange: ClosedRange<Date>, highlightRangeEnds: Bool) -> some View {
Expand All @@ -96,13 +98,15 @@ struct CalendarSelectableCell: View {
calendar: calendar,
highlightRangeEnds: highlightRangeEnds
)
.accessibilityLabel(Text(
accessibilityProvider.rangeSelection.accessibilityLabel(
for: dayDate,
selection: closedRange
)
))
.accessibility(addTraits: .isSelected)
.if(accessibilityProvider.rangeSelection != nil) { cell in
cell.accessibilityLabel(Text(
accessibilityProvider.rangeSelection?.accessibilityLabel(
for: dayDate,
selection: closedRange
) ?? ""
))
}
}

private func wholeMonthRangeCell(range: ClosedRange<Date>) -> some View {
Expand All @@ -112,13 +116,15 @@ struct CalendarSelectableCell: View {
calendar: calendar,
highlightRangeEnds: false
)
.accessibilityLabel(Text(
accessibilityProvider.rangeSelection.accessibilityLabel(
for: dayDate,
selection: range
)
))
.accessibility(addTraits: .isSelected)
.if(accessibilityProvider.rangeSelection != nil) { cell in
cell.accessibilityLabel(Text(
accessibilityProvider.rangeSelection?.accessibilityLabel(
for: dayDate,
selection: range
) ?? ""
))
}
}

private func initialSelection(_ initialDateSelection: Date, matchesDate date: Date) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import SwiftUI

public struct CalendarAccessibilityConfiguration {
public let singleSelection: SingleDayAccessibilityProvider
public let rangeSelection: RangeDayAccessibilityProvider
public init(singleSelection: SingleDayAccessibilityProvider, rangeSelection: RangeDayAccessibilityProvider) {
public let singleSelection: SingleDayAccessibilityProvider?
public let rangeSelection: RangeDayAccessibilityProvider?
public init(
singleSelection: SingleDayAccessibilityProvider? = nil,
rangeSelection: RangeDayAccessibilityProvider? = nil
) {
self.singleSelection = singleSelection
self.rangeSelection = rangeSelection
}
Expand Down Expand Up @@ -124,7 +127,7 @@ struct CalendarTypeContainerFactory<MonthHeader: View, DayAccessoryView: View>:
selection.wrappedValue = .intermediate(dayDate)
UIAccessibility.post(
notification: .announcement,
argument: calendarAccessibilityConfiguration.rangeSelection
argument: calendarAccessibilityConfiguration.rangeSelection?
.accessibilityInstructionAfterSelectingDate()
)
} else {
Expand All @@ -134,7 +137,7 @@ struct CalendarTypeContainerFactory<MonthHeader: View, DayAccessoryView: View>:
selection.wrappedValue = .intermediate(dayDate)
UIAccessibility.post(
notification: .announcement,
argument: calendarAccessibilityConfiguration.rangeSelection
argument: calendarAccessibilityConfiguration.rangeSelection?
.accessibilityInstructionAfterSelectingDate()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,7 @@ struct CalendarExampleWholeMonthView: View {
accessibilityConfigurations: accessibilityConfigurations
),
calendar: calendar,
validRange: validRange,
calendarAccessibilityConfiguration: CalendarAccessibilityConfiguration(
singleSelection: .init(
accessibilityConfigurations: .init(selectionHint: "hint"),
dateFormatter: DateFormatter()
),
rangeSelection: .init(
accessibilityConfigurations: .init(
startSelectionHint: "startSelectionHint",
endSelectionHint: "endSelectionHint",
startSelectionState: "startSelectionState",
endSelectionState: "endSelectionState",
betweenSelectionState: "betweenSelectionState",
startAndEndSelectionState: "startAndEndSelectionState",
returnDatePrompt: "returnDatePrompt"
),
dateFormatter: DateFormatter()
)
)
validRange: validRange
)
.monthAccessoryAction { _ in
return CalendarMonthAccessoryAction(
Expand Down

0 comments on commit 4aac63d

Please sign in to comment.