addTasks method

Future<void> addTasks(
  1. List<ModelsJsonTask> tasks,
  2. AppModel app,
  3. ModelsJsonConstructJsonEvent event
)

Implementation

Future<void> addTasks(List<ModelsJsonTask> tasks, AppModel app,
    ModelsJsonConstructJsonEvent event) async {
  List<AbstractModelWithInformation> modelsWithInformation =
      event.dataContainer;
  List<ModelReference> referencedModels = [];
  var appId = app.documentID;
  final Map<String, dynamic> theMap = {};
  tasks.add(() async {
    for (var modelWithInformation in modelsWithInformation) {
      theMap[modelWithInformation.label] = await modelWithInformation
          .toRichMap(appId: appId, referencedModels: referencedModels);
    }
  });

  tasks.add(() async {
    Set<String> referencedModels2 = <String>{};

    referencedModels
        .retainWhere((element) => referencedModels2.add(element.key()));

    for (var referencedModel in referencedModels) {
      var fullName =
          "${referencedModel.packageName}-${referencedModel.componentName}";
      var map = theMap[fullName];
      if (map == null) {
        theMap[fullName] = [];
      }
      var entity = referencedModel.referenced
          .toEntity(appId: appId /*, referencesCollector: referencedModels*/);
      var doc = entity.toDocument();
      doc['documentID'] = referencedModel.referenced.documentID;
      await entity.enrichedDocument(doc);
      theMap[fullName].add(doc);
    }
  });

  tasks.add(() async {
    var jsonEncoded = jsonEncode(theMap);
    if (event is ModelsJsonConstructJsonEventToClipboard) {
      try {
        await Clipboard.setData(ClipboardData(text: jsonEncoded));
        add(ModelsAndJsonAvailableInClipboardEvent());
      } catch (e) {
        add(ModelsAndJsonErrorEvent(
            "Couldn't copy the json to clipboard. It's likely too large"));
      }
    } else if (event is ModelsJsonConstructJsonEventToMemberMediumModel) {
      String docID = newRandomKey();
      var memberMedium = await MemberMediumHelper(
              app, event.member.documentID, MemberMediumAccessibleByGroup.me)
          .uploadTextData(docID, jsonEncoded, event.baseName);
      try {
        await Clipboard.setData(ClipboardData(text: memberMedium.url ?? ''));
      } catch (e) {
        print("Can't set clipboard. Exception: $e");
      }
      add(ModelsAndJsonAvailableAsMemberMediumEvent(memberMedium));
    }
  });
}