canAddChannel method

bool canAddChannel(
  1. CollectionEventSource eventSource,
  2. GroupChannel addedChannel
)

Implementation

bool canAddChannel(
  CollectionEventSource eventSource,
  GroupChannel addedChannel,
) {
  if (eventSource == CollectionEventSource.channelLoadMore) {
    return true;
  }

  if (channelList.isEmpty) {
    return false;
  }

  if (channelList.isNotEmpty && hasMore) {
    final a = channelList.last;
    final b = addedChannel;
    switch (_query.order) {
      case GroupChannelListQueryOrder.chronological:
        if (a.createdAt != null && b.createdAt != null) {
          if (b.createdAt!.compareTo(a.createdAt!) == -1) {
            // a.createdAt > b.createdAt
            return false;
          }
        }
        break;

      case GroupChannelListQueryOrder.latestLastMessage:
        if (a.lastMessage != null && b.lastMessage == null) {
          // a < b
          return false;
        } else if (a.lastMessage == null && b.lastMessage != null) {
        } else if (a.lastMessage != null && b.lastMessage != null) {
          if (b.lastMessage!.createdAt.compareTo(a.lastMessage!.createdAt) ==
              -1) {
            // a.lastMessage!.createdAt > b.lastMessage!.createdAt
            return false;
          }
        } else {
          if (a.createdAt != null && b.createdAt != null) {
            if (b.createdAt!.compareTo(a.createdAt!) == -1) {
              // a.createdAt > b.createdAt
              return false;
            }
          }
        }
        break;

      case GroupChannelListQueryOrder.channelNameAlphabetical:
        if (a.name.compareTo(b.name) == -1) {
          // a.name < b.name
          return false;
        }
        break;

      case GroupChannelListQueryOrder.metadataValueAlphabetical:
        // TODO: (?)
        break;
    }
  }
  return true;
}