updateCurrentPath method

void updateCurrentPath(
  1. Offset newPos,
  2. void testConcluded(
    1. int result,
    2. int mistakes
    )
)

Implementation

void updateCurrentPath(Offset newPos, void Function(int, int) testConcluded) {
  if (_isDraging && !_isFinished) {
    Path path = _paths.last;
    path.lineTo(newPos.dx, newPos.dy);
    Offset firstPoint = path
        .computeMetrics()
        .first
        .getTangentForOffset(0)!
        .position;

    // Avoid if drag hits another locations before hitting the next location (e.g. A-C-B)
    List<Location> locationCopy = List.from(locations);
    locationCopy.remove(prevLocation);
    locationCopy.remove(nextLocation);
    for (Location l in locationCopy) {
      if (l.rect.contains(newPos)) {
        _isDraging = false;
        gestureController.addWrongGesture(
          'Draw path',
          'Drew a path which hit ${l.id} instead of the correct, next item ${nextLocation.id}',
        );
        deleteWrong();
        mistakeCount++;
        return;
      }
    }

    // If dragging directly without lifting finger
    if (prevLocation.rect.contains(firstPoint) &&
        nextLocation.rect.contains(newPos)) {
      gestureController.addCorrectGesture(
        'Draw path',
        'Drew a correct path from ${prevLocation.id} to ${nextLocation.id}',
      );
      Path newPath = Path();
      newPath.moveTo(newPos.dx, newPos.dy);
      _paths.add(newPath);
      if (index < locations.length - 1) {
        prevLocation = nextLocation;
        index += 1;
        nextLocation = locations[index];
      } else {
        _isFinished = true;

        gestureController.testEnded();
        gestureController.resultsShown();
        int secondsUsed = DateTime.now().difference(startTime).inSeconds;
        testConcluded(secondsUsed, mistakeCount);
      }
    }
  }
}