AppCreateBloc constructor

AppCreateBloc(
  1. String appId,
  2. AppModel initialiseWithApp
)

Implementation

AppCreateBloc(
  this.appId,
  AppModel initialiseWithApp,
)   : appModel = deepCopy(appId, initialiseWithApp),
      super(AppCreateUninitialised()) {
  on<AppCreateEventValidateEvent>((event, emit) async {
    var pages = await _pages();
    var dialogs = await _dialogs();
    var policies = await _policies();
    var workflows = await _workflows();
    var theHomeMenu = await homeMenu(appId, store: true);
    var theAppBar = await appBar(appId);
    var leftDrawer = await getDrawer(appId, DrawerType.left, store: true);
    var rightDrawer = await getDrawer(appId, DrawerType.right, store: true);
    _listen();
    emit(AppCreateValidated(
        deepCopy(appId, event.appModel),
        pages,
        dialogs,
        workflows,
        policies,
        theHomeMenu,
        theAppBar,
        leftDrawer,
        rightDrawer));
  });

  on<AppCreateDeletePage>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      var page = event.deleteThis;
      await pageRepository(appId: appId)!.delete(page);
      var pages = await _pages();
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          pages,
          appCreateInitialised.dialogs,
          appCreateInitialised.workflows,
          appCreateInitialised.policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

  on<AppCreateDeleteDialog>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      var dialog = event.deleteThis;
      await dialogRepository(appId: appId)!.delete(dialog);
      var dialogs = await _dialogs();
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          dialogs,
          appCreateInitialised.workflows,
          appCreateInitialised.policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

  on<AppCreateDeleteWorkflow>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      var workflow = event.deleteThis;
      await workflowRepository(appId: appId)!.delete(workflow);
      var workflows = await _workflows();
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          appCreateInitialised.dialogs,
          workflows,
          appCreateInitialised.policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

  on<AppCreateDeletePolicy>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      var policy = event.deleteThis;
      await appPolicyRepository(appId: appId)!.delete(policy);
      var policies = await _policies();
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          appCreateInitialised.dialogs,
          appCreateInitialised.workflows,
          policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

  on<AppCreateAddPolicy>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      var policyMedium = event.addThis;
      var appPolicyModel = AppPolicyModel(
        documentID: newRandomKey(),
        appId: appId,
        name: 'new policy',
        policy: policyMedium,
        conditions: StorageConditionsModel(
          privilegeLevelRequired:
              PrivilegeLevelRequiredSimple.noPrivilegeRequiredSimple,
        ),
      );
      await appPolicyRepository(appId: appId)!.add(appPolicyModel);
      var policies = await _policies();
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          appCreateInitialised.dialogs,
          appCreateInitialised.workflows,
          policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

/*
  on<AppCreateAddWorkflow>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      var workflow = event.addThis;
      var appPolicyModel = AppPolicyModel(
        documentID: newRandomKey(),
        appId: appId,
        name: 'new policy',
        policy: policyMedium,
        conditions: StorageConditionsModel(
          privilegeLevelRequired:
          PrivilegeLevelRequiredSimple.noPrivilegeRequiredSimple,
        ),
      );
      await appPolicyRepository(appId: appId)!.add(appPolicyModel);
      var policies = await _policies();
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          appCreateInitialised.dialogs,
          appCreateInitialised.workflows,
          policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });
*/

  on<AppCreateEventApplyChanges>((event, emit) async {
    var theState = state as AppCreateInitialised;
    // check other blocks for implementation
    appModel.email = theState.appModel.email;
    appModel.description = theState.appModel.description;
    appModel.title = theState.appModel.title;
    appModel.logo = theState.appModel.logo;
    appModel.homePages = theState.appModel.homePages;
    appModel.appStatus = theState.appModel.appStatus;
    appModel.autoPrivileged1 = theState.appModel.autoPrivileged1;
    appModel.isFeatured = theState.appModel.isFeatured;
    appModel.includeSubscriptions = theState.appModel.includeSubscriptions;
    appModel.includeInvoiceAddress = theState.appModel.includeInvoiceAddress;
    appModel.includeShippingAddress =
        theState.appModel.includeShippingAddress;
    appModel.welcomeMessage = theState.appModel.welcomeMessage;

    if (event.save) {
      var app = await appRepository(appId: appId)!
          .get(theState.appModel.documentID);
      if (app == null) {
        await appRepository(appId: appId)!.add(theState.appModel);
      } else {
        await appRepository(appId: appId)!.update(theState.appModel);
      }
    }
    closeAll();
  });

  on<AppCreateEventClose>((event, emit) async {
    closeAll();
  });

  on<PagesUpdated>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          event.pages,
          appCreateInitialised.dialogs,
          appCreateInitialised.workflows,
          appCreateInitialised.policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

  on<DialogsUpdated>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          event.dialogs,
          appCreateInitialised.workflows,
          appCreateInitialised.policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

  on<WorkflowsUpdated>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          appCreateInitialised.dialogs,
          event.workflows,
          appCreateInitialised.policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });

  on<PoliciesUpdated>((event, emit) async {
    if (state is AppCreateInitialised) {
      var appCreateInitialised = state as AppCreateInitialised;
      emit(AppCreateValidated(
          appCreateInitialised.appModel,
          appCreateInitialised.pages,
          appCreateInitialised.dialogs,
          appCreateInitialised.workflows,
          event.policies,
          appCreateInitialised.homeMenuModel,
          appCreateInitialised.appBarModel,
          appCreateInitialised.leftDrawerModel,
          appCreateInitialised.rightDrawerModel));
    }
  });
}