diff --git a/README.md b/README.md index 173116b..cae4dd3 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,11 @@ init( appearance: Appearance = .default, minimumDate: Date? = nil, maximumDate: Date? = nil, + startDate: Date? = nil, locale: Locale? = nil ) ``` -The standard initializer lets you specify the first day of the week, appearance, optional minimum and maximum dates, and the locale, although it provides sensible defaults for all of these. +The standard initializer lets you specify the first day of the week, appearance, optional minimum and maximum dates, start date of the calendar and the locale, although it provides sensible defaults for all of these. `CalendarPicker` has an additional initializer: diff --git a/Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift b/Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift index 09bb40c..5df6a44 100644 --- a/Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift +++ b/Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift @@ -42,6 +42,9 @@ public struct CalendarView { } } + /// Start date (if any) + public var startDate: Date? + /// Calendar appearance public var appearance: CalendarPicker.Appearance { get { @@ -91,12 +94,14 @@ public struct CalendarView { /// - appearance: appearance of calendar view. Default is `Appearance.default`. /// - minimumDate: minimum date to enable. Default is `nil`. /// - maximumDate: maximum date to enable. Default is `nil`. + /// - startDate: start date of the calendar. Default is `nil`. /// - locale: locale for data formatting e.g Date format. Default is `nil`. public init( firstWeekday: Int? = nil, appearance: CalendarPicker.Appearance = .default, minimumDate: Date? = nil, maximumDate: Date? = nil, + startDate: Date? = nil, locale: Locale? = nil ) { self.firstWeekday = firstWeekday ?? (Locale.current.calendar.firstWeekday - 1) @@ -104,6 +109,7 @@ public struct CalendarView { self.minimumDate = minimumDate?.dateOnly self.maximumDate = maximumDate?.dateOnly self.locale = locale ?? Locale.current + self.startDate = startDate } } @@ -120,6 +126,11 @@ extension CalendarView: View { }) ) .background(Color(self.appearance.backgroundColor)) + .onAppear(perform: { + if let getStartDate = self.startDate { + currentDate = getStartDate + } + }) } @ViewBuilder diff --git a/Sources/YCalendarPicker/UIKit/CalendarPicker.swift b/Sources/YCalendarPicker/UIKit/CalendarPicker.swift index 2a3fd90..659b212 100644 --- a/Sources/YCalendarPicker/UIKit/CalendarPicker.swift +++ b/Sources/YCalendarPicker/UIKit/CalendarPicker.swift @@ -58,12 +58,14 @@ public class CalendarPicker: UIControl { /// - appearance: appearance for the calendar. Default is `.default`. /// - minimumDate: minimum selectable date. Default is `nil`. /// - maximumDate: maximum selectable date. Default is `nil`. + /// - startDate: start date of the calendar. Default is `nil`. /// - locale: locale for date formatting. Pass `nil` to use current locale. Default is `nil`. public required init( firstWeekday: Int? = nil, appearance: Appearance = .default, minimumDate: Date? = nil, maximumDate: Date? = nil, + startDate: Date? = nil, locale: Locale? = nil ) { calendarView = CalendarView( @@ -71,6 +73,7 @@ public class CalendarPicker: UIControl { appearance: appearance, minimumDate: minimumDate, maximumDate: maximumDate, + startDate: startDate, locale: locale ) super.init(frame: .zero) @@ -83,6 +86,7 @@ public class CalendarPicker: UIControl { appearance: Appearance(), minimumDate: nil, maximumDate: nil, + startDate: nil, locale: nil ) super.init(coder: coder) diff --git a/Tests/YCalendarPickerTests/SwiftUI/CalendarViewTests.swift b/Tests/YCalendarPickerTests/SwiftUI/CalendarViewTests.swift index 2d63dad..d526419 100644 --- a/Tests/YCalendarPickerTests/SwiftUI/CalendarViewTests.swift +++ b/Tests/YCalendarPickerTests/SwiftUI/CalendarViewTests.swift @@ -177,6 +177,12 @@ final class CalendarViewTests: XCTestCase { XCTAssertNil(sut.date) } + func testOnStartDate() { + let sut = makeSUT(startDate: Date().date(byAddingMonth: 4)) + XCTAssertNotNil(sut.startDate) + XCTAssertNotEqual(sut.currentDate, sut.startDate) + } + func testCalendarViewPreviewIsNotNill() { XCTAssertNotNil(CalendarView_Previews.previews) } @@ -190,12 +196,14 @@ private extension CalendarViewTests { func makeSUT( firstWeekday: Int? = nil, minimumDate: Date? = nil, - maximumDate: Date? = nil + maximumDate: Date? = nil, + startDate: Date? = nil ) -> CalendarView { let sut = CalendarView( firstWeekday: firstWeekday ?? 0, minimumDate: minimumDate, - maximumDate: maximumDate + maximumDate: maximumDate, + startDate: startDate ) XCTAssertNotNil(sut.body) return sut