getPathFromPop method

VPopResult getPathFromPop(
  1. VRouteElement elementToPop, {
  2. required Map<String, String> pathParameters,
  3. required GetNewParentPathResult parentPathResult,
})
override

VPopResult.didPop is true if this VRouteElement popped VPopResult.extendedPath is null if this path can't be the right one according to the path parameters VPopResult is null when this VRouteElement does not pop AND none of its stackedRoutes popped

Implementation

VPopResult getPathFromPop(
  VRouteElement elementToPop, {
  required Map<String, String> pathParameters,
  required GetNewParentPathResult parentPathResult,
}) {
  // If vRouteElement is this, then this is the element to pop so we return null
  if (elementToPop == this) {
    return PoppingPopResult(poppedVRouteElements: [this]);
  }

  final List<GetNewParentPathResult> newParentPaths = [
    getNewParentPath(
      parentPathResult,
      thisPath: path,
      thisPathParametersKeys: pathParametersKeys,
      pathParameters: pathParameters,
    ),
    for (var i = 0; i < aliases.length; i++)
      getNewParentPath(
        parentPathResult,
        thisPath: aliases[i],
        thisPathParametersKeys: aliasesPathParametersKeys[i],
        pathParameters: pathParameters,
      ),
  ];

  for (var vRouteElement in stackedRoutes) {
    final List<MissingPathParamsError> missingPathParamsErrors = [];
    Set<VRouteElement> poppedVRouteElements = Set.of([]);

    for (var newParentPath in newParentPaths) {
      final childPopResult = vRouteElement.getPathFromPop(
        elementToPop,
        pathParameters: pathParameters,
        parentPathResult: newParentPath,
      );
      if (!(childPopResult is NotFoundPopResult)) {
        // If the VRouteElement to pop has been found

        // If it pops, we should pop
        if (childPopResult is PoppingPopResult) {
          // Add ourselves to the poppedVRouteElements in a PoppingPopResult
          return PoppingPopResult(
            poppedVRouteElements:
                childPopResult.poppedVRouteElements + [this],
          );
        }

        // If ValidPopResult, return as is
        if (childPopResult is ValidPopResult) {
          return childPopResult;
        }

        // Else the childPopResult should be a PathParamsPopErrors
        poppedVRouteElements.addAll(
          (childPopResult as PathParamsPopErrors).poppedVRouteElements,
        );
        missingPathParamsErrors.addAll(childPopResult.values);
      }
    }

    // If missingPathParamsErrors is not empty, it means that we did found the VRouteElement
    // to pop in the current route but no ValidPopResult (only PathParamsPopErrors)
    // So return a PathParamsPopErrors with the aggregated missingPathParamsErrors
    if (missingPathParamsErrors.isNotEmpty) {
      return PathParamsPopErrors(
        poppedVRouteElements: poppedVRouteElements.toList(),
        values: missingPathParamsErrors,
      );
    }
  }

  // If none of the stackedRoutes popped and this did not pop, return a NotValidPopResult
  // This should never reach RootVRouter
  return NotFoundPopResult();
}