Skip to content

Commit 0c15e8c

Browse files
author
Shubham Jitiya
committed
✨ Handle for month
1 parent fabcd8f commit 0c15e8c

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

lib/src/event_controller.dart

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,47 @@ class EventController<T extends Object?> extends ChangeNotifier {
124124
return currentDate.isBefore(endDate);
125125
}
126126
}
127+
128+
bool _isMonthlyRecurrence({
129+
required DateTime currentDate,
130+
required DateTime startDate,
131+
required RecurrenceSettings recurrenceSettings,
132+
}) {
133+
final recurrenceEndDate = recurrenceSettings.endDate;
134+
final interval = recurrenceSettings.interval;
135+
136+
switch (recurrenceSettings.recurrenceEndOn) {
137+
case RecurrenceEnd.never:
138+
return currentDate.day == startDate.day;
139+
case RecurrenceEnd.on:
140+
return recurrenceEndDate != null &&
141+
(currentDate.day == startDate.day) &&
142+
currentDate.isBefore(recurrenceEndDate.add(
143+
Duration(days: 1),
144+
));
145+
146+
/// Calculate the last valid occurrence based on the interval count
147+
/// `interval - 1` is used here because:
148+
/// - The `interval` represents the total number of occurrences, including the starting month.
149+
/// - Subtracting 1 allows us to calculate the number of additional months to add
150+
/// after the start month to get to the final occurrence.
151+
/// For example, if `interval = 3`, occurrences should happen in:
152+
/// - Start month (1st occurrence), plus two additional months to reach the 3rd occurrence.
153+
case RecurrenceEnd.after:
154+
if (interval == null || interval <= 1) {
155+
return false;
156+
}
157+
final lastOccurrenceDate = DateTime(
158+
startDate.year,
159+
startDate.month + (interval - 1),
160+
startDate.day,
161+
);
162+
return (currentDate.day == startDate.day) &&
163+
currentDate.isBefore(lastOccurrenceDate.add(
164+
Duration(days: 1),
165+
));
166+
}
167+
}
127168
//#endregion
128169

129170
//#region Public Methods
@@ -251,7 +292,7 @@ class EventController<T extends Object?> extends ChangeNotifier {
251292
case RepeatFrequency.monthly:
252293
final isWeeklyRecurrence = _isMonthlyRecurrence(
253294
currentDate: date,
254-
eventEnd: event.endDate,
295+
startDate: event.date,
255296
recurrenceSettings: recurrenceSettings,
256297
);
257298
if (isWeeklyRecurrence) {
@@ -262,7 +303,6 @@ class EventController<T extends Object?> extends ChangeNotifier {
262303
// TODO: Handle this case.
263304
break;
264305
case RepeatFrequency.doNotRepeat:
265-
// TODO: Handle this case.
266306
break;
267307
}
268308
}

0 commit comments

Comments
 (0)