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 e5b73d16..f67c4b3a 100644 --- a/lib/src/event_controller.dart +++ b/lib/src/event_controller.dart @@ -147,6 +147,12 @@ class EventController extends ChangeNotifier { notifyListeners(); } + void clear() { + _calendarData.clear(); + + notifyListeners(); + } + /// Removes multiple [event] from this controller. void removeWhere(TestPredicate> test) { _calendarData.removeWhere(test); @@ -615,5 +621,14 @@ class CalendarData { events.addAll(recurringEvents); return events; } + + /// Remove all events from the controller. + void clear() { + _fullDayEventList.clear(); + _rangingEventList.clear(); + _singleDayEvents.clear(); + _eventList.clear(); + _recurringEventsList.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)); + }); +}