leave method

Future leave()

Implementation

Future leave() async {
  try {
    final localAddress = getCachedWallet()!.address!;

    // update liveSpaceData by removing the current user
    if (localAddress == pCAIP10ToWallet(data.spaceCreator)) {
      // host
      data.liveSpaceData.host = AdminPeer();
    } else if (_room != null) {
      // speaker
      final speakers = <AdminPeer>[];
      for (var speaker in data.liveSpaceData.speakers) {
        if (speaker.address != localAddress) {
          speakers.add(speaker);
        }
      }
      data.liveSpaceData.speakers = speakers;
    } else {
      // listener
      final listeners = <ListenerPeer>[];
      for (var listener in data.liveSpaceData.listeners) {
        if (listener.address != localAddress) {
          listeners.add(listener);
        }
      }
      data.liveSpaceData.listeners = listeners;
    }

    // prepare messageContent of the user activity message
    String messageContent =
        _room != null ? CHAT.UA_SPEAKER_LEAVE : CHAT.UA_LISTENER_LEAVE;

    await _disconnectFromRoom();

    if (_playbackUrl != null) {
      _playbackUrl = null;
    }

    sendLiveSpaceData(
        messageType: MessageType.USER_ACTIVITY,
        updatedLiveSpaceData: data.liveSpaceData,
        content: messageContent,
        affectedAddresses: [localAddress],
        spaceId: data.spaceId);
    data = _initSpaceData;
  } catch (e) {
    log('leave error $e');
  }

  notifyListeners();
}