parseCoordinates function

  1. @visibleForTesting
void parseCoordinates(
  1. Subtitle subtitle,
  2. String chunk1
)

Implementation

@visibleForTesting
void parseCoordinates(Subtitle subtitle, String chunk1) {
  final RegExp detectCoordination = RegExp(r'((X|Y)(\d)):(\d\d\d)');

  final Iterable<Match> result = detectCoordination.allMatches(chunk1);

  if (result.length != 0) {
    List<Match> listOfXs =
        result.where((Match match) => match.group(2) == 'X').toList();

    //divide by 2 and create a Coordination of each X:Y group
    for (Match item in listOfXs) {
      if (item.groups([3, 4]).any((g) => g == null)) continue;

      int number = int.parse(item.group(3)!);
      Match? matchingY = result.firstWhereOrNull((Match matchY) {
        if (matchY.group(3) == null) return false;

        return (matchY.group(2) == 'Y' &&
            int.tryParse(matchY.group(3)!) == number);
      });
      if (matchingY == null) continue;

      final coordinates = Coordinates(
          x: int.parse(item.group(4) ?? '0'),
          y: int.parse(matchingY.group(4) ?? '0'));
      Line parsedLine = Line(subtitle.rawLines[listOfXs.indexOf(item)],
          coordinates: coordinates);
      subtitle.parsedLines.add(parsedLine);
    }
  } else {
    for (String line in subtitle.rawLines) {
      Line parsedLine = Line(line);
      subtitle.parsedLines.add(parsedLine);
    }
  }
}