getCalendarEntries method Null safety
Retrieve all calendar entries between two dates. states==null || states.len==0, will retrieve all calendar entries independing of the invitation status of the user states!=null && states.len!=0, only will retrieve calendar entries which their invitation status for the user match with any of the target statuses
Implementation
Future<BuiltList<CalendarEntry>> getCalendarEntries({
required final Instant fromDate,
required final Instant endDate,
required final int limit,
required final String userId,
}) async {
final gqlQueryString = '''
query FetchAllInvitations {
existingInvitationsForUser(fromDate: "${fromDate.toString()}", endDate: "${endDate.toString()}", limit: $limit) {
${InvitationGqlEntity.gqlQueryFragment}
}
}
''';
try {
final queryResult = await graphQLClient.query<void>(QueryOptions(
document: gql(gqlQueryString), fetchPolicy: FetchPolicy.networkOnly));
if (queryResult.hasException) {
logger?.e(queryResult.exception.toString());
return BuiltList<CalendarEntry>();
}
final data =
queryResult.data!['existingInvitationsForUser'] as List<dynamic>;
// translate the incoming JSON objects into calendar entries
final calendarEntries = data
.map((dynamic untypedElement) =>
untypedElement as Map<String, dynamic>)
.map((jsonElement) => CalendarEntryGqlEntity.fromJson(
jsonElement['calendarEntry'] as Map<String, dynamic>))
.whereNotNull()
.map((gqlEntity) => gqlEntity.toCalendarEntry())
.toBuiltList();
// NOTE: This piece of code is only a temporary thing. Right now the
// app expects the invitation message in the description field of the
// calendar entry, even though the description field has a different
// purpose. The lines below move the invitation message into the
// calendar entry's description field. When the app is changed, the line
// below needs to be removed.
final updatedEntries = calendarEntries
.map((entry) => entry.rebuild((builder) => builder
..description = entry.invitations
.firstWhere(
(invitation) => invitation.participant.userId == userId)
.message))
.toBuiltList();
if (!_calendarStreamController.isClosed) {
_calendarStreamController
.add(updatedEntries.map((p0) => EntityRead(p0)).toBuiltList());
}
return updatedEntries;
} catch (e, stackTrace) {
logger?.e('Could not read calendar', e.toString(), stackTrace);
return BuiltList<CalendarEntry>();
}
}