Skip to content

Commit

Permalink
♻️ Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Nov 21, 2024
1 parent 790b419 commit 00e7faa
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions lib/src/event_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,26 @@ class EventController<T extends Object?> extends ChangeNotifier {
currentDate.isAtSameMomentAs(recurrenceEndDate));
}

// Event is not recurring. So, no need to handle recurrence.
// This method checks for the event whether it should exclude
// or not on given date.
// Event is excluded:
// - If given date is of before event start date
// - If given date is after recurrence end date
// - If given date is in excluded list of events.
// On returning true it excludes event and on false it won't exclude.
bool _isExcluded(RecurrenceSettings settings, DateTime date) {
final recurrenceEndDate = settings.endDate;
return (recurrenceEndDate != null && date.isAfter(recurrenceEndDate)) ||
(settings.excludeDates?.contains(date) ?? false);
}

bool _handleRecurrence({
required DateTime currentDate,
required DateTime eventStartDate,
required DateTime eventEndDate,
required RecurrenceSettings recurrenceSettings,
}) {
if (recurrenceSettings.excludeDates?.contains(currentDate) ?? false) {
return false;
}
switch (recurrenceSettings.frequency) {
case RepeatFrequency.doNotRepeat:
break;
Expand Down Expand Up @@ -318,32 +329,30 @@ class EventController<T extends Object?> extends ChangeNotifier {
/// Takes a [date] parameter representing the date for which to retrieve events.
/// Returns a list of [CalendarEventData] objects representing all events on the specified date.
List<CalendarEventData<T>> getAllEventsOnDay(DateTime date) {
final events =
getEventsOnDay(date).where((event) => !event.isExcluded(date)).toList();
final repeatedEvents =
getRepeatedEvents(date).where((event) => !event.isExcluded(date));
final events = getEventsOnDay(date);
final repeatedEvents = getRepeatedEvents(date);
events.addAll(repeatedEvents);

return events;
}

/// Filters list of repeated events to show in the cell for given date
/// from all the repeated events.
/// Event reoccurrence will only show after today's date and event's day.
/// Returns repeated events on given date.
List<CalendarEventData<T>> getRepeatedEvents(DateTime date) {
final repeatedEvents = _calendarData.repeatedEvents;
List<CalendarEventData<T>> events = [];

// Iterate through all repeated events and skips
// if the given date is before start date of repeating event
// or if the date is in excluded list of dates.
// We do not need to handle Recurrence for it.
for (final event in repeatedEvents) {
// Skip the days before start date of repeating event
if (date.isBefore(event.date)) {
continue;
}
final recurrenceSettings = event.recurrenceSettings;
// if event is not repeating or date is in excluded
if (recurrenceSettings == null) {

if (recurrenceSettings == null ||
date.isBefore(event.date) ||
_isExcluded(recurrenceSettings, date)) {
continue;
}

final isRecurrence = _handleRecurrence(
currentDate: date,
eventStartDate: event.date,
Expand All @@ -352,7 +361,7 @@ class EventController<T extends Object?> extends ChangeNotifier {
);

// Add in calendar if event is recurring and
// current date is not same as event start date
// given date is different from start date of event
// because each event is already added to start date of event
if (isRecurrence && !date.isAtSameMomentAs(event.date)) {
events.add(event);
Expand Down

0 comments on commit 00e7faa

Please sign in to comment.