Skip to content

Commit e7212a4

Browse files
authored
Git Issue(21): To show the given date month when calendar is presented (#22)
* Git Issue(21): To show the given date month when calendar is presented * Removed extra line space
1 parent 36efe97 commit e7212a4

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ init(
3434
appearance: Appearance = .default,
3535
minimumDate: Date? = nil,
3636
maximumDate: Date? = nil,
37+
startDate: Date? = nil,
3738
locale: Locale? = nil
3839
)
3940
```
40-
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.
41+
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.
4142

4243
`CalendarPicker` has an additional initializer:
4344

Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public struct CalendarView {
4242
}
4343
}
4444

45+
/// Start date (if any)
46+
public var startDate: Date?
47+
4548
/// Calendar appearance
4649
public var appearance: CalendarPicker.Appearance {
4750
get {
@@ -91,19 +94,22 @@ public struct CalendarView {
9194
/// - appearance: appearance of calendar view. Default is `Appearance.default`.
9295
/// - minimumDate: minimum date to enable. Default is `nil`.
9396
/// - maximumDate: maximum date to enable. Default is `nil`.
97+
/// - startDate: start date of the calendar. Default is `nil`.
9498
/// - locale: locale for data formatting e.g Date format. Default is `nil`.
9599
public init(
96100
firstWeekday: Int? = nil,
97101
appearance: CalendarPicker.Appearance = .default,
98102
minimumDate: Date? = nil,
99103
maximumDate: Date? = nil,
104+
startDate: Date? = nil,
100105
locale: Locale? = nil
101106
) {
102107
self.firstWeekday = firstWeekday ?? (Locale.current.calendar.firstWeekday - 1)
103108
self.appearance = appearance
104109
self.minimumDate = minimumDate?.dateOnly
105110
self.maximumDate = maximumDate?.dateOnly
106111
self.locale = locale ?? Locale.current
112+
self.startDate = startDate
107113
}
108114
}
109115

@@ -120,6 +126,11 @@ extension CalendarView: View {
120126
})
121127
)
122128
.background(Color(self.appearance.backgroundColor))
129+
.onAppear(perform: {
130+
if let getStartDate = self.startDate {
131+
currentDate = getStartDate
132+
}
133+
})
123134
}
124135

125136
@ViewBuilder

Sources/YCalendarPicker/UIKit/CalendarPicker.swift

+4
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,22 @@ public class CalendarPicker: UIControl {
5858
/// - appearance: appearance for the calendar. Default is `.default`.
5959
/// - minimumDate: minimum selectable date. Default is `nil`.
6060
/// - maximumDate: maximum selectable date. Default is `nil`.
61+
/// - startDate: start date of the calendar. Default is `nil`.
6162
/// - locale: locale for date formatting. Pass `nil` to use current locale. Default is `nil`.
6263
public required init(
6364
firstWeekday: Int? = nil,
6465
appearance: Appearance = .default,
6566
minimumDate: Date? = nil,
6667
maximumDate: Date? = nil,
68+
startDate: Date? = nil,
6769
locale: Locale? = nil
6870
) {
6971
calendarView = CalendarView(
7072
firstWeekday: firstWeekday,
7173
appearance: appearance,
7274
minimumDate: minimumDate,
7375
maximumDate: maximumDate,
76+
startDate: startDate,
7477
locale: locale
7578
)
7679
super.init(frame: .zero)
@@ -83,6 +86,7 @@ public class CalendarPicker: UIControl {
8386
appearance: Appearance(),
8487
minimumDate: nil,
8588
maximumDate: nil,
89+
startDate: nil,
8690
locale: nil
8791
)
8892
super.init(coder: coder)

Tests/YCalendarPickerTests/SwiftUI/CalendarViewTests.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ final class CalendarViewTests: XCTestCase {
177177
XCTAssertNil(sut.date)
178178
}
179179

180+
func testOnStartDate() {
181+
let sut = makeSUT(startDate: Date().date(byAddingMonth: 4))
182+
XCTAssertNotNil(sut.startDate)
183+
XCTAssertNotEqual(sut.currentDate, sut.startDate)
184+
}
185+
180186
func testCalendarViewPreviewIsNotNill() {
181187
XCTAssertNotNil(CalendarView_Previews.previews)
182188
}
@@ -190,12 +196,14 @@ private extension CalendarViewTests {
190196
func makeSUT(
191197
firstWeekday: Int? = nil,
192198
minimumDate: Date? = nil,
193-
maximumDate: Date? = nil
199+
maximumDate: Date? = nil,
200+
startDate: Date? = nil
194201
) -> CalendarView {
195202
let sut = CalendarView(
196203
firstWeekday: firstWeekday ?? 0,
197204
minimumDate: minimumDate,
198-
maximumDate: maximumDate
205+
maximumDate: maximumDate,
206+
startDate: startDate
199207
)
200208
XCTAssertNotNil(sut.body)
201209
return sut

0 commit comments

Comments
 (0)