Skip to content

Commit 9e30bb1

Browse files
[CM-1503] added hidden functionality for day view (#14)
* [UPDATE] added hidden functionality for day view * Updated precedence, swipe bug resolved * updated read me
1 parent 3fc6043 commit 9e30bb1

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public struct Day {
109109
public var borderColor: UIColor
110110
/// Border width for day view
111111
public var borderWidth: CGFloat
112+
/// Hides day view (if true)
113+
public var isHidden: Bool
112114
}
113115
```
114116

Sources/YCalendarPicker/Model/CalendarMonthItem+DayAppearance.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ extension CalendarMonthItem {
1313
if isBooked {
1414
return appearance.bookedDayAppearance
1515
} else if !isEnabled {
16+
if isGrayedOut {
17+
return appearance.grayedDayAppearance
18+
}
1619
return appearance.disabledDayAppearance
1720
} else if isSelected {
1821
return appearance.selectedDayAppearance

Sources/YCalendarPicker/SwiftUI/Views/CalendarView.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,30 @@ extension CalendarView {
212212

213213
func getCurrentDateAfterSwipe(swipeValue: CGSize) -> Date {
214214
let monthCount = (swipeValue.width > 0) ? -1 : 1
215-
215+
216+
var isNextButtonDisabled: Bool {
217+
guard let expectedDate = currentDate.date(byAddingMonth: 1)?.dateOnly else { return true }
218+
if let maxDate = maximumDate, expectedDate > maxDate {
219+
return true
220+
}
221+
return false
222+
}
223+
224+
var isPreviousButtonDisabled: Bool {
225+
// -7 as max days from previous month can be 7.
226+
// current date is first of every month
227+
guard let expectedDate = currentDate.date(byAddingDays: -7)?.dateOnly else { return true }
228+
229+
if let minDate = minimumDate, expectedDate < minDate {
230+
return true
231+
}
232+
return false
233+
}
234+
235+
if (monthCount == -1 && isPreviousButtonDisabled) || (monthCount == 1 && isNextButtonDisabled) {
236+
return currentDate
237+
}
238+
216239
return currentDate.date(byAddingMonth: monthCount)?.dateOnly ?? currentDate
217240
}
218241

Sources/YCalendarPicker/SwiftUI/Views/DayView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extension DayView: View {
4747
.padding(.horizontal, DayView.padding)
4848
.padding(.vertical, DayView.padding)
4949
.onTapGesture {
50+
guard !appearance.isHidden else { return }
5051
guard dateItem.isEnabled else { return }
5152
selectedDate = dateItem.date
5253
}

Sources/YCalendarPicker/UIKit/CalendarPicker+Appearance+Day.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extension CalendarPicker.Appearance {
2222
public var borderColor: UIColor
2323
/// Border width for day view
2424
public var borderWidth: CGFloat
25+
/// Hides day view (if true)
26+
public var isHidden: Bool
2527

2628
/// Initializes a calendar day appearance
2729
/// - Parameters:
@@ -30,18 +32,26 @@ extension CalendarPicker.Appearance {
3032
/// - backgroundColor: Background color for day view. Default is `.clear`.
3133
/// - borderColor: Border color for day view. Default is `.clear`.
3234
/// - borderWidth: Border width for day view. Default is `1.0`.
35+
/// - isHidden: Hides day(s). Default is `false`.
3336
public init(
3437
typography: Typography = .day,
3538
foregroundColor: UIColor = .label,
3639
backgroundColor: UIColor = .clear,
3740
borderColor: UIColor = .clear,
38-
borderWidth: CGFloat = 1.0
41+
borderWidth: CGFloat = 1.0,
42+
isHidden: Bool = false
3943
) {
40-
self.typography = typography
41-
self.foregroundColor = foregroundColor
42-
self.backgroundColor = backgroundColor
43-
self.borderColor = borderColor
44-
self.borderWidth = borderWidth
44+
if isHidden {
45+
self = Defaults.hidden
46+
self.isHidden = isHidden
47+
} else {
48+
self.typography = typography
49+
self.foregroundColor = foregroundColor
50+
self.backgroundColor = backgroundColor
51+
self.borderColor = borderColor
52+
self.borderWidth = borderWidth
53+
self.isHidden = isHidden
54+
}
4555
}
4656
}
4757
}
@@ -74,5 +84,11 @@ extension CalendarPicker.Appearance.Day {
7484
foregroundColor: CalendarPicker.Appearance.onTintColor,
7585
backgroundColor: CalendarPicker.Appearance.tintColor
7686
)
87+
88+
/// Default appearance for hidden day
89+
public static let hidden = CalendarPicker.Appearance.Day(
90+
foregroundColor: .clear,
91+
backgroundColor: .clear
92+
)
7793
}
7894
}

Tests/YCalendarPickerTests/UIKit/CalendarPickerAppearanceDayTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ final class CalendarPickerAppearanceDayTests: XCTestCase {
2727
XCTAssertEqual(sut.borderColor, UIColor.clear)
2828
XCTAssertEqual(sut.borderWidth, 1.0)
2929
}
30+
31+
func testHiddenAppearance() {
32+
let sut = CalendarPicker.Appearance.Day(isHidden: true)
33+
XCTAssertTypographyEqual(sut.typography, .day)
34+
XCTAssertEqual(sut.foregroundColor, UIColor.clear)
35+
XCTAssertEqual(sut.backgroundColor, UIColor.clear)
36+
XCTAssertEqual(sut.borderColor, UIColor.clear)
37+
XCTAssertEqual(sut.borderWidth, 1.0)
38+
}
3039
}

0 commit comments

Comments
 (0)