getConsecutives function

List<ConsecutiveOccurrence> getConsecutives({
  1. required List<Message> items,
  2. required Message check,
  3. int? amount,
})

Implementation

List<ConsecutiveOccurrence> getConsecutives(
    {required List<Message> items, required Message check, int? amount}) {
  int found = 0;
  List<ConsecutiveOccurrence> results = [];
  for (var index = 0; index < items.length; index++) {
    if (index > 0) {
      if (items[index].type == check.type &&
          items[index - 1].type == check.type &&
          items[index].author.userID == items[index - 1].author.userID &&
          isSameDay(items[index - 1].createdAt, items[index].createdAt) &&
          results.isNotEmpty) {
        found += 1;
        results[results.length - 1].updateEndIndex(index);
      } else if (found >= 0 && items[index].type == check.type) {
        found = 1;
        results.add(ConsecutiveOccurrence(startIndex: index));
      }
    }
  }
  results = results.where((result) {
    return (result.endIndex ?? -1) - result.startIndex + 1 >= (amount ?? 4);
  }).toList();
  return results;
}