Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added 'clear' method to EventController. #445

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
bsutton marked this conversation as resolved.
Show resolved Hide resolved
_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));
});
}
Loading