init method

Future<void> init()

Initializes this RxChatImpl.

Implementation

Future<void> init() async {
  Log.debug('init()', '$runtimeType($id)');

  if (status.value.isSuccess) {
    return Future.value();
  }

  status.value = RxStatus.loading();

  reads.addAll(
    chat.value.lastReads.map((e) => LastChatRead(e.memberId, e.at)),
  );

  _initLocalSubscription();
  _initDraftSubscription();
  _initMessagesPagination();
  _initMembersPagination();

  _updateFields();

  if (chat.value.isDialog) {
    _updateAvatar();
    _membersPaginationSubscription = members.items.changes.listen((e) async {
      _updateAvatar();
    });
  }

  Chat previous = chat.value;
  _worker = ever(chat, (_) {
    _updateFields(previous: previous);
    previous = chat.value;
  });

  _messagesSubscription = messages.changes.listen((e) {
    switch (e.op) {
      case OperationKind.removed:
        _recalculateReadsFor(e.element.value);
        break;

      case OperationKind.added:
      case OperationKind.updated:
        // No-op.
        break;
    }
  });

  _callSubscription = StreamGroup.mergeBroadcast([
    _chatRepository.calls.changes,
    WebUtils.onStorageChange,
  ]).listen((_) {
    inCall.value =
        _chatRepository.calls[id] != null || WebUtils.containsCall(id);
  });

  await _draftGuard.protect(() async {
    draft.value = await _draftLocal.read(id);
  });
}