submitFormWithProgress method

Stream<SubmitFormProgressEvent> submitFormWithProgress(
  1. ApptiveLink link,
  2. FormData formData, {
  3. bool saveToPendingItems = true,
  4. Map<String, String> headers = const {},
})

Submits formData against link

headers will be added in addition to ApptiveGridClient.defaultHeaders

This will return a Stream of SubmitFormProgressEvents to indicate the current step of the submission

Implementation

Stream<SubmitFormProgressEvent> submitFormWithProgress(
  ApptiveLink link,
  FormData formData, {
  bool saveToPendingItems = true,
  Map<String, String> headers = const {},
}) async* {
  final actionItem = ActionItem(link: link, data: formData);

  final controller = StreamController<SubmitFormProgressEvent>();

  _performAttachmentActions(
    formData.attachmentActions,
    fromForm: true,
    headers: headers,
    statusController: controller,
  );

  SubmitFormProgressEvent? attachmentActions;
  yield* controller.stream.map((event) {
    if (event is AttachmentCompleteProgressEvent ||
        event is ErrorProgressEvent) {
      attachmentActions ??= event;
    }
    return event;
  }).handleError((error) async {
    if (error is http.Response) {
      attachmentActions ??= AttachmentCompleteProgressEvent(error);
    } else {
      attachmentActions ??= ErrorProgressEvent(error);
    }
  });

  if ((attachmentActions is ErrorProgressEvent) ||
      (attachmentActions is AttachmentCompleteProgressEvent &&
          ((attachmentActions as AttachmentCompleteProgressEvent)
                      .response
                      ?.statusCode ??
                  400) >=
              400)) {
    late final dynamic error;
    if (attachmentActions is ErrorProgressEvent) {
      error = (attachmentActions as ErrorProgressEvent).error;
    } else {
      error = (attachmentActions as AttachmentCompleteProgressEvent).response;
    }
    if (saveToPendingItems && error != null) {
      yield SubmitCompleteProgressEvent(
        await _handleActionError(
          error,
          actionItem: actionItem,
          saveToPendingItems: saveToPendingItems,
        ),
      );
    } else {
      yield ErrorProgressEvent(error);
    }

    controller.close();
    return;
  }

  yield UploadFormProgressEvent(formData);
  late http.Response? response;
  try {
    response = await performApptiveLink<http.Response>(
      link: link,
      body: formData.toRequestObject(),
      headers: headers,
      parseResponse: (response) async => response,
    );
  } catch (error) {
    // Catch all Exception for compatibility Reasons between Web and non Web Apps

    if (saveToPendingItems) {
      yield SubmitCompleteProgressEvent(
        await _handleActionError(
          error,
          actionItem: actionItem,
          saveToPendingItems: saveToPendingItems,
        ),
      );
    } else {
      yield ErrorProgressEvent(error);
    }
    return;
  }
  if (response != null && response.statusCode < 400) {
    // Action was performed successfully. Remove it from pending Actions
    await options.cache?.removePendingActionItem(actionItem);
  }
  yield SubmitCompleteProgressEvent(response);
}