Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git Issue(21): To show the given date month when calendar is presented #22

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
11 changes: 11 additions & 0 deletions Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public struct CalendarView {
}
}

/// Start date (if any)
public var startDate: Date?

/// Calendar appearance
public var appearance: CalendarPicker.Appearance {
get {
Expand Down Expand Up @@ -91,19 +94,22 @@ 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)
self.appearance = appearance
self.minimumDate = minimumDate?.dateOnly
self.maximumDate = maximumDate?.dateOnly
self.locale = locale ?? Locale.current
self.startDate = startDate
}
}

Expand All @@ -120,6 +126,11 @@ extension CalendarView: View {
})
)
.background(Color(self.appearance.backgroundColor))
.onAppear(perform: {
if let getStartDate = self.startDate {
currentDate = getStartDate
}
})
}

@ViewBuilder
Expand Down
4 changes: 4 additions & 0 deletions Sources/YCalendarPicker/UIKit/CalendarPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ 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(
firstWeekday: firstWeekday,
appearance: appearance,
minimumDate: minimumDate,
maximumDate: maximumDate,
startDate: startDate,
locale: locale
)
super.init(frame: .zero)
Expand All @@ -83,6 +86,7 @@ public class CalendarPicker: UIControl {
appearance: Appearance(),
minimumDate: nil,
maximumDate: nil,
startDate: nil,
locale: nil
)
super.init(coder: coder)
Expand Down
12 changes: 10 additions & 2 deletions Tests/YCalendarPickerTests/SwiftUI/CalendarViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down