RxUserImpl constructor

RxUserImpl(
  1. UserRepository _userRepository,
  2. UserDriftProvider _userLocal,
  3. DtoUser dto
)

Implementation

RxUserImpl(this._userRepository, this._userLocal, DtoUser dto)
  : user = Rx<User>(dto.value),
    lastSeen = Rx(dto.value.lastSeenAt) {
  // Start the [_lastSeenTimer] right away.
  _runLastSeenTimer();

  final ChatContactId? contactId = user.value.contacts.firstOrNull?.id;
  if (contactId != null) {
    final FutureOr<RxChatContact?> contactOrFuture = _userRepository
        .getContact
        ?.call(contactId);

    if (contactOrFuture is RxChatContact?) {
      contact.value = contactOrFuture;
    } else {
      contactOrFuture.then((v) => contact.value = v);
    }
  }

  // Re-run [_runLastSeenTimer], if [User.lastSeenAt] has been changed.
  PreciseDateTime? at = user.value.lastSeenAt;
  _worker = ever(user, (User user) async {
    if (at != user.lastSeenAt) {
      _runLastSeenTimer();
      at = user.lastSeenAt;
    }

    final ChatContactId? contactId = user.contacts.firstOrNull?.id;
    if (contact.value?.id != contactId) {
      if (contactId != null) {
        final FutureOr<RxChatContact?> contactOrFuture = _userRepository
            .getContact
            ?.call(contactId);

        if (contactOrFuture is RxChatContact?) {
          contact.value = contactOrFuture;
        } else {
          contact.value = await contactOrFuture;
        }
      } else {
        contact.value = null;
      }
    }
  });

  _localSubscription = _userLocal.watch(id).listen((e) {
    if (e != null) {
      user.value = e.value;
    }
  });
}