Skip to content

Commit

Permalink
DON-1068: Add floating year label configuration to BPKCalendar
Browse files Browse the repository at this point in the history
  • Loading branch information
Yura Reutskiy committed Jan 24, 2025
1 parent 6d77026 commit f5197c4
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Backpack-SwiftUI/Calendar/Classes/BPKCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct BPKCalendar<DayAccessoryView: View>: View {
private var accessoryAction: ((Date) -> CalendarMonthAccessoryAction?)?
private var initialMonthScroll: MonthScroll?
private let monthHeaderDateFormatter: DateFormatter

private let showFloatYearLabel: Bool
private let dayAccessoryView: (Date) -> DayAccessoryView
@State private var currentlyShownMonth: Date

Expand All @@ -49,6 +49,7 @@ public struct BPKCalendar<DayAccessoryView: View>: View {
calendar: Calendar,
validRange: ClosedRange<Date>,
initialMonthScroll: MonthScroll? = nil,
showFloatYearLabel: Bool = true,
dayAccessoryView: @escaping (Date) -> DayAccessoryView = { _ in EmptyView() }
) {
self.dayAccessoryView = dayAccessoryView
Expand All @@ -57,6 +58,7 @@ public struct BPKCalendar<DayAccessoryView: View>: View {
self.calendar = calendar
self.selectionType = selectionType
self.initialMonthScroll = initialMonthScroll
self.showFloatYearLabel = showFloatYearLabel

monthHeaderDateFormatter = DateFormatter()
monthHeaderDateFormatter.timeZone = calendar.timeZone
Expand Down Expand Up @@ -90,7 +92,9 @@ public struct BPKCalendar<DayAccessoryView: View>: View {
},
dayAccessoryView: dayAccessoryView
)
yearBadge
if showFloatYearLabel {
yearBadge
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions Backpack-SwiftUI/Calendar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ BPKCalendar(
}
)
```

## Float year label

There is a configurable option to hide/show a floating year label from the calendar view. Default option is to show the year.

```swift
BPKCalendar(
selectionType: .range(selectedRange: $selectedDateRange),
calendar: .current,
validRange: validStartDate...validEndDate,
showFloatYearLabel: false
)
```
19 changes: 19 additions & 0 deletions Backpack-SwiftUI/Tests/Calendar/BPKCalendarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,23 @@ class BPKCalendarTests: XCTestCase {
.frame(width: 400)
)
}

func test_singleSelectionCalendarWithNoFloatYearLabel() {
let testDate = Calendar.current.date(from: DateComponents(year: 2020, month: 2, day: 5))!

assertSnapshot(
BPKCalendar(
selectionType: .single(
selected: .constant(.single(testDate)),
accessibilityConfigurations: SingleAccessibilityConfigurations(
selectionHint: ""
)
),
calendar: Calendar.current,
validRange: validStart...validEnd,
showFloatYearLabel: false
)
.frame(width: 320, height: 720)
)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ struct CalendarExampleRangeView: View {
let calendar: Calendar
let formatter: DateFormatter
let showAccessoryViews: Bool
let showFloatYearLabel: Bool

init(showAccessoryViews: Bool, makeInitialMonthScroll: Bool = false) {
init(showAccessoryViews: Bool, showFloatYearLabel: Bool = true, makeInitialMonthScroll: Bool = false) {
let calendar = Calendar.current
let start = calendar.date(from: .init(year: 2023, month: 11, day: 6))!
let end = calendar.date(from: .init(year: 2024, month: 11, day: 28))!

self.validRange = start...end
self.calendar = calendar
self.showFloatYearLabel = showFloatYearLabel

let formatter = DateFormatter()
formatter.dateStyle = .short
Expand Down Expand Up @@ -96,6 +98,7 @@ struct CalendarExampleRangeView: View {
),
calendar: calendar,
validRange: validRange,
showFloatYearLabel: showFloatYearLabel,
dayAccessoryView: { _ in
BPKIconView(.search, size: .small)
.foregroundColor(.accentColor)
Expand All @@ -109,7 +112,8 @@ struct CalendarExampleRangeView: View {
),
calendar: calendar,
validRange: validRange,
initialMonthScroll: monthScroll
initialMonthScroll: monthScroll,
showFloatYearLabel: showFloatYearLabel
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ struct CalendarGroupsProvider {
presentableCalendar("Range Selection", view: CalendarExampleRangeView(showAccessoryViews: false)),
presentableCalendar("Single Selection", view: CalendarExampleSingleView()),
presentableCalendar("With Accessory Views", view: CalendarExampleRangeView(showAccessoryViews: true)),
presentableCalendar(
"With No Float Year Labelst",
view: CalendarExampleRangeView(showAccessoryViews: false, showFloatYearLabel: false)
),
presentableCalendar("With Whole Month Selection", view: CalendarExampleWholeMonthView()),
presentableCalendar(
"With Initial Month Scrolling",
Expand Down

0 comments on commit f5197c4

Please sign in to comment.