triggerReports method

void triggerReports(
  1. List<AbstractReport> reportList
)

Implementation

void triggerReports(List<AbstractReport> reportList) {
  // Get context dynamically from navigatorKey, fallback to cached context for backward compatibility
  final BuildContext? ctx = getContext();

  if (ctx == null) {
    log.e("Ignoring trigger because we have no BuildContext yet. Make sure to pass pulse.navigatorKey to MaterialApp's navigatorKey parameter.");
    return;
  }

  if (!config.haveCategories()) {
    log.e('Ignoring trigger because we have no configuration');
    return;
  }

  // Set triggered state and capture initial screen state
  AbstractReport.isTriggered = true;

  // Find the screen report and trigger it first to capture current state
  var screenReport = reportList.whereType<ScreenReport>().firstOrNull;
  if (screenReport != null) {
    screenReport.trigger();
  }

  try {
    log.d('Triggering shake');

    // First, ask user to select report Type
    TypeDialog(theme)
        .chooseType(ctx)
        .then((reportType) {
          if (reportType != ReportType.CANCELLED) {
            // Get fresh context for the next dialog
            final freshCtx = getContext();
            if (freshCtx == null || !freshCtx.mounted) {
              log.e('Context no longer available after type selection');
              cleanReports();
              AbstractReport.isTriggered = false;
              return;
            }
            // Only call createReport AFTER type is selected
            handleTypeSelected(freshCtx, reportList, reportType);
          } else {
            // User cancelled at the report type selection
            log.i('User cancelled at report type selection');
            cleanReports();
            AbstractReport.isTriggered = false;
          }
        })
        .catchError((error, stackTrace) {
          handleError(error);
          log.e(stackTrace);
        });
    log.d('Finished handling shake');
  } catch (err) {
    log.e('Unexpected error $err');
    if (ctx.mounted) {
      TypeDialog(theme).errorDialog(ctx, 'Unexpected error', err.toString());
    }
    AbstractReport.isTriggered = false;
  }
}