Skip to content

Commit 9770f6c

Browse files
committed
feat: outofrange logic for calendarmodel
1 parent d6a00b7 commit 9770f6c

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

Sources/OBCalendar/Examples/DemoCalendar.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct DemoCalendar: View {
6464
OBCalendar(years: years) { model,scrollProxy in
6565
ZStack {
6666
let day = model.day
67-
if day.dateType == .currentMonth {
67+
if case .insideRange(.currentMonth) = model.day.dateType {
6868
Text("\(day.day)")
6969
.foregroundColor(
7070
selectedDate == day.date
@@ -78,8 +78,8 @@ struct DemoCalendar: View {
7878
.frame(width: 35, height: 35)
7979
.background(
8080
ContentBuilder.buildContent {
81-
if selectedDate == model.day.date
82-
&& model.day.dateType == .currentMonth {
81+
if selectedDate == model.day.date,
82+
case .insideRange(.currentMonth) = model.day.dateType {
8383
Circle()
8484
.foregroundColor(.green)
8585
}

Sources/OBCalendar/Model/CalendarModel.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ public enum CalendarModel {
2525
case previousMonth
2626
case nextMonth
2727
case currentMonth
28-
case outOfRange
28+
}
29+
30+
public enum RangeType {
31+
case outOfRange(DateType)
32+
case insideRange(DateType)
2933
}
3034

3135
public let day: Int
3236
public let date: Date
33-
public let dateType: DateType
37+
public let dateType: RangeType
3438
}
3539
}
3640

3741
extension Array where Element == CalendarModel.Year {
38-
mutating func appendDay(number: Int, date: Date, dateType: CalendarModel.Day.DateType) {
42+
mutating func appendDay(number: Int, date: Date, dateType: CalendarModel.Day.RangeType) {
3943
let lastMonths = self[self.endIndex-1].months
4044
self[self.endIndex-1]
4145
.months[lastMonths.endIndex-1]

Sources/OBCalendar/OBCalendar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public struct OBCalendar<
117117
) { model, scrollProxy in
118118

119119
ZStack {
120-
if model.day.dateType == .currentMonth {
120+
if case .insideRange(.currentMonth) = model.day.dateType {
121121
Text("\(model.day.day)")
122122
} else {
123123
placeholderView

Sources/OBCalendar/Utility/CalendarModelBuilder.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public enum CalendarModelBuilder {
5656
result.appendDay(
5757
number: currentDay,
5858
date: currentDate,
59-
dateType: .currentMonth
59+
dateType: .insideRange(.currentMonth)
6060
)
6161

6262
if let nextDay = calendar.date(byAdding: .day, value: 1, to: currentDate) {
@@ -118,8 +118,12 @@ extension CalendarModelBuilder {
118118
number: calendar.component(.day, from: dateToBeAdded),
119119
date: dateToBeAdded,
120120
dateType: dateToBeAdded < startingDate
121-
? .outOfRange
122-
: .previousMonth
121+
? .outOfRange(
122+
calendar.compare(dateToBeAdded, to: startingDate, toGranularity: .month) == .orderedSame
123+
? .currentMonth
124+
: .previousMonth
125+
)
126+
: .insideRange(.previousMonth)
123127
)
124128
}
125129
}
@@ -151,8 +155,12 @@ extension CalendarModelBuilder {
151155
number: number,
152156
date: dateToBeAdded,
153157
dateType: dateToBeAdded > endingDate
154-
? .outOfRange
155-
: .nextMonth
158+
? .outOfRange(
159+
calendar.compare(dateToBeAdded, to: endingDate, toGranularity: .month) == .orderedSame
160+
? .currentMonth
161+
: .nextMonth
162+
)
163+
: .insideRange(.nextMonth)
156164
)
157165
}
158166
}

0 commit comments

Comments
 (0)