showAsyncProgressKDialog<T> function

Future<T?> showAsyncProgressKDialog<T>(
  1. BuildContext context, {
  2. required Future<T> doProcess(),
  3. void onSuccess(
    1. T value
    )?,
  4. void onError(
    1. String errMessage
    )?,
  5. bool retryable = false,
  6. bool confirmationRequired = false,
  7. String? confirmationTitle,
  8. String? confirmationMessage,
  9. bool showSuccessSnackBar = false,
  10. String? successMessage,
  11. String? errorAcceptText,
  12. String? errorRetryText,
  13. String? loadingMessage,
  14. String? bottomErrorAlertTitle,
})

Implementation

Future<T?> showAsyncProgressKDialog<T>(
  BuildContext context, {
  required Future<T> Function() doProcess,
  void Function(T value)? onSuccess,
  void Function(String errMessage)? onError,
  bool retryable = false,
  bool confirmationRequired = false,
  String? confirmationTitle,
  String? confirmationMessage,
  bool showSuccessSnackBar = false,
  String? successMessage,
  String? errorAcceptText,
  String? errorRetryText,
  String? loadingMessage,
  String? bottomErrorAlertTitle,
}) async {
  confirmationMessage ??= strings.confirmationMessage;
  errorAcceptText ??= strings.acceptButtonText;
  errorRetryText ??= strings.errorRetryText;

  if (confirmationRequired) {
    final confirmed = await showConfirmationKDialog(
      context,
      title: confirmationTitle,
      message: confirmationMessage,
    );
    if (!confirmed) return null;
  }
  if (context.mounted) {
    void Function() closeloader;

    if (loadingMessage == null) {
      closeloader = await showKDialogWithLoadingIndicator(context);
    } else {
      closeloader =
          await showKDialogWithLoadingMessage(context, message: loadingMessage);
    }
    T? results;
    try {
      results = await doProcess();
      closeloader();
      if (onSuccess != null && results != null) onSuccess(results);

      if (context.mounted && (showSuccessSnackBar || successMessage != null)) {
        ScaffoldMessenger.of(context)
            .showSnackBar(_snackBar(message: successMessage));
      }
    } catch (err) {
      closeloader();

      bool? retry;
      if (context.mounted) {
        retry = await showBottomAlertKDialog(
          title: bottomErrorAlertTitle,
          context,
          message: err.toString(),
          retryable: retryable,
          acceptText: errorAcceptText,
          retryText: errorRetryText,
        );
      }

      if ((retry ?? false) && context.mounted) {
        return await showAsyncProgressKDialog(
          context,
          doProcess: doProcess,
          onError: onError,
          onSuccess: onSuccess,
          retryable: retryable,
          errorAcceptText: errorAcceptText,
          errorRetryText: errorRetryText,
        );
      }

      if (onError != null) onError(err.toString());
    }
    return results;
  }

  return null;
}