From c16ba5af2bfcf14890ea9ef98363afdcaedbe736 Mon Sep 17 00:00:00 2001 From: Brett Sutton Date: Fri, 3 Jan 2025 09:09:33 +1100 Subject: [PATCH 1/2] Added 'clear' method to EventController. --- README.md | 23 ++++++++++++----------- lib/src/event_controller.dart | 14 ++++++++++++++ test/src/event_controller_test.dart | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 test/src/event_controller_test.dart diff --git a/README.md b/README.md index 7448455f..4984a0ee 100644 --- a/README.md +++ b/README.md @@ -255,17 +255,18 @@ assigned. Methods provided by `EventController` -| Name | Parameters | Description | -|-----------------|--------------------------------------------------------------|-----------------------------------------------------------| -| add | CalendarEventData\ event | Adds one event in controller and rebuilds view. | -| addAll | List\\> events | Adds list of events in controller and rebuilds view. | -| remove | CalendarEventData\ event | Removes an event from controller and rebuilds view. | -| removeAll | List\\> events | Removes all event defined in the list | -| removeWhere | TestPredicate\\> test | Removes all events for which test returns true. | -| update | CalendarEventData\ event, CalendarEventData\ updated | Updates event with updated event. | -| getFullDayEvent | DateTime date | Returns the list of full day events stored in controller | -| updateFilter | EventFilter\ newFilter | Updates the event filter of the controller. | -| getEventsOnDay | DateTime date | Returns list of events on `date` | +| Name | Parameters | Description | +|-----------------|--------------------------------------------------------------|-------------------------------------------------------------| +| add | CalendarEventData\ event | Adds one event in controller and rebuilds view. | +| addAll | List\\> events | Adds list of events in controller and rebuilds view. | +| remove | CalendarEventData\ event | Removes an event from controller and rebuilds view. | +| removeAll | List\\> events | Removes all event defined in the list and rebuilds the view | +| clear | | Removes events from the controller and rebuilds the view | +| removeWhere | TestPredicate\\> test | Removes all events for which test returns true. | +| update | CalendarEventData\ event, CalendarEventData\ updated | Updates event with updated event. | +| getFullDayEvent | DateTime date | Returns the list of full day events stored in controller | +| updateFilter | EventFilter\ newFilter | Updates the event filter of the controller. | +| getEventsOnDay | DateTime date | Returns list of events on `date` | Check [documentation](https://pub.dev/documentation/calendar_view/latest/calendar_view/EventController-class.html) for more info. diff --git a/lib/src/event_controller.dart b/lib/src/event_controller.dart index 1a131a95..d6e28239 100644 --- a/lib/src/event_controller.dart +++ b/lib/src/event_controller.dart @@ -113,6 +113,12 @@ class EventController extends ChangeNotifier { notifyListeners(); } + void clear() { + _calendarData.clear(); + + notifyListeners(); + } + /// Removes multiple [event] from this controller. void removeWhere(TestPredicate> test) { _calendarData.removeWhere(test); @@ -344,5 +350,13 @@ class CalendarData { } return events; } + + /// Remove all events from the controller. + void clear() { + _fullDayEventList.clear(); + _rangingEventList.clear(); + _singleDayEvents.clear(); + _eventList.clear(); + } //#endregion } diff --git a/test/src/event_controller_test.dart b/test/src/event_controller_test.dart new file mode 100644 index 00000000..91ce5471 --- /dev/null +++ b/test/src/event_controller_test.dart @@ -0,0 +1,25 @@ +import 'package:calendar_view/calendar_view.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('event controller ...', (tester) async { + final controller = EventController(); + + final now = DateTime.now(); + controller.add(CalendarEventData( + title: 'none', + date: now, + startTime: now, + endTime: now.add(Duration(hours: 1)))); + controller.add(CalendarEventData( + title: 'All Day', + date: DateTime.now().withoutTime, + )); + + expect(controller.getFullDayEvent(now).length, equals(1)); + expect(controller.getEventsOnDay(now).length, equals(2)); + expect(controller.allEvents.length, equals(2)); + controller.clear(); + expect(controller.allEvents.length, equals(0)); + }); +} From 58d7d91d60e4fc6013654deb0ec825cfd6f02d12 Mon Sep 17 00:00:00 2001 From: Brett Sutton Date: Tue, 7 Jan 2025 09:34:13 +1100 Subject: [PATCH 2/2] add _recurringEventsList to the list of lists being cleared. --- lib/src/event_controller.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/src/event_controller.dart b/lib/src/event_controller.dart index fec471cc..f67c4b3a 100644 --- a/lib/src/event_controller.dart +++ b/lib/src/event_controller.dart @@ -628,6 +628,7 @@ class CalendarData { _rangingEventList.clear(); _singleDayEvents.clear(); _eventList.clear(); + _recurringEventsList.clear(); } //#endregion }