From 7346d4f047a78966e76888f53f25dea21ada5ad0 Mon Sep 17 00:00:00 2001 From: Shubham Jitiya Date: Wed, 23 Oct 2024 16:39:34 +0530 Subject: [PATCH] =?UTF-8?q?feat:=20Fixes=20issue=20#378:=20=E2=9C=A8Added?= =?UTF-8?q?=20support=20for=20daily,=20weekly=20&=20yearly=20recurrence=20?= =?UTF-8?q?event=20in=20month=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/constants.dart | 2 + example/lib/main.dart | 19 ++ example/lib/pages/event_details_page.dart | 48 ++- example/lib/widgets/add_event_form.dart | 314 +++++++++++++++++++ example/lib/widgets/day_view_widget.dart | 1 + example/lib/widgets/delete_event_dialog.dart | 63 ++++ example/lib/widgets/month_view_widget.dart | 1 + example/lib/widgets/week_view_widget.dart | 1 + lib/src/calendar_event_data.dart | 7 + lib/src/enumerations.dart | 35 +++ lib/src/event_controller.dart | 274 +++++++++++++++- lib/src/modals.dart | 252 ++++++++++++++- lib/src/month_view/month_view.dart | 3 +- 13 files changed, 1004 insertions(+), 16 deletions(-) create mode 100644 example/lib/widgets/delete_event_dialog.dart diff --git a/example/lib/constants.dart b/example/lib/constants.dart index 36ec8416..21da78d8 100644 --- a/example/lib/constants.dart +++ b/example/lib/constants.dart @@ -5,6 +5,8 @@ import 'app_colors.dart'; class AppConstants { AppConstants._(); + static final List weekTitles = ['M', 'T', 'W', 'T', 'F', 'S', 'S']; + static OutlineInputBorder inputBorder = OutlineInputBorder( borderRadius: BorderRadius.circular(7), borderSide: BorderSide( diff --git a/example/lib/main.dart b/example/lib/main.dart index 146e23af..68411964 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -42,10 +42,29 @@ List _events = [ startTime: DateTime(_now.year, _now.month, _now.day, 18, 30), endTime: DateTime(_now.year, _now.month, _now.day, 22), ), + CalendarEventData( + date: _now.subtract(Duration(days: 3)), + recurrenceSettings: RecurrenceSettings.withCalculatedEndDate( + startDate: _now.subtract(Duration(days: 3)), + endDate: _now.subtract(Duration(days: 3)), + frequency: RepeatFrequency.daily, + recurrenceEndOn: RecurrenceEnd.after, + interval: 5, + ), + title: 'Physics test prep', + description: 'Prepare for physics test', + ), CalendarEventData( date: _now.add(Duration(days: 1)), startTime: DateTime(_now.year, _now.month, _now.day, 18), endTime: DateTime(_now.year, _now.month, _now.day, 19), + recurrenceSettings: RecurrenceSettings( + startDate: _now, + endDate: _now.add(Duration(days: 5)), + frequency: RepeatFrequency.daily, + recurrenceEndOn: RecurrenceEnd.after, + interval: 5, + ), title: "Wedding anniversary", description: "Attend uncle's wedding anniversary.", ), diff --git a/example/lib/pages/event_details_page.dart b/example/lib/pages/event_details_page.dart index 2785688d..f4625953 100644 --- a/example/lib/pages/event_details_page.dart +++ b/example/lib/pages/event_details_page.dart @@ -1,4 +1,5 @@ import 'package:calendar_view/calendar_view.dart'; +import 'package:example/widgets/delete_event_dialog.dart'; import 'package:flutter/material.dart'; import '../extension.dart'; @@ -6,8 +7,14 @@ import 'create_event_page.dart'; class DetailsPage extends StatelessWidget { final CalendarEventData event; + final DateTime date; + + const DetailsPage({ + required this.event, + required this.date, + super.key, + }); - const DetailsPage({super.key, required this.event}); @override Widget build(BuildContext context) { return Scaffold( @@ -83,17 +90,15 @@ class DetailsPage extends StatelessWidget { SizedBox( height: 10.0, ), - Text(event.description!), + Text(event.description!) ], const SizedBox(height: 50), Row( children: [ Expanded( child: ElevatedButton( - onPressed: () { - CalendarControllerProvider.of(context) - .controller - .remove(event); + onPressed: () async { + await _handleDeleteEvent(context); Navigator.of(context).pop(); }, child: Text('Delete Event'), @@ -111,7 +116,7 @@ class DetailsPage extends StatelessWidget { ), ); - if (result) { + if (result != null) { Navigator.of(context).pop(); } }, @@ -124,4 +129,33 @@ class DetailsPage extends StatelessWidget { ), ); } + + /// Handles the deletion of an event, showing a dialog for repeating events. + /// + /// This method checks if the event is a repeating event. If it is, it shows + /// a dialog to the user to choose the deletion type (e.g., delete this + /// event, delete following events, delete all events). + /// If the event is not repeating, it defaults to deleting all occurrences + /// of the event. + Future _handleDeleteEvent(BuildContext context) async { + DeleteEvent? result; + final isRepeatingEvent = event.recurrenceSettings != null && + (event.recurrenceSettings?.frequency != RepeatFrequency.doNotRepeat); + + if (isRepeatingEvent) { + result = await showDialog( + context: context, + builder: (_) => DeleteEventDialog(), + ); + } else { + result = DeleteEvent.all; + } + if (result != null) { + CalendarControllerProvider.of(context).controller.deleteRecurrenceEvent( + date: date, + event: event, + deleteEventType: result, + ); + } + } } diff --git a/example/lib/widgets/add_event_form.dart b/example/lib/widgets/add_event_form.dart index 40b18718..3e78b101 100644 --- a/example/lib/widgets/add_event_form.dart +++ b/example/lib/widgets/add_event_form.dart @@ -25,15 +25,20 @@ class AddOrEditEventForm extends StatefulWidget { class _AddOrEditEventFormState extends State { late DateTime _startDate = DateTime.now().withoutTime; late DateTime _endDate = DateTime.now().withoutTime; + late DateTime _recurrenceEndDate = DateTime.now().withoutTime; DateTime? _startTime; DateTime? _endTime; + List _selectedDays = List.filled(7, false); + RepeatFrequency? _selectedFrequency = RepeatFrequency.doNotRepeat; + RecurrenceEnd? _selectedRecurrenceEnd = RecurrenceEnd.never; Color _color = Colors.blue; final _form = GlobalKey(); late final _descriptionController = TextEditingController(); + late final _occurrenceController = TextEditingController(); late final _titleController = TextEditingController(); late final _titleNode = FocusNode(); late final _descriptionNode = FocusNode(); @@ -43,6 +48,7 @@ class _AddOrEditEventFormState extends State { super.initState(); _setDefaults(); + _setInitialWeekday(); } @override @@ -52,6 +58,7 @@ class _AddOrEditEventFormState extends State { _descriptionController.dispose(); _titleController.dispose(); + _occurrenceController.dispose(); super.dispose(); } @@ -102,6 +109,7 @@ class _AddOrEditEventFormState extends State { } _startDate = date.withoutTime; + updateWeekdaysSelection(); if (mounted) { setState(() {}); @@ -137,6 +145,8 @@ class _AddOrEditEventFormState extends State { )); } else { _endDate = date.withoutTime; + _recurrenceEndDate = _endDate; + updateWeekdaysSelection(); } if (mounted) { @@ -247,6 +257,265 @@ class _AddOrEditEventFormState extends State { hintText: "Event Description", ), ), + Align( + alignment: Alignment.centerLeft, + child: Text( + 'Repeat', + style: TextStyle( + color: AppColors.black, + fontWeight: FontWeight.w500, + fontSize: 17, + ), + ), + ), + Row( + children: [ + Radio( + value: RepeatFrequency.doNotRepeat, + groupValue: _selectedFrequency, + onChanged: (value) { + setState( + () => _selectedFrequency = value, + ); + }, + ), + Text( + 'Do not repeat', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ), + ], + ), + Row( + children: [ + Radio( + value: RepeatFrequency.daily, + groupValue: _selectedFrequency, + onChanged: (value) { + setState( + () => _selectedFrequency = value, + ); + }, + ), + Text( + 'Daily', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ) + ], + ), + Row( + children: [ + Radio( + value: RepeatFrequency.weekly, + groupValue: _selectedFrequency, + onChanged: (value) { + setState( + () => _selectedFrequency = value, + ); + }, + ), + Text( + 'Weekly', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ), + ], + ), + Row( + children: [ + Radio( + value: RepeatFrequency.monthly, + groupValue: _selectedFrequency, + onChanged: (value) { + setState( + () => _selectedFrequency = value, + ); + }, + ), + Text( + 'Monthly', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ), + ], + ), + Row( + children: [ + Radio( + value: RepeatFrequency.yearly, + groupValue: _selectedFrequency, + onChanged: (value) { + setState( + () => _selectedFrequency = value, + ); + }, + ), + Text( + 'Yearly', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ), + ], + ), + if (_selectedFrequency == RepeatFrequency.weekly) ...[ + Wrap( + children: + List.generate(AppConstants.weekTitles.length, (int index) { + return ChoiceChip( + label: Text(AppConstants.weekTitles[index]), + showCheckmark: false, + selected: _selectedDays[index], + onSelected: (selected) { + setState(() { + _selectedDays[index] = selected; + if (!_selectedDays.contains(true)) { + _selectedDays[_startDate.weekday - 1] = true; + } + }); + }, + shape: CircleBorder(), + ); + }).toList(), + ), + ], + SizedBox(height: 15), + if (_selectedFrequency != RepeatFrequency.doNotRepeat) + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'Reoccurrence ends on: ', + style: TextStyle( + color: AppColors.black, + fontWeight: FontWeight.w500, + fontSize: 17, + ), + ), + Row( + children: [ + Radio( + value: RecurrenceEnd.never, + groupValue: _selectedRecurrenceEnd, + onChanged: (value) => setState( + () => _selectedRecurrenceEnd = value, + ), + ), + Text( + 'Never', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ), + ], + ), + Row( + children: [ + Radio( + value: RecurrenceEnd.onDate, + groupValue: _selectedRecurrenceEnd, + onChanged: (value) => setState( + () => _selectedRecurrenceEnd = value, + ), + ), + Text( + 'On', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ) + ], + ), + Row( + children: [ + Radio( + value: RecurrenceEnd.after, + groupValue: _selectedRecurrenceEnd, + onChanged: (value) => setState( + () => _selectedRecurrenceEnd = value, + ), + ), + Text( + 'After', + style: TextStyle( + color: AppColors.black, + fontSize: 17, + ), + ), + ], + ), + ], + ), + SizedBox(height: 15), + if (_selectedRecurrenceEnd == RecurrenceEnd.onDate) + DateTimeSelectorFormField( + initialDateTime: _recurrenceEndDate, + decoration: AppConstants.inputDecoration.copyWith( + labelText: 'Ends on', + ), + onSelect: (date) { + if (date.withoutTime.isBefore(_endDate.withoutTime)) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text('Recurrence ends after end date'), + )); + } else { + _recurrenceEndDate = date.withoutTime; + } + + if (mounted) { + setState(() {}); + } + }, + validator: (value) { + if (value == null || value == "") { + return 'Please select end date.'; + } + + // TODO(Shubham): Add validation of end occurrence >= end date + return null; + }, + textStyle: TextStyle( + color: AppColors.black, + fontSize: 17.0, + ), + onSave: (date) => _recurrenceEndDate = date ?? _recurrenceEndDate, + type: DateTimeSelectionType.date, + ), + if (_selectedRecurrenceEnd == RecurrenceEnd.after) + TextFormField( + controller: _occurrenceController, + style: TextStyle( + color: AppColors.black, + fontSize: 17.0, + ), + keyboardType: TextInputType.number, + minLines: 1, + maxLength: 3, + validator: (value) { + if (value == null || value.trim() == '') { + return 'Please specify occurrences'; + } + + return null; + }, + decoration: AppConstants.inputDecoration.copyWith( + hintText: '30', + suffixText: 'Occurrences', + counterText: '', + ), + ), SizedBox( height: 15.0, ), @@ -285,6 +554,15 @@ class _AddOrEditEventFormState extends State { _form.currentState?.save(); + final recurrenceSettings = RecurrenceSettings.withCalculatedEndDate( + startDate: _startDate, + endDate: _recurrenceEndDate, + frequency: _selectedFrequency ?? RepeatFrequency.daily, + weekdays: _selectedIndexes, + interval: int.tryParse(_occurrenceController.text), + recurrenceEndOn: _selectedRecurrenceEnd ?? RecurrenceEnd.never, + ); + final event = CalendarEventData( date: _startDate, endTime: _endTime, @@ -293,12 +571,42 @@ class _AddOrEditEventFormState extends State { color: _color, title: _titleController.text.trim(), description: _descriptionController.text.trim(), + recurrenceSettings: recurrenceSettings, ); widget.onEventAdd?.call(event); _resetForm(); } + /// Get list of weekdays in indices from the selected days + List get _selectedIndexes { + List selectedIndexes = []; + for (int i = 0; i < _selectedDays.length; i++) { + if (_selectedDays[i] == true) { + selectedIndexes.add(i); + } + } + return selectedIndexes; + } + + void updateWeekdaysSelection() { + _selectedDays.fillRange(0, _selectedDays.length, false); + if (_endDate.difference(_startDate).inDays >= 7) { + _selectedDays.fillRange(0, _selectedDays.length, true); + } + DateTime current = _startDate; + while (current.isBefore(_endDate) || current.isAtSameMomentAs(_endDate)) { + _selectedDays[current.weekday - 1] = true; + current = current.add(Duration(days: 1)); + } + } + + /// Set initial selected week to start date + void _setInitialWeekday() { + final currentWeekday = DateTime.now().weekday - 1; + _selectedDays[currentWeekday] = true; + } + void _setDefaults() { if (widget.event == null) return; @@ -310,6 +618,12 @@ class _AddOrEditEventFormState extends State { _endTime = event.endTime ?? _endTime; _titleController.text = event.title; _descriptionController.text = event.description ?? ''; + _selectedFrequency = event.recurrenceSettings?.frequency; + _selectedRecurrenceEnd = + event.recurrenceSettings?.recurrenceEndOn ?? RecurrenceEnd.never; + _recurrenceEndDate = event.recurrenceSettings?.endDate ?? _startDate; + _occurrenceController.text = + (event.recurrenceSettings?.interval ?? 0).toString(); } void _resetForm() { diff --git a/example/lib/widgets/day_view_widget.dart b/example/lib/widgets/day_view_widget.dart index bc8cf3f8..a02c189f 100644 --- a/example/lib/widgets/day_view_widget.dart +++ b/example/lib/widgets/day_view_widget.dart @@ -38,6 +38,7 @@ class DayViewWidget extends StatelessWidget { MaterialPageRoute( builder: (_) => DetailsPage( event: events.first, + date: date, ), ), ); diff --git a/example/lib/widgets/delete_event_dialog.dart b/example/lib/widgets/delete_event_dialog.dart new file mode 100644 index 00000000..31c9b20f --- /dev/null +++ b/example/lib/widgets/delete_event_dialog.dart @@ -0,0 +1,63 @@ +import 'package:calendar_view/calendar_view.dart'; +import 'package:flutter/material.dart'; + +class DeleteEventDialog extends StatefulWidget { + @override + _RadioDialogState createState() => _RadioDialogState(); +} + +class _RadioDialogState extends State { + DeleteEvent _selectedOption = DeleteEvent.current; + + @override + Widget build(BuildContext context) { + return AlertDialog( + title: Text('Delete recurring event '), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + RadioListTile( + title: Text('This event'), + value: DeleteEvent.current, + groupValue: _selectedOption, + onChanged: (deleteType) { + if (deleteType != null) { + setState(() => _selectedOption = deleteType); + } + }, + ), + RadioListTile( + title: Text('This and following events'), + value: DeleteEvent.following, + groupValue: _selectedOption, + onChanged: (deleteType) { + if (deleteType != null) { + setState(() => _selectedOption = deleteType); + } + }, + ), + RadioListTile( + title: Text('All events'), + value: DeleteEvent.all, + groupValue: _selectedOption, + onChanged: (deleteType) { + if (deleteType != null) { + setState(() => _selectedOption = deleteType); + } + }, + ), + ], + ), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: Text('Cancel'), + ), + TextButton( + onPressed: () => Navigator.of(context).pop(_selectedOption), + child: Text('Done'), + ), + ], + ); + } +} diff --git a/example/lib/widgets/month_view_widget.dart b/example/lib/widgets/month_view_widget.dart index bacec7eb..bdaf73f8 100644 --- a/example/lib/widgets/month_view_widget.dart +++ b/example/lib/widgets/month_view_widget.dart @@ -24,6 +24,7 @@ class MonthViewWidget extends StatelessWidget { MaterialPageRoute( builder: (_) => DetailsPage( event: event, + date: date, ), ), ); diff --git a/example/lib/widgets/week_view_widget.dart b/example/lib/widgets/week_view_widget.dart index 765e8fcb..5c036911 100644 --- a/example/lib/widgets/week_view_widget.dart +++ b/example/lib/widgets/week_view_widget.dart @@ -33,6 +33,7 @@ class WeekViewWidget extends StatelessWidget { MaterialPageRoute( builder: (_) => DetailsPage( event: events.first, + date: date, ), ), ); diff --git a/lib/src/calendar_event_data.dart b/lib/src/calendar_event_data.dart index e6b053fd..45f60544 100644 --- a/lib/src/calendar_event_data.dart +++ b/lib/src/calendar_event_data.dart @@ -44,6 +44,9 @@ class CalendarEventData { /// Define style of description. final TextStyle? descriptionStyle; + /// Define reoccurrence settings + final RecurrenceSettings? recurrenceSettings; + /// {@macro calendar_event_data_doc} CalendarEventData({ required this.title, @@ -55,6 +58,7 @@ class CalendarEventData { this.endTime, this.titleStyle, this.descriptionStyle, + this.recurrenceSettings, DateTime? endDate, }) : _endDate = endDate?.withoutTime, date = date.withoutTime; @@ -119,6 +123,7 @@ class CalendarEventData { "title": title, "description": description, "endDate": endDate, + "recurrenceSettings": recurrenceSettings, }; /// Returns new object of [CalendarEventData] with the updated values defined @@ -135,6 +140,7 @@ class CalendarEventData { TextStyle? descriptionStyle, DateTime? endDate, DateTime? date, + RecurrenceSettings? recurrenceSettings, }) { return CalendarEventData( title: title ?? this.title, @@ -147,6 +153,7 @@ class CalendarEventData { endDate: endDate ?? this.endDate, event: event ?? this.event, titleStyle: titleStyle ?? this.titleStyle, + recurrenceSettings: recurrenceSettings ?? this.recurrenceSettings, ); } diff --git a/lib/src/enumerations.dart b/lib/src/enumerations.dart index 5659e4e9..b33c8d34 100644 --- a/lib/src/enumerations.dart +++ b/lib/src/enumerations.dart @@ -48,3 +48,38 @@ enum LineStyle { /// Dashed line dashed, } + +/// Defines reoccurrence of event: Daily, weekly, monthly or yearly +enum RepeatFrequency { + doNotRepeat, + daily, + weekly, + monthly, + yearly, +} + +/// Defines reoccurrence event ends on: +/// `never` to repeat without any end date specified, +/// `onDate` to repeat till date specified +/// `after` repeat till defined number of occurrence. +enum RecurrenceEnd { + never, + onDate, + after, +} + +/// Specifies the scope of deletion for recurring events in a calendar. +/// +/// This enum is used to determine which instances of a recurring event +/// should be deleted when a deletion action is performed. +/// +/// - [DeleteEvent.all] - Deletes all instances of the recurring event. +/// - [DeleteEvent.current] - Deletes only the currently selected instance +/// of the event. +/// - [DeleteEvent.following] - Deletes the current and all future instances +/// of the recurring event. +enum DeleteEvent { + all, + current, + following, +} diff --git a/lib/src/event_controller.dart b/lib/src/event_controller.dart index 1a131a95..2757efb0 100644 --- a/lib/src/event_controller.dart +++ b/lib/src/event_controller.dart @@ -6,9 +6,7 @@ import 'dart:collection'; import 'package:flutter/material.dart'; -import 'calendar_event_data.dart'; -import 'extensions.dart'; -import 'typedefs.dart'; +import '../calendar_view.dart'; class EventController extends ChangeNotifier { /// Calendar controller to control all the events related operations like, @@ -73,7 +71,217 @@ class EventController extends ChangeNotifier { //#endregion + //#region Private Methods + + /// Determines whether the given date should be included as a recurring event + /// for daily recurrence settings. + /// + /// Returns `true` if the event should repeat on the given `currentDate`, + /// otherwise returns `false`. + /// `endDate` may change, such as when handling deletions or updates. + /// + /// - If `recurrenceEndDate` is not specified, the event repeats indefinitely + /// and this method returns `true`. + /// - If `recurrenceEndDate` is specified: + /// - The event is included if the `currentDate` is before + /// the `recurrenceEndDate`. + /// - The event is also included on the exact `recurrenceEndDate` + /// (checked using `isAtSameMomentAs`), allowing the event to occur + /// on the last day. + bool _isDailyRecurrence({ + required DateTime currentDate, + required RecurrenceSettings recurrenceSettings, + }) { + final recurrenceEndDate = recurrenceSettings.endDate; + return recurrenceEndDate == null || + (currentDate.isBefore(recurrenceEndDate) || + currentDate.isAtSameMomentAs(recurrenceEndDate)); + } + + /// If the weekday matches with `recurrenceSettings` and there is no end date, + /// the recurrence is infinite + /// + /// + /// If the weekday matches and there is an end date, check if the current date + /// is before or on the end date + /// This ensures the recurrence continues until the specified end date + /// + /// Recurrence endDate may change if event is deleted. + bool _isWeeklyRecurrence({ + required DateTime currentDate, + required RecurrenceSettings recurrenceSettings, + }) { + // Adjust weekday to zero-based indexing and + // check if date’s weekday is in the recurrence weekdays + final isMatchingWeekday = + recurrenceSettings.weekdays.contains(currentDate.weekday - 1); + final recurrenceEndDate = recurrenceSettings.endDate; + + if (!isMatchingWeekday) { + return false; + } + + // If no end date is specified, repeat infinitely + return recurrenceEndDate == null || + (currentDate.isBefore(recurrenceEndDate) || + currentDate.isAtSameMomentAs(recurrenceEndDate)); + } + + // Repeat event on same day + // 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. + bool _isMonthlyRecurrence({ + required DateTime currentDate, + required DateTime startDate, + required RecurrenceSettings recurrenceSettings, + }) { + // Exclude if day is different + if (currentDate.day != startDate.day) { + return false; + } + + // Continues if day is same + final recurrenceEndDate = recurrenceSettings.endDate; + + return recurrenceEndDate == null || + (currentDate.isBefore(recurrenceEndDate) || + 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, + required RecurrenceSettings recurrenceSettings, + }) { + if (currentDate.month != startDate.month || + currentDate.day != startDate.day) { + return false; + } + + final recurrenceEndDate = recurrenceSettings.endDate; + return recurrenceEndDate == null || + (currentDate.isBefore(recurrenceEndDate) || + 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, + }) { + switch (recurrenceSettings.frequency) { + case RepeatFrequency.doNotRepeat: + break; + case RepeatFrequency.daily: + return _isDailyRecurrence( + currentDate: currentDate, + recurrenceSettings: recurrenceSettings, + ); + case RepeatFrequency.weekly: + return _isWeeklyRecurrence( + currentDate: currentDate, + recurrenceSettings: recurrenceSettings, + ); + case RepeatFrequency.monthly: + return _isMonthlyRecurrence( + currentDate: currentDate, + startDate: eventStartDate, + recurrenceSettings: recurrenceSettings, + ); + case RepeatFrequency.yearly: + return _isYearlyRecurrence( + currentDate: currentDate, + startDate: eventStartDate, + recurrenceSettings: recurrenceSettings); + } + return false; + } + + void _deleteCurrentEvent(DateTime date, CalendarEventData event) { + final excludeDates = event.recurrenceSettings?.excludeDates ?? []; + excludeDates.add(date); + final updatedRecurrenceSettings = + event.recurrenceSettings?.copyWith(excludeDates: excludeDates); + final updatedEvent = + event.copyWith(recurrenceSettings: updatedRecurrenceSettings); + update(event, updatedEvent); + } + + /// If the selected date to delete the event is the same as the event's start date, delete all recurrences. + /// Otherwise, delete the event on the selected date and all subsequent recurrences. + void _deleteFollowingEvents(DateTime date, CalendarEventData event) { + final newEndDate = date.subtract( + const Duration(days: 1), + ); + final updatedRecurrenceSettings = event.recurrenceSettings?.copyWith( + endDate: newEndDate, + ); + if (date == event.date) { + remove(event); + } else { + final updatedEvent = + event.copyWith(recurrenceSettings: updatedRecurrenceSettings); + update(event, updatedEvent); + } + } + //#endregion + //#region Public Methods + /// Deletes a recurring event based on the specified deletion type. + /// + /// This method handles the deletion of recurring events by determining the + /// type of deletion + /// requested (all events, the current event, or following events) and + /// performing the appropriate action. + /// + /// Takes the following parameters: + /// - [date]: The date of the event to be deleted. + /// - [event]: The event data to be deleted. + /// - [deleteEventType]: The `DeleteEventType` of deletion to perform + /// (all events, the current event, or following events). + /// + /// The method performs the following actions based on the [deleteEventType]: + /// - [DeleteEvent.all]: Removes the entire series of events. + /// - [DeleteEvent.current]: Deletes only the current event. + /// - [DeleteEvent.following]: Deletes the current event and + /// all subsequent events. + void deleteRecurrenceEvent({ + required DateTime date, + required CalendarEventData event, + required DeleteEvent deleteEventType, + }) { + switch (deleteEventType) { + case DeleteEvent.all: + remove(event); + break; + case DeleteEvent.current: + _deleteCurrentEvent(date, event); + break; + case DeleteEvent.following: + _deleteFollowingEvents(date, event); + break; + } + } + /// Add all the events in the list /// If there is an event with same date then void addAll(List> events) { @@ -137,11 +345,63 @@ class EventController extends ChangeNotifier { {bool includeFullDayEvents = true}) { //ignore: deprecated_member_use_from_same_package if (_eventFilter != null) return _eventFilter!.call(date, this.events); - return _calendarData.getEventsOnDay(date.withoutTime, includeFullDayEvents: includeFullDayEvents); } + /// Retrieves all events for a given date, including repeated events that are + /// not excluded on that day. + /// + /// This method combines events that occur on the specified date with repeated + /// events that are not excluded. + /// It filters out any events that are marked as excluded for the given date. + /// + /// 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> getAllEventsOnDay(DateTime date) { + final events = getEventsOnDay(date); + final repeatedEvents = getRepeatedEvents(date); + events.addAll(repeatedEvents); + return events; + } + + /// Returns repeated events on given date. + List> getRepeatedEvents(DateTime date) { + final repeatedEvents = _calendarData.repeatedEvents; + final 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) { + final recurrenceSettings = event.recurrenceSettings; + + if (recurrenceSettings == null || + date.isBefore(event.date) || + _isExcluded(recurrenceSettings, date)) { + continue; + } + + final isRecurrence = _handleRecurrence( + currentDate: date, + eventStartDate: event.date, + eventEndDate: event.endDate, + recurrenceSettings: recurrenceSettings, + ); + + // Add in calendar if event is recurring and + // 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); + } + } + return events; + } + /// Returns full day events on given day. List> getFullDayEvent(DateTime date) { return _calendarData.getFullDayEvent(date.withoutTime); @@ -179,6 +439,10 @@ class CalendarData { /// available in this list as global itemList of all events). final _eventList = >[]; + /// If recurrence settings exist then get all the repeated events + List> get repeatedEvents => + _eventList.where((event) => event.recurrenceSettings != null).toList(); + UnmodifiableListView> get events => UnmodifiableListView(_eventList); @@ -249,7 +513,6 @@ class CalendarData { // TODO: improve this... if (_eventList.contains(event)) return; - if (event.isFullDayEvent) { addFullDayEvent(event); } else if (event.isRangingEvent) { @@ -329,7 +592,6 @@ class CalendarData { if (includeFullDayEvents) { events.addAll(getFullDayEvent(date)); } - return events; } diff --git a/lib/src/modals.dart b/lib/src/modals.dart index 5fc80acb..2a0485ce 100644 --- a/lib/src/modals.dart +++ b/lib/src/modals.dart @@ -4,8 +4,7 @@ import 'package:flutter/material.dart'; -import 'enumerations.dart'; -import 'typedefs.dart'; +import '../calendar_view.dart'; /// Settings for hour lines class HourIndicatorSettings { @@ -83,3 +82,252 @@ class LiveTimeIndicatorSettings { showBullet: false, ); } + +/// Set `frequency = RepeatFrequency.daily` to repeat every day +/// after current date & event day. +/// Set `frequency = RepeatFrequency.weekly` & provide list of weekdays +/// to repeat on. +/// [startDate]: Defines start date of repeating events. +/// [endDate]: Defines end date of repeating events. +/// [interval]: Defines repetition of event after given [interval] in days. +/// [frequency]: Defines repeat daily, weekly, monthly or yearly. +/// [weekdays]: Contains list of weekdays to repeat starting from 0 index. +/// By default weekday of event is considered if not provided. +class RecurrenceSettings { + RecurrenceSettings({ + required this.startDate, + this.endDate, + this.interval, + this.frequency = RepeatFrequency.weekly, + this.recurrenceEndOn = RecurrenceEnd.never, + this.excludeDates, + List? weekdays, + }) : weekdays = weekdays ?? [startDate.weekday]; + + // Set by calculating end date for daily + // Ex. If current date is 11-11-24 and interval is 5 then new end date will be + // 15-11-24. Interval - 1 is added because event has been already occurred + // once on start date. + + // Set by calculating end date for weekly + // Ex. If current date is 1-11-24 and interval is 5 then new end date + // will be 29-11-24. + RecurrenceSettings.withCalculatedEndDate({ + required this.startDate, + required DateTime endDate, + this.interval, + this.frequency = RepeatFrequency.weekly, + this.recurrenceEndOn = RecurrenceEnd.never, + this.excludeDates, + List? weekdays, + }) : weekdays = weekdays ?? [startDate.weekday] { + this.endDate = _getEndDate(endDate); + } + + final DateTime startDate; + late DateTime? endDate; + final int? interval; + final RepeatFrequency frequency; + final RecurrenceEnd recurrenceEndOn; + final List weekdays; + final List? excludeDates; + + /// Calculates the end date for a monthly recurring event + /// based on the start date and the number of occurrences. + /// + /// If the next month does not have the event date and the recurrence + /// is still set to repeat for the given number of occurrences, + /// it will keep looking for a valid date in the following month. + /// + /// Example: If the start date is 29-01-25 and the recurrence ends + /// after 2 occurrences, + /// the end date will be 29-03-25 because February does not have a 29th date. + DateTime get _endDateMonthly { + var occurrences = (interval ?? 1) - 1; + var nextDate = startDate; + + while (occurrences > 0) { + nextDate = DateTime( + nextDate.year, + nextDate.month + 1, + nextDate.day, + ); + + // Adjust the date if the resulting month does not have the same day + // as the start date + // Example: DateTime(2024, 10 + 1, 31) gives 2024-12-01 + // TODO(Shubham): Review if in next consecutive month as well date does not found? + if (nextDate.day != startDate.day) { + nextDate = DateTime( + nextDate.year, + nextDate.month, + startDate.day, + ); + } + occurrences--; + } + return nextDate; + } + + /// Returns the calculated end date for the selected weekdays and occurrences, + /// or null if the conditions are not met. + /// + /// This method calculates the end date for a recurring event based + /// on the selected weekdays and the specified occurrences. + /// It iterates through the dates starting from the start date and + /// counts the occurrences of the selected weekdays + /// until the target occurrence is met. + /// + /// Example: If the start date is 12-11-24 (Tuesday), and the selected + /// weekdays are [Tuesday, Wednesday] for 3 occurrences, + /// the event will repeat on 12-11-24, 13-11-24, and 19-11-24. + /// + // TODO(Shubham): Update to remove current start day if it doesn't fall + // under selected weekdays + DateTime? get _endDateWeekly { + if (weekdays.isEmpty) { + return null; + } + + // Sort and normalize weekdays (0 = Sunday, 6 = Saturday) + final sortedWeekdays = weekdays.map((day) => (day + 1) % 7).toList() + ..sort(); + + final totalWeekdays = sortedWeekdays.length; + final targetOccurrence = interval ?? 1; + + // Calculate the number of full weeks and remaining occurrences + final fullWeeks = (targetOccurrence - 1) ~/ totalWeekdays; + final remainingOccurrences = (targetOccurrence - 1) % totalWeekdays; + + // Calculate the base date from full weeks + var resultDate = startDate.add(Duration(days: fullWeeks * 7)); + + // Find the weekday for the remaining occurrences + if (remainingOccurrences >= 0) { + final startWeekdayIndex = + sortedWeekdays.indexWhere((day) => day >= resultDate.weekday); + final weekdayIndex = startWeekdayIndex == -1 + ? 0 + remainingOccurrences + : (startWeekdayIndex + remainingOccurrences) % totalWeekdays; + + final targetWeekday = sortedWeekdays[weekdayIndex]; + final daysToAdd = (targetWeekday - resultDate.weekday + 7) % 7; + + resultDate = resultDate.add(Duration(days: daysToAdd)); + } + + 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) { + final newDate = DateTime( + nextDate.year + 1, + startDate.month, + startDate.day, + ); + + // 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; + } + + /// Determines the end date for a recurring event based on the + /// `RepeatFrequency` & `RecurrenceEnd`. + /// + /// Returns null if the end date is not applicable. + /// For example: An event that does not repeat and event that never ends. + DateTime? _getEndDate(DateTime endDate) { + if (frequency == RepeatFrequency.doNotRepeat || + recurrenceEndOn == RecurrenceEnd.never) { + return null; + } + + // TODO(Shubham): May not required to check all OR conditions verify that. + if (recurrenceEndOn == RecurrenceEnd.onDate && + (frequency == RepeatFrequency.daily || + frequency == RepeatFrequency.weekly || + frequency == RepeatFrequency.monthly || + frequency == RepeatFrequency.yearly)) { + return endDate; + } + + if (recurrenceEndOn == RecurrenceEnd.after) { + return _handleOccurrence(endDate); + } + return null; + } + + // Finds the end date to repeat and event for the given number of occurrences + DateTime? _handleOccurrence(DateTime endDate) { + final occurrence = interval ?? 1; + if (occurrence <= 1) { + return endDate; + } + switch (frequency) { + case RepeatFrequency.doNotRepeat: + return null; + case RepeatFrequency.daily: + return endDate.add(Duration(days: occurrence - 1)); + case RepeatFrequency.weekly: + return _endDateWeekly ?? endDate; + case RepeatFrequency.monthly: + return _endDateMonthly; + case RepeatFrequency.yearly: + return _endDateYearly; + } + } + + @override + String toString() { + return 'start date: $startDate, ' + 'end date: $endDate, ' + 'interval: $interval, ' + 'frequency: $frequency ' + 'weekdays: $weekdays' + 'recurrence Ends on: $recurrenceEndOn' + 'exclude dates: $excludeDates'; + } + + RecurrenceSettings copyWith({ + DateTime? startDate, + DateTime? endDate, + int? interval, + RepeatFrequency? frequency, + RecurrenceEnd? recurrenceEndOn, + List? weekdays, + List? excludeDates, + }) { + return RecurrenceSettings( + startDate: startDate ?? this.startDate, + endDate: endDate ?? this.endDate, + interval: interval ?? this.interval, + frequency: frequency ?? this.frequency, + recurrenceEndOn: recurrenceEndOn ?? this.recurrenceEndOn, + weekdays: weekdays ?? this.weekdays, + excludeDates: excludeDates ?? this.excludeDates, + ); + } +} diff --git a/lib/src/month_view/month_view.dart b/lib/src/month_view/month_view.dart index 51690910..1ead1029 100644 --- a/lib/src/month_view/month_view.dart +++ b/lib/src/month_view/month_view.dart @@ -698,7 +698,8 @@ class _MonthPageBuilder extends StatelessWidget { itemCount: 42, shrinkWrap: true, itemBuilder: (context, index) { - final events = controller.getEventsOnDay(monthDays[index]); + final events = controller.getAllEventsOnDay(monthDays[index]); + return GestureDetector( onTap: () => onCellTap?.call(events, monthDays[index]), onLongPress: () => onDateLongPress?.call(monthDays[index]),