Skip to content

Commit

Permalink
Merge pull request #445 from bsutton/master
Browse files Browse the repository at this point in the history
feat: Added 'clear' method to EventController.
  • Loading branch information
PRBaraiya authored Jan 7, 2025
2 parents b814e1f + 58d7d91 commit 56a65c0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,18 @@ assigned.

Methods provided by `EventController`

| Name | Parameters | Description |
|-----------------|--------------------------------------------------------------|-----------------------------------------------------------|
| add | CalendarEventData\<T\> event | Adds one event in controller and rebuilds view. |
| addAll | List\<CalendarEventData\<T\>\> events | Adds list of events in controller and rebuilds view. |
| remove | CalendarEventData\<T\> event | Removes an event from controller and rebuilds view. |
| removeAll | List\<CalendarEventData\<T\>\> events | Removes all event defined in the list |
| removeWhere | TestPredicate\<CalendarEventData\<T\>\> test | Removes all events for which test returns true. |
| update | CalendarEventData\<T\> event, CalendarEventData\<T\> updated | Updates event with updated event. |
| getFullDayEvent | DateTime date | Returns the list of full day events stored in controller |
| updateFilter | EventFilter\<T\> newFilter | Updates the event filter of the controller. |
| getEventsOnDay | DateTime date | Returns list of events on `date` |
| Name | Parameters | Description |
|-----------------|--------------------------------------------------------------|-------------------------------------------------------------|
| add | CalendarEventData\<T\> event | Adds one event in controller and rebuilds view. |
| addAll | List\<CalendarEventData\<T\>\> events | Adds list of events in controller and rebuilds view. |
| remove | CalendarEventData\<T\> event | Removes an event from controller and rebuilds view. |
| removeAll | List\<CalendarEventData\<T\>\> events | Removes all event defined in the list and rebuilds the view |
| clear | | Removes events from the controller and rebuilds the view |
| removeWhere | TestPredicate\<CalendarEventData\<T\>\> test | Removes all events for which test returns true. |
| update | CalendarEventData\<T\> event, CalendarEventData\<T\> updated | Updates event with updated event. |
| getFullDayEvent | DateTime date | Returns the list of full day events stored in controller |
| updateFilter | EventFilter\<T\> 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.

Expand Down
15 changes: 15 additions & 0 deletions lib/src/event_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ class EventController<T extends Object?> extends ChangeNotifier {
notifyListeners();
}

void clear() {
_calendarData.clear();

notifyListeners();
}

/// Removes multiple [event] from this controller.
void removeWhere(TestPredicate<CalendarEventData<T>> test) {
_calendarData.removeWhere(test);
Expand Down Expand Up @@ -615,5 +621,14 @@ class CalendarData<T extends Object?> {
events.addAll(recurringEvents);
return events;
}

/// Remove all events from the controller.
void clear() {
_fullDayEventList.clear();
_rangingEventList.clear();
_singleDayEvents.clear();
_eventList.clear();
_recurringEventsList.clear();
}
//#endregion
}
25 changes: 25 additions & 0 deletions test/src/event_controller_test.dart
Original file line number Diff line number Diff line change
@@ -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<String>(
title: 'none',
date: now,
startTime: now,
endTime: now.add(Duration(hours: 1))));
controller.add(CalendarEventData<String>(
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));
});
}

0 comments on commit 56a65c0

Please sign in to comment.