create method

Future<AppModel> create(
  1. AppBuilderFeedback appBuilderFeedback,
  2. bool fromExisting, {
  3. MemberMediumModel? memberMediumModel,
  4. String? url,
})

Implementation

Future<AppModel> create(
    AppBuilderFeedback appBuilderFeedback, bool fromExisting,
    {MemberMediumModel? memberMediumModel, String? url}) async {
  List<NewAppTask> tasks = [];
  // create the app
  tasks.add(() async {
    newlyCreatedApp = await appRepository()!.add(AppModel(
      documentID: appId,
      appStatus: AppStatus.offline,
      title: 'New application',
      ownerID: memberId,
    ));
  });

  tasks.add(() async {
    print("claimAccess");
    await claimAccess(appId, memberId);
  });
  tasks.add(() async {
    print("claimOwnerShipApplication");
    claimOwnerShipApplication(appId, memberId);
  });

  List<NewAppTask> newTasks = [];
  if (fromExisting) {
    if (url != null) {
      newTasks = await createAppFromUrl(url);
    } else if (memberMediumModel != null) {
      newTasks = await createAppFromMemberMedium(memberMediumModel);
    } else {
      newTasks = await createAppFromClipboard();
    }
  } else {
    newTasks = await createApp();
  }
  tasks.addAll(newTasks);

  var progressManager = ProgressManager(
      tasks.length, (progress) => appBuilderFeedback.progress(progress));

  var currentTask = tasks[0];
  currentTask().then((value) => tasks[1]);

  int i = 0;
  for (var task in tasks) {
    i++;
    try {
      await task();
    } catch (e) {
      print('Exception running task $i, error: $e');
    }
    progressManager.progressedNextStep();
    if (appBuilderFeedback.isCancelled()) {
      throw Exception("Process cancelled");
    }
  }

  if (newlyCreatedApp != null) {
    appBuilderFeedback.finished();
    return newlyCreatedApp!;
  } else {
    throw Exception("no app created");
  }
}