setForeground method

void setForeground(
  1. bool foreground
)

The app moved to the foreground or background.

Implementation

void setForeground(bool foreground) {
  if (_state.foreground == foreground) return;
  _emit(_state.copyWith(foreground: foreground));
  _backgroundTimer?.cancel();
  _backgroundTimer = null;
  if (foreground) {
    final socket = _socket;
    if (socket == null) {
      if (_started && _state.phase != ChatPhase.outdated) unawaited(_bootstrapAndConnect());
    } else if (!socket.active) {
      socket.connect();
    } else {
      _send(ClientFrame.appState(foreground: true, conversationId: _state.visible ? _state.conversation?.id : null));
      final conv = _state.conversation;
      if (_state.visible && conv != null && _state.timeline.lastSeq > 0) {
        _send(ClientFrame.read(conversationId: conv.id, upToSeq: _state.timeline.lastSeq));
      }
    }
  } else {
    _send(ClientFrame.appState(foreground: false));
    _stopTyping();
    _backgroundTimer = Timer(backgroundCloseDelay, () {
      _backgroundTimer = null;
      if (!_state.foreground) {
        _socket?.close();
        _emit(_state.copyWith(connection: HatangConnectionState.disconnected));
      }
    });
  }
}