Skip to content

Commit

Permalink
Merge pull request #2291 from acterglobal/ben-better-cal-sync-event-d…
Browse files Browse the repository at this point in the history
…etails

Additional test and fix for event data updates
  • Loading branch information
gnunicorn authored Oct 18, 2024
2 parents cba5b22 + 6655141 commit 813177e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
1 change: 1 addition & 0 deletions .changes/2291-calendar-sync-event-details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Labs] [Fix] Only add reminders on the device calendar for events that the user RSVP'ed maybe or yes, too. Keep the others quiet.
35 changes: 14 additions & 21 deletions app/lib/features/calendar_sync/calendar_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Future<void> _refreshCalendar(
foundEventIds.add(localEvent.eventId);
}

localEvent = await _updateEventDetails(calEvent, rsvp, localEvent);
localEvent = await updateEventDetails(calEvent, rsvp, localEvent);
final localRequest = await deviceCalendar.createOrUpdateEvent(localEvent);
if (localRequest == null) {
_log.severe('Updating $calEventId failed. No response. skipping');
Expand Down Expand Up @@ -236,14 +236,14 @@ Future<void> _refreshCalendar(
}
}

Future<Event> _updateEventDetails(
@visibleForTesting
Future<Event> updateEventDetails(
CalendarEvent acterEvent,
RsvpStatusTag? rsvp,
Event localEvent,
) async {
localEvent.title = acterEvent.title();
localEvent.description = acterEvent.description()?.body();
localEvent.reminders = [Reminder(minutes: 10)];
localEvent.start = TZDateTime.from(
toDartDatetime(acterEvent.utcStart()),
UTC,
Expand All @@ -252,11 +252,15 @@ Future<Event> _updateEventDetails(
toDartDatetime(acterEvent.utcEnd()),
UTC,
);
localEvent.status = switch (rsvp) {
RsvpStatusTag.Yes => EventStatus.Confirmed,
RsvpStatusTag.Maybe => EventStatus.Tentative,
_ => EventStatus.None
final (status, reminders) = switch (rsvp) {
RsvpStatusTag.Yes => (EventStatus.Confirmed, [Reminder(minutes: 10)]),
RsvpStatusTag.Maybe => (EventStatus.Tentative, [Reminder(minutes: 10)]),
RsvpStatusTag.No => (EventStatus.Canceled, null),
null => (EventStatus.None, null),
};

localEvent.status = status;
localEvent.reminders = reminders;
return localEvent;
}

Expand All @@ -269,21 +273,10 @@ Future<List<String>> _findActerCalendars() async {
if (calendars == null) {
return [];
}
if (Platform.isAndroid) {
return calendars
.where(
(c) =>
c.accountType == 'LOCAL' &&
c.accountName == 'Acter' &&
c.name == 'Acter',
)
.map((c) {
_log.info('Found ${c.id} (${c.accountType})');
return c.id!;
}).toList();
}
return calendars
.where((c) => c.accountType == 'Local' && c.name == 'Acter')
.where(
(c) => c.accountType?.toLowerCase() == 'local' && c.name == 'Acter',
)
.map((c) {
_log.info('Found ${c.id} (${c.accountType})');
return c.id!;
Expand Down
42 changes: 42 additions & 0 deletions app/test/features/calendar_sync/calendar_sync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,46 @@ void main() {
);
});
});
group('Calendar Event Updater tests', () {
testWidgets('basics update fine', (tester) async {
final events = generateMockCalendarEvents(10);

final event = Event('1');
for (final mock in events) {
await updateEventDetails(mock, null, event);
// ensuring the general stuff worked
expect(event.title, mock.title());
expect(event.description, mock.description()?.body());
// todo: check the start and end time ...?!?
}
});
testWidgets('status and reminders are set correctly', (tester) async {
final mockEvent = generateMockCalendarEvents(1).first;
final targetEvent = Event('1');

// yes add reminder
await updateEventDetails(mockEvent, RsvpStatusTag.Yes, targetEvent);
expect(targetEvent.status, EventStatus.Confirmed);
expect(targetEvent.reminders?.length, 1);
expect(targetEvent.reminders?.first.minutes, 10);

// maybe remove timer
await updateEventDetails(mockEvent, RsvpStatusTag.No, targetEvent);
expect(targetEvent.status, EventStatus.Canceled);
expect(targetEvent.reminders, null);

targetEvent.reminders = [Reminder(minutes: 30)];
// not responded removes timer
await updateEventDetails(mockEvent, null, targetEvent);
expect(targetEvent.status, EventStatus.None);
expect(targetEvent.reminders, null);

targetEvent.reminders = [Reminder(minutes: 30)];
// maybe adds timer
await updateEventDetails(mockEvent, RsvpStatusTag.Maybe, targetEvent);
expect(targetEvent.status, EventStatus.Tentative);
expect(targetEvent.reminders?.length, 1);
expect(targetEvent.reminders?.first.minutes, 10);
});
});
}

0 comments on commit 813177e

Please sign in to comment.