hangUp method

Future<bool> hangUp(
  1. BuildContext context, {
  2. bool showConfirmation = false,
  3. ZegoCallEndReason reason = ZegoCallEndReason.localHangUp,
})

This function is used to end the current call.

You can pass the context context for any necessary pop-ups or page transitions. By using the showConfirmation parameter, you can control whether to display a confirmation dialog to confirm ending the call.

if you want hangUp in minimize state, please call minimize.hangUp

Related APIs: ZegoUIKitPrebuiltCallEvents.onHangUpConfirmation ZegoUIKitPrebuiltCallEvents.onCallEnd

Implementation

Future<bool> hangUp(
  BuildContext context, {
  bool showConfirmation = false,
  ZegoCallEndReason reason = ZegoCallEndReason.localHangUp,
}) async {
  if (ZegoUIKit().getRoom().id.isEmpty) {
    ZegoLoggerService.logInfo(
      'hang up, not in call',
      tag: 'call',
      subTag: 'controller',
    );

    return false;
  }

  if (null == private.prebuiltConfig) {
    ZegoLoggerService.logInfo(
      'hang up, config is null',
      tag: 'call',
      subTag: 'controller',
    );

    return false;
  }

  if (private.isHangUpRequestingNotifier.value) {
    ZegoLoggerService.logInfo(
      'hang up, is hang up requesting...',
      tag: 'call',
      subTag: 'controller',
    );

    return false;
  }

  ZegoLoggerService.logInfo(
    'hang up, show confirmation:$showConfirmation',
    tag: 'call',
    subTag: 'controller',
  );

  if (showConfirmation) {
    private.isHangUpRequestingNotifier.value = true;

    ///  if there is a user-defined event before the click,
    ///  wait the synchronize execution result
    final hangUpConfirmationEvent = ZegoCallHangUpConfirmationEvent(
      context: context,
    );
    defaultAction() async {
      return private.defaultHangUpConfirmationAction(
        hangUpConfirmationEvent,
        context,
      );
    }

    var canHangUp = true;
    if (private.events?.onHangUpConfirmation != null) {
      canHangUp = await private.events?.onHangUpConfirmation?.call(
            hangUpConfirmationEvent,
            defaultAction,
          ) ??
          true;
    } else {
      canHangUp = await defaultAction.call();
    }
    if (!canHangUp) {
      ZegoLoggerService.logInfo(
        'hang up, reject',
        tag: 'call',
        subTag: 'controller',
      );

      private.isHangUpRequestingNotifier.value = false;

      return false;
    }
  }

  ZegoLoggerService.logInfo(
    'hang up, restore mini state by hang up',
    tag: 'call',
    subTag: 'controller',
  );
  minimize.hide();

  pip.cancelBackground();
  private.uninitByPrebuilt();
  user.private.uninitByPrebuilt();
  audioVideo.private.uninitByPrebuilt();
  minimize.private.uninitByPrebuilt();
  permission.private.uninitByPrebuilt();
  pip.private.uninitByPrebuilt();

  final result = await ZegoUIKit().leaveRoom().then((result) {
    ZegoLoggerService.logInfo(
      'hang up, leave room result, ${result.errorCode} ${result.extendedData}',
      tag: 'call',
      subTag: 'controller',
    );
    return 0 == result.errorCode;
  });

  if (ZegoPluginAdapter().getPlugin(ZegoUIKitPluginType.beauty) != null) {
    ZegoUIKit().getBeautyPlugin().uninit();
  }

  ZegoCallKitBackgroundService().setWaitCallPageDisposeFlag(false);

  await ZegoUIKitPrebuiltCallInvitationService().private.clearInvitation();

  final endEvent = ZegoCallEndEvent(
    callID: ZegoUIKit().getRoom().id,
    reason: reason,
    isFromMinimizing:
        ZegoCallMiniOverlayPageState.minimizing == minimize.state,
    invitationData: ZegoUIKitPrebuiltCallInvitationService()
        .private
        .currentCallInvitationData,
  );
  defaultAction() {
    private.defaultEndEvent(endEvent, context);
  }

  if (private.events?.onCallEnd != null) {
    private.events?.onCallEnd?.call(endEvent, defaultAction);
  } else {
    defaultAction.call();
  }

  ZegoLoggerService.logInfo(
    'hang up, finished',
    tag: 'call',
    subTag: 'controller',
  );

  return result;
}