createAppFromJson static method

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

Implementation

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

  Map<String, dynamic>? map = jsonDecode(jsonText);
  if (map != null) {
    var oldAppId = map['app']['documentID'];
    var leftDrawerDocumentId = drawerID(appId, DrawerType.left);
    var rightDrawerDocumentId = drawerID(appId, DrawerType.right);
    var homeMenuId = homeMenuID(appId);
    var appBarId = appBarID(appId);
    List<ComponentSpec> createdComponents = [];
    for (var entry in map.entries) {
      tasks.add(() async {
        var key = entry.key;
        if (key == JsonConsts.app) {
          var appMap = entry.value;
          appMap['ownerID'] = memberId;
          var appEntity = AppEntity.fromMap(appMap);
          if (appEntity != null) {
            await appRepository()!.updateEntity(appId, appEntity);
          }
        } else if (key == "${DrawerModel.packageName}-${DrawerModel.id}") {
          var values = entry.value;
          for (var theItem in values) {
            String documentID;
            if (theItem['documentID'] ==
                drawerID(oldAppId, DrawerType.left)) {
              documentID = leftDrawerDocumentId;
            } else {
              documentID = rightDrawerDocumentId;
            }
            theItem['appId'] = appId;
            var entity = drawerRepository(appId: appId)!.fromMap(theItem);
            if (entity != null) {
              await drawerRepository(appId: appId)!
                  .addEntity(documentID, entity);
            } else {
              print("Error getting entity for $theItem");
            }
          }
        } else if (key == "${AppBarModel.packageName}-${AppBarModel.id}") {
          var values = entry.value;
          for (var theItem in values) {
            theItem['appId'] = appId;
            var entity = appBarRepository(appId: appId)!.fromMap(theItem);
            if (entity != null) {
              await appBarRepository(appId: appId)!
                  .addEntity(appBarId, entity);
            } else {
              print("Error getting entity for $theItem");
            }
          }
        } else if (key ==
            "${HomeMenuModel.packageName}-${HomeMenuModel.id}") {
          var values = entry.value;
          for (var theItem in values) {
            theItem['appId'] = appId;
            var entity = homeMenuRepository(appId: appId)!.fromMap(theItem);
            if (entity != null) {
              await homeMenuRepository(appId: appId)!
                  .addEntity(homeMenuId, entity);
            } else {
              print("Error getting entity for $theItem");
            }
          }
        } else if (key == JsonConsts.pages) {
          List<dynamic>? pages = entry.value;
          if (pages != null) {
            for (var page in pages) {
              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) {
              await createDialogEntity(dialog, appId, feedback: feedback);
            }
          }
        } else if (key == "${MenuDefModel.packageName}-${MenuDefModel.id}") {
          var theItems = entry.value;
          if (theItems != null) {
            await restoreFromMap(
                theItems, menuDefRepository(appId: appId)!, appId,
                postProcessing: (menuDef) {
              var menuItems = menuDef['menuItems'];
              for (var menuItem in menuItems) {
                var action = menuItem['action'];
                if (action != null) {
                  action['appID'] = appId;
                }
              }
            });
          }
        } else 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);
            }
          }
        } 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);
            }
          }
        } 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);
            }
          }
        } else {
          var split = key.split('-');
          if (split.length == 2) {
            try {
              var pluginName = split[0];
              var componentId = split[1];
              var values = entry.value;
              var retrieveRepo = Apis.apis()
                  .getRegistryApi()
                  .getRetrieveRepository(pluginName, componentId);
              if (retrieveRepo != null) {
                var documentIds = await restoreFromMap(
                    values, retrieveRepo(appId: appId), appId);
                for (var doc in documentIds) {
                  print("Doc : $doc");
                }
                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");
          }
        }
      });
    }
    revalidateComponentModels(app, tasks, createdComponents);
  }

  return tasks;
}