createOtherFromJson static method

Future<List<NewAppTask>> createOtherFromJson(
  1. AppModel app,
  2. String memberId,
  3. String jsonText,
  4. bool includeMedia, {
  5. Feedback? feedback,
})

Implementation

static Future<List<NewAppTask>> createOtherFromJson(
  AppModel app,
  String memberId,
  String jsonText,
  bool includeMedia, {
  Feedback? feedback,
}) async {
  var appId = app.documentID;
  List<NewAppTask> tasks = [];

  Map<String, dynamic>? map = jsonDecode(jsonText);
  if (map != null) {
    var leftDrawerDocumentId = drawerID(appId, DrawerType.left);
    var rightDrawerDocumentId = drawerID(appId, DrawerType.right);
    var homeMenuId = homeMenuID(appId);
    var appBarId = appBarID(appId);

    Map<String, String> newDocumentIds = {};

    // pages and/or dialogs
    for (var entry in map.entries) {
      tasks.add(() async {
        var key = entry.key;
        if (key == JsonConsts.pages) {
          List<dynamic>? pages = entry.value;
          if (pages != null) {
            for (var page in pages) {
              page['documentID'] = newRandomKey();
              var bodyComponents = page['bodyComponents'];
              for (var bodyComponent in bodyComponents) {
                var oldComponentId = bodyComponent['componentId'];
                var newComponentId = newRandomKey();
                bodyComponent['componentId'] = newComponentId;
                newDocumentIds[oldComponentId] = newComponentId;
              }
              await createPageEntity(page, appId, homeMenuId,
                  leftDrawerDocumentId, rightDrawerDocumentId, appBarId,
                  feedback: feedback);
            }
          }
        } else if (key == JsonConsts.dialogs) {
          List<dynamic>? dialogs = entry.value;
          if (dialogs != null) {
            for (var dialog in dialogs) {
              dialog['documentID'] = newRandomKey();
              await createDialogEntity(dialog, appId, feedback: feedback);
            }
          }
        }
      });
    }

    List<ComponentSpec> createdComponents = [];
    // components
    for (var entry in map.entries) {
      tasks.add(() async {
        var key = entry.key;
        if (key == JsonConsts.app) {
        } else if (key == "${DrawerModel.packageName}-${DrawerModel.id}") {
        } else if (key == "${AppBarModel.packageName}-${AppBarModel.id}") {
        } else if (key ==
            "${HomeMenuModel.packageName}-${HomeMenuModel.id}") {
        } else if (key == "${MenuDefModel.packageName}-${MenuDefModel.id}") {
        } else if (key == JsonConsts.pages) {
        } else if (key == JsonConsts.dialogs) {
        } else if (key ==
            "${PlatformMediumModel.packageName}-${PlatformMediumModel.id}") {
        } else if (key ==
            "${MemberMediumModel.packageName}-${MemberMediumModel.id}") {
        } else if (key ==
            "${PublicMediumModel.packageName}-${PublicMediumModel.id}") {
        } else {
          var split = key.split('-');
          if (split.length == 2) {
            try {
              var pluginName = split[0];
              var componentId = split[1];
              var values = entry.value;
              print("restoring: $pluginName $componentId");
              var retrieveRepo = Apis.apis()
                  .getRegistryApi()
                  .getRetrieveRepository(pluginName, componentId);
              if (retrieveRepo != null) {
                var documentIds = await restoreFromMap(
                  values,
                  retrieveRepo(appId: appId),
                  appId,
                  newDocumentIds: newDocumentIds,
                );
                createdComponents.addAll(documentIds
                    .map((documentId) =>
                        ComponentSpec(pluginName, componentId, documentId))
                    .toList());
              } else {
                print("Can't find repo for: $key");
              }
            } catch (e) {
              print("Error processing $key");
            }
          } else {
            print("Don't know how to handle this entry with key: $key");
          }
        }
      });
    }

    // medium
    if (includeMedia) {
      for (var entry in map.entries) {
        tasks.add(() async {
          var key = entry.key;
          if (key ==
              "${PlatformMediumModel.packageName}-${PlatformMediumModel.id}") {
            var theItems = entry.value;
            if (theItems != null) {
              var repository = platformMediumRepository(appId: appId)!;
              for (var theItem in theItems) {
                await createPlatformMedium(theItem, app, memberId, repository,
                    newDocumentIds: newDocumentIds);
              }
            }
          } else if (key ==
              "${MemberMediumModel.packageName}-${MemberMediumModel.id}") {
            var theItems = entry.value;
            if (theItems != null) {
              var repository = memberMediumRepository(appId: appId)!;
              for (var theItem in theItems) {
                await createMemberMedium(theItem, app, memberId, repository,
                    newDocumentIds: newDocumentIds);
              }
            }
          } else if (key ==
              "${PublicMediumModel.packageName}-${PublicMediumModel.id}") {
            var theItems = entry.value;
            if (theItems != null) {
              var repository = publicMediumRepository(appId: appId)!;
              for (var theItem in theItems) {
                await createPublicMedium(theItem, app, memberId, repository,
                    newDocumentIds: newDocumentIds);
              }
            }
          }
        });
      }
    }

    revalidateComponentModels(app, tasks, createdComponents);
  }

  return tasks;
}