updateChannelState method

void updateChannelState(
  1. ChannelState updatedState
)

Update channelState with updated information.

Implementation

void updateChannelState(ChannelState updatedState) {
  final _existingStateMessages = [...messages];
  final newMessages = <Message>[
    ..._existingStateMessages.merge(updatedState.messages),
  ].sorted(_sortByCreatedAt);

  final _existingStateWatchers = _channelState.watchers ?? [];
  final _updatedStateWatchers = updatedState.watchers ?? [];
  final newWatchers = <User>[
    ..._updatedStateWatchers,
    ..._existingStateWatchers
        .where((w) =>
            !_updatedStateWatchers.any((newWatcher) => newWatcher.id == w.id))
        .toList(),
  ];

  final newMembers = <Member>[
    ...updatedState.members ?? [],
  ];

  final _existingStateRead = _channelState.read ?? [];
  final _updatedStateRead = updatedState.read ?? [];
  final newReads = <Read>[
    ..._updatedStateRead,
    ..._existingStateRead
        .where((r) =>
            !_updatedStateRead.any((newRead) => newRead.user.id == r.user.id))
        .toList(),
  ];

  _checkExpiredAttachmentMessages(updatedState);

  _channelState = _channelState.copyWith(
    messages: newMessages,
    channel: _channelState.channel?.merge(updatedState.channel),
    watchers: newWatchers,
    watcherCount: updatedState.watcherCount,
    members: newMembers,
    read: newReads,
    pinnedMessages: updatedState.pinnedMessages,
  );
}