navigateToStep method

Future<void> navigateToStep(
  1. Enum step
)

Implementation

Future<void> navigateToStep(Enum step) async {
  __.in_('navigateToStep');
  try {
    checkInitialized('BaseFlow');

    // Start with the provided step
    Enum currentStepToTry = step;

    // Try steps until we find one with a valid precondition or run out of steps

    //TODO: A while(true) loop is not a good idea, we should have a clear exit condition.
    while (true) {
      // Handle special BaseFlowStep.end case
      if (currentStepToTry == BaseFlowStep.end || currentStepToTry == null) {
        __.info('End step received, completing flow');
        await complete();
        return;
      }

      // Check if this is the first step
      if (currentStepToTry == getFirstStep()) {
        onStart();
      }

      // Check precondition
      if (checkPreCondition(currentStepToTry as S)) {
        // Precondition met, we can navigate to this step
        break;
      } else {
        // TODO: This is incorrect - we should have a unique enum to handle this condition.
        this.currentStep = currentStepToTry;
        processStepResult(step, StepResult.stepSkip);
        __.info('Precondition failed for step: ${currentStepToTry.name}');
      }

      // Get the next step to try
      Enum? nextStep = getNextStep(currentStepToTry);

      if (nextStep == null) {
        // No more steps to try
        // TODO: Retaining this code, but it will never come here.
        __.warning('Precondition failed and no next step available');
        nextStep = BaseFlowStep.end as S;
      }

      // Update current step to try
      currentStepToTry = nextStep;
      __.info('Trying next step: ${currentStepToTry.name}');
    }

    // We found a step with a valid precondition, navigate to it

    performNavigationWithEnum(currentStepToTry);

    currentStep = currentStepToTry;
    __.info('Navigated to step: ${currentStepToTry.name}');
  } finally {
    __.out_('navigateToStep');
  }

  //TODO: Instead of setting a state variable, try returning the step, that will be more elegant.
}