fetch method

Future<void> fetch()

Implementation

Future<void> fetch() async {
  final NodesResponse response = await _figmaClient.getFileNodes(fileKey, FigmaQuery(
    ids: [canvasKey],
  ));

  final Map<String, String> nameMap = <String, String>{};

  response.nodes![canvasKey]!.components!.forEach((key, value) {
    final String fileName = _cleanupFileName(value.name!);
    if (fileName != '' && !fileName.startsWith('.') && !_isNumeric(fileName[0])) {
      nameMap[key] = _cleanupFileName(value.name!);
    }
  });

  final List<String> componentIds = nameMap.keys.toList();

  print('Found ${componentIds.length} components, fetching file information');

  const int slotSize = 500;
  final int queueSize = (componentIds.length / slotSize).ceil();

  final Map<String, String> imageResults = <String, String>{};

  // fetch image paths
  final List<int> pages = Iterable<int>.generate(queueSize).toList();

  for(final int index in pages) {
    final int comidx = index * slotSize;
    final List<String> ids = componentIds.sublist(comidx, min(componentIds.length, comidx + 500));

    if (ids.isEmpty) {
      continue;
    }

    _fileInfoQueue.add(() async {
      try {
        final ImageResponse imageResponse = await _figmaClient.getImages(fileKey, FigmaQuery(
          // hack to get around the usage of ";" in the figma package
          ids: [ids.join(',')],
          format: 'svg',
          scale: 1,
          svgSimplifyStroke: true
        ));

        imageResults.addAll(imageResponse.images!);
      } on FigmaError catch (_) {} catch (_) { }
    });
  }

  await _fileInfoQueue.onIdle();

  print('got all file the information, fetching files');
  print('---');

  // download and write svg
  imageResults.entries.map((element) {
    return _downloadQueue.add(() async {
      final String? fileName = nameMap[element.key];

      final Response response = await get(Uri.parse(element.value));

      await File('${xmlOuputPath.absolute.path}/$fileName.svg').writeAsBytes(response.bodyBytes);
      print('$fileName.svg saved');

    });
  }).toList();

  await _downloadQueue.onIdle();
}