getStepAfterStep method

  1. @override
RPStep getStepAfterStep (RPStep step, RPTaskResult result)
override

Returns the step after a specified step if there's any taking the RPStepNavigationRules into consideration.

If the specified step is null then it returns the first step. Returns null if step was the last one in the sequence.

Implementation

@override
RPStep getStepAfterStep(RPStep step, RPTaskResult result) {
  RPStep _stepToReturn;

  _returnNextQuestion() {
    int nextIndex = _steps.indexOf(step) + 1;

    if (nextIndex < _steps.length) {
      _stepToReturn = _steps[nextIndex];
    } else {
      _stepToReturn = null;
    }
  }

  if (step == null) {
    _stepToReturn = _steps.first;
    return _stepToReturn;
  }

  if (_stepNavigationRules.containsKey(step.identifier)) {
    RPStepNavigationRule rule = _stepNavigationRules[step.identifier];

    switch (rule.runtimeType) {
      case RPPredicateStepNavigationRule:
        (rule as RPPredicateStepNavigationRule)
            .resultPredicatesWithDestinationIdentifiers
            .forEach((resultPredicate, destinationStepIdentifier) {
          // Catching the first
          if (resultPredicate.getPredictionResult()) {
            _steps.forEach((step) {
              if (step.identifier == destinationStepIdentifier) {
                _stepToReturn = step;
              }
            });
          } else {
            _returnNextQuestion();
          }
        });
        break;
      case RPDirectStepNavigationRule:
        String destinationStepIdentifier = (rule as RPDirectStepNavigationRule).destinationStepIdentifier;
        _steps.forEach((step) {
          if (step.identifier == destinationStepIdentifier) {
            _stepToReturn = step;
          }
        });
        break;
      default:
        throw ("Navigation Rule's type ${_stepNavigationRules[step.identifier].runtimeType} is not a navigation rule type");
        break;
    }
  } else {
    _returnNextQuestion();
  }

  return _stepToReturn;
}