diff --git a/lib/src/event_controller.dart b/lib/src/event_controller.dart index 33415352..2757efb0 100644 --- a/lib/src/event_controller.dart +++ b/lib/src/event_controller.dart @@ -131,7 +131,6 @@ class EventController 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, @@ -150,6 +149,9 @@ class EventController 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, diff --git a/lib/src/modals.dart b/lib/src/modals.dart index f34cec45..a6ec180a 100644 --- a/lib/src/modals.dart +++ b/lib/src/modals.dart @@ -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; @@ -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;