setMsgReadRecv method

void setMsgReadRecv(
  1. String contactName,
  2. String what,
  3. int seq,
  4. DateTime? ts,
)

Update a cached contact with new read/received/message count

Implementation

void setMsgReadRecv(String contactName, String what, int seq, DateTime? ts) {
  var cont = _contacts[contactName];
  var oldVal, doUpdate = false;

  if (cont != null) {
    this.seq = seq;
    cont.seq = cont.seq ?? 0;
    cont.read = cont.read ?? 0;
    cont.recv = cont.recv ?? 0;
    switch (what) {
      case 'recv':
        oldVal = cont.recv;
        cont.recv = max(cont.recv ?? 0, seq);
        doUpdate = (oldVal != cont.recv);
        break;
      case 'read':
        oldVal = cont.read;
        cont.read = max(cont.read ?? 0, seq);
        doUpdate = (oldVal != cont.read);
        break;
      case 'msg':
        oldVal = cont.seq;
        cont.seq = max(cont.seq ?? 0, seq);
        if (cont.touched == null ||
            (ts != null && cont.touched!.isBefore(ts))) {
          cont.touched = ts;
        }
        doUpdate = (oldVal != cont.seq);
        break;
    }

    // Sanity checks.
    if ((cont.recv ?? 0) < (cont.read ?? 0)) {
      cont.recv = cont.read;
      doUpdate = true;
    }
    if ((cont.seq ?? 0) < (cont.recv ?? 0)) {
      cont.seq = cont.recv;
      if (cont.touched == null ||
          (ts != null && cont.touched!.isBefore(ts))) {
        cont.touched = ts;
      }
      doUpdate = true;
    }
    cont.unread = (cont.seq ?? 0) - (cont.read ?? 0);

    if (doUpdate && (cont.acs == null || !cont.acs!.isMuted(null))) {
      onContactUpdate.add(ContactUpdateEvent(what, cont));
    }
  }
}