Skip to content

Commit 47579da

Browse files
[UPDATE] added preceding minimum date (#17)
1 parent f17ae8b commit 47579da

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ extension CalendarView {
222222
}
223223

224224
var isPreviousButtonDisabled: Bool {
225+
if appearance.allowPrecedeMinimumDate {
226+
return false
227+
}
225228
// -7 as max days from previous month can be 7.
226229
// current date is first of every month
227230
guard let expectedDate = currentDate.date(byAddingDays: -7)?.dateOnly else { return true }

Sources/YCalendarPicker/SwiftUI/Views/MonthView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ internal struct MonthView {
3232
}
3333

3434
var isPreviousButtonDisabled: Bool {
35+
if appearance.allowPrecedeMinimumDate {
36+
return false
37+
}
3538
// -7 as max days from previous month can be 7.
3639
// current date is first of every month
3740
guard let expectedDate = currentDate.date(byAddingDays: -7)?.dateOnly else { return true }

Sources/YCalendarPicker/UIKit/CalendarPicker+Appearance.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ extension CalendarPicker {
3939
public var monthStyle: (textColor: UIColor, typography: Typography)
4040
/// Background color for calendar view
4141
public var backgroundColor: UIColor
42-
42+
/// Enable preceding to minimum date.
43+
public var allowPrecedeMinimumDate: Bool
44+
4345
/// Initializes a calendar appearance.
4446
/// - Parameters:
4547
/// - normalDayAppearance: Appearance for days within current month. Default is `.Defaults.normal`.
@@ -53,6 +55,8 @@ extension CalendarPicker {
5355
/// - nextImage: Next button image. Default is `Appearance.defaultNextImage`.
5456
/// - monthStyle: Typography and text color for Month name. Default is `DefaultStyles.month`.
5557
/// - backgroundColor: Background color for calendar view. Default is `.systemBackground`.
58+
/// - allowPrecedeMinimumDate: Enable preceding to minimum date. Default is `false`.
59+
5660
public init(
5761
normalDayAppearance: Day = .Defaults.normal,
5862
grayedDayAppearance: Day = .Defaults.grayed,
@@ -64,7 +68,8 @@ extension CalendarPicker {
6468
previousImage: UIImage? = Appearance.defaultPreviousImage,
6569
nextImage: UIImage? = Appearance.defaultNextImage,
6670
monthStyle: (textColor: UIColor, typography: Typography) = DefaultStyles.month,
67-
backgroundColor: UIColor = .systemBackground
71+
backgroundColor: UIColor = .systemBackground,
72+
allowPrecedeMinimumDate: Bool = false
6873
) {
6974
self.normalDayAppearance = normalDayAppearance
7075
self.grayedDayAppearance = grayedDayAppearance
@@ -77,6 +82,7 @@ extension CalendarPicker {
7782
self.nextImage = nextImage
7883
self.monthStyle = monthStyle
7984
self.backgroundColor = backgroundColor
85+
self.allowPrecedeMinimumDate = allowPrecedeMinimumDate
8086
}
8187
}
8288
}

Tests/YCalendarPickerTests/UIKit/CalendarPickerTests.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,34 @@ final class CalendarPickerTests: XCTestCase {
6464
sut.date = selectedDate
6565
XCTAssertEqual(sut.date, selectedDate.dateOnly)
6666
}
67-
67+
68+
func testCalendarPickerPrecedeMinDate() throws {
69+
let minDate = Date().previousDate()
70+
let sut = makeSUT(minDate: minDate)
71+
72+
var monthView = try XCTUnwrap(sut.calendarView.getMonthView() as? MonthView)
73+
74+
XCTAssertTrue(monthView.isPreviousButtonDisabled)
75+
76+
XCTAssertEqual(
77+
sut.calendarView.currentDate,
78+
sut.calendarView.getCurrentDateAfterSwipe(
79+
swipeValue: CGSize(width: 10, height: 10)
80+
)
81+
)
82+
83+
sut.appearance.allowPrecedeMinimumDate = true
84+
monthView = try XCTUnwrap(sut.calendarView.getMonthView() as? MonthView)
85+
XCTAssertFalse(monthView.isPreviousButtonDisabled)
86+
87+
XCTAssertNotEqual(
88+
sut.calendarView.currentDate,
89+
sut.calendarView.getCurrentDateAfterSwipe(
90+
swipeValue: CGSize(width: 10, height: 10)
91+
)
92+
)
93+
}
94+
6895
func testCalendarPickerIsNotNilForOptionalInit() {
6996
XCTAssertNotNil(makeSUTWithFailable())
7097
}

0 commit comments

Comments
 (0)