Skip to content

Commit

Permalink
✨ Handle yearly recurring event with leap year
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-jitiya-simform committed Nov 22, 2024
1 parent d7ce254 commit d7e12b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/src/event_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class EventController<T extends Object?> extends ChangeNotifier {
// Returns true if event should repeat on the given date otherwise false.
// For monthly repetition of event event start date & given date should match.
// repetition will include the recurrence end date.
// TODO(Shubham): Handle feb or 30/31 dates.
bool _isMonthlyRecurrence({
required DateTime currentDate,
required DateTime startDate,
Expand All @@ -150,6 +149,9 @@ class EventController<T extends Object?> extends ChangeNotifier {
currentDate.isAtSameMomentAs(recurrenceEndDate));
}

// If end date is not mentioned repeat infinitely
// If end date is mentioned repeat till end date including last date
// End date will change in case of "Following events" are deleted
bool _isYearlyRecurrence({
required DateTime currentDate,
required DateTime startDate,
Expand Down
33 changes: 22 additions & 11 deletions lib/src/modals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.

import 'package:calendar_view/calendar_view.dart';
import 'package:flutter/material.dart';

import 'enumerations.dart';
import 'typedefs.dart';

/// Settings for hour lines
class HourIndicatorSettings {
final double height;
Expand Down Expand Up @@ -221,22 +219,35 @@ class RecurrenceSettings {
return resultDate;
}

/// Calculate end date for yearly recurring event
DateTime get _endDateYearly {
var occurrences = (interval ?? 1) - 1;
var nextDate = startDate;

// If the start date is not 29th Feb, we can directly calculate last year.
if (startDate.day != 29 && startDate.month != DateTime.february) {
return DateTime(
nextDate.year + occurrences,
startDate.month,
startDate.day,
);
}
// TODO(Shubham): Optimize for larger recurrences if required
while (occurrences > 0) {
nextDate = DateTime(
final newDate = DateTime(
nextDate.year + 1,
nextDate.month,
nextDate.day,
startDate.month,
startDate.day,
);

// TODO(Shubham): Verify for invalid date in next month
// Identify leap year and update next date accordingly
// if (nextDate.day != startDate.day) {
// continue;
// }
// If month changes that means that date does not exist in given year
if (newDate.month != startDate.month) {
nextDate = DateTime(
newDate.year,
);
continue;
}
nextDate = newDate;
occurrences--;
}
return nextDate;
Expand Down

0 comments on commit d7e12b0

Please sign in to comment.