notifyList method

Future<List<AtNotification>> notifyList({
  1. String? fromDate,
})

Returns list of latest notifications of followers with update operation. Returns null if such notifications are not present.

Implementation

Future<List<AtNotification>> notifyList({String? fromDate}) async {
  int fromDateTime = 0;

  if (fromDate != null) {
    fromDateTime = DateTime.parse(fromDate).millisecondsSinceEpoch;
    fromDate = fromDate.split(' ')[0];
  }
  var response = await AtClientManager.getInstance().atClient.notifyList(
      regex:
          ('${AppConstants.containsFollowing}|${AppConstants.containsFollowers}'),
      fromDate: fromDate);
  response = response.toString().replaceAll('data:', '');
  if (response == 'null') {
    return [];
  }
  List<AtNotification> notificationList = AtNotification.fromJsonList(
      List<Map<String, dynamic>>.from(jsonDecode(response)));

  notificationList
      .retainWhere((notification) => notification.epochMillis > fromDateTime);
  notificationList.sort((notification1, notification2) =>
      notification2.epochMillis.compareTo(notification1.epochMillis));
  Set<AtNotification> uniqueNotifications = {};
  Set<String> uniqueKeys = {};
  for (var notification in notificationList) {
    bool isUnique = uniqueKeys.add(notification.from + notification.key);
    if (isUnique) {
      uniqueNotifications.add(notification);
    }
  }
  return uniqueNotifications.toList();
}