getMessages method

  1. @override
Future<List<Message>> getMessages({
  1. DateTime? start,
  2. DateTime? end,
  3. int? count = 20,
})
override

Get a list of messages in a given time period from start to end with a maximum of count messages.

If start is null, all messages back in time is included. If end is null, all messages up to now is included.

Note that the list is not sorted in any way. Sorting - e.g., by date - must be handled by the app if needed.

Implementation

@override
Future<List<Message>> getMessages({
  DateTime? start,
  DateTime? end,
  int? count = 20,
}) async {
  _assertCarpService();
  start ??=
      DateTime.fromMillisecondsSinceEpoch(0); // this is a looooooong time ago
  end ??= DateTime.now();

  // TODO - The query interface does not work - change back when issue is fixed
  // // first get the collection ID for the messages, if not know already
  // if (_messagesCollectionId == null) {
  //   _messagesCollectionId =
  //       (await CarpService().collection(MESSAGES_PATH).get()).id;
  // }
  // info('Getting messages from CARP server. '
  //     'study_id: ${CarpService().app?.studyId}, '
  //     'query: ${_getMessagesQuery(start, end)}');
  // List<DocumentSnapshot> messages =
  //     await CarpService().documentsByQuery(_getMessagesQuery(start, end));

  List<DocumentSnapshot> messages =
      await CarpService().collection(MESSAGES_PATH).documents;

  info('Messages downloaded - # : ${messages.length}');

  return messages
      .map((message) => Message.fromJson(message.data))
      .toList()
      .sublist(0, (count! < messages.length) ? count : messages.length);
}