Skip to content

Commit

Permalink
♻️ refactor recurrence handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Oct 30, 2024
1 parent 0c15e8c commit 3fd2504
Showing 1 changed file with 45 additions and 36 deletions.
81 changes: 45 additions & 36 deletions lib/src/event_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,43 @@ class EventController<T extends Object?> extends ChangeNotifier {
));
}
}

bool _handleRecurrence({
required DateTime currentDate,
required DateTime eventStartDate,
required DateTime eventEndDate,
required RecurrenceSettings recurrenceSettings,
}) {
switch (recurrenceSettings.frequency) {
case RepeatFrequency.daily:
final isDailyRecurrence = _isDailyRecurrence(
currentDate: currentDate,
eventEnd: eventEndDate,
recurrenceSettings: recurrenceSettings,
);
return isDailyRecurrence;
case RepeatFrequency.weekly:
final isWeeklyRecurrence = _isWeeklyRecurrence(
currentDate: currentDate,
eventStartDate: eventStartDate,
recurrenceSettings: recurrenceSettings,
);
return isWeeklyRecurrence;
case RepeatFrequency.monthly:
final isMonthlyRecurrence = _isMonthlyRecurrence(
currentDate: currentDate,
startDate: eventStartDate,
recurrenceSettings: recurrenceSettings,
);
return isMonthlyRecurrence;
case RepeatFrequency.yearly:
// TODO(Shubham): Handle this case.
break;
case RepeatFrequency.doNotRepeat:
break;
}
return false;
}
//#endregion

//#region Public Methods
Expand Down Expand Up @@ -268,42 +305,14 @@ class EventController<T extends Object?> extends ChangeNotifier {
if (recurrenceSettings == null) {
continue;
}
switch (recurrenceSettings.frequency) {
case RepeatFrequency.daily:
final isDailyRecurrence = _isDailyRecurrence(
currentDate: date,
eventEnd: event.endDate,
recurrenceSettings: recurrenceSettings,
);
if (isDailyRecurrence) {
events.add(event);
}
break;
case RepeatFrequency.weekly:
final isWeeklyRecurrence = _isWeeklyRecurrence(
currentDate: date,
eventStartDate: event.date,
recurrenceSettings: recurrenceSettings,
);
if (isWeeklyRecurrence) {
events.add(event);
}
break;
case RepeatFrequency.monthly:
final isWeeklyRecurrence = _isMonthlyRecurrence(
currentDate: date,
startDate: event.date,
recurrenceSettings: recurrenceSettings,
);
if (isWeeklyRecurrence) {
events.add(event);
}
break;
case RepeatFrequency.yearly:
// TODO: Handle this case.
break;
case RepeatFrequency.doNotRepeat:
break;
final isRecurrence = _handleRecurrence(
currentDate: date,
eventStartDate: event.date,
eventEndDate: event.endDate,
recurrenceSettings: recurrenceSettings,
);
if (isRecurrence) {
events.add(event);
}
}
return events;
Expand Down

0 comments on commit 3fd2504

Please sign in to comment.