DrawerCreateBloc constructor

DrawerCreateBloc(
  1. String appId,
  2. DrawerType drawerType,
  3. DrawerModel initialiseWithDrawerModel
)

Implementation

DrawerCreateBloc(
  this.appId,
  this.drawerType,
  DrawerModel initialiseWithDrawerModel,
)   : drawerModel = deepCopy(appId, drawerType, initialiseWithDrawerModel),
      super(DrawerCreateUninitialised()) {
  on<DrawerCreateEventValidateEvent>((event, emit) {
    // the updates happen on a (deep) copy
    emit(DrawerCreateValidated(
        deepCopy(appId, drawerType, event.drawerModel)));
  });

  on<DrawerCreateEventApplyChanges>((event, emit) async {
    var theState = state as DrawerCreateInitialised;
    drawerModel.menu = theState.drawerModel.menu;
    drawerModel.headerText = theState.drawerModel.headerText;
    drawerModel.headerBackgroundOverride =
        theState.drawerModel.headerBackgroundOverride;
    drawerModel.secondHeaderText = theState.drawerModel.secondHeaderText;
    if (event.save) {
      var drawer = await drawerRepository(appId: drawerModel.appId)!
          .get(theState.drawerModel.documentID);
      if (drawer == null) {
        await drawerRepository(appId: drawerModel.appId)!
            .add(theState.drawerModel);
      } else {
        await drawerRepository(appId: drawerModel.appId)!
            .update(theState.drawerModel);
      }

      var menuDef = await menuDefRepository(appId: drawerModel.appId)!
          .get(theState.drawerModel.menu!.documentID);
      if (menuDef == null) {
        await menuDefRepository(appId: drawerModel.appId)!
            .add(theState.drawerModel.menu!);
      } else {
        await menuDefRepository(appId: drawerModel.appId)!
            .update(theState.drawerModel.menu!);
      }
    }
  });
}