MenuDefCreateBloc constructor

MenuDefCreateBloc(
  1. AppModel app,
  2. MenuDefModel menu
)

Implementation

MenuDefCreateBloc(
  this.app,
  this.menu,
) : super(MenuDefCreateUninitialised()) {
  on<MenuDefCreateInitialiseEvent>((event, emit) {
    //var appId = app.documentID;
    emit(MenuDefCreateInitialised(
      menuDefModel: event.menuDefModel,
    ));
  });

  on<MenuDefCreateDeleteMenuItem>((event, emit) {
    emit(_newStateDeleteItem(event.menuItemModel));
    apply();
  });

  on<MenuDefCreateAddLogin>((event, emit) {
    emit(_newStateWithNewItem(
      MenuItemModel(
          documentID: newRandomKey(),
          text: 'Sign in',
          description: 'Sign in',
          action: InternalAction(app,
              internalActionEnum: InternalActionEnum.login)),
    ));
    apply();
  });

  on<MenuDefCreateAddLogout>((event, emit) {
    emit(_newStateWithNewItem(
      MenuItemModel(
          documentID: newRandomKey(),
          text: 'Sign out',
          description: 'Sign out',
          action: InternalAction(app,
              internalActionEnum: InternalActionEnum.logout)),
    ));
    apply();
  });

  on<MenuDefCreateAddGoHome>((event, emit) {
    emit(_newStateWithNewItem(
      MenuItemModel(
          documentID: newRandomKey(),
          text: 'Go home',
          icon: IconModel(
              codePoint: Icons.home.codePoint,
              fontFamily: Icons.settings.fontFamily),
          description: 'Go home',
          action: InternalAction(app,
              internalActionEnum: InternalActionEnum.goHome)),
    ));
    apply();
  });

  on<MenuDefCreateAddOtherApps>((event, emit) {
    emit(_newStateWithNewItem(
      MenuItemModel(
          documentID: newRandomKey(),
          text: 'Other apps',
          description: 'Other apps',
          action: InternalAction(app,
              internalActionEnum: InternalActionEnum.otherApps)),
    ));
    apply();
  });

  on<MenuDefCreateAddMenuItemForPage>((event, emit) {
    emit(_newStateWithNewItem(
      MenuItemModel(
          documentID: newRandomKey(),
          text: 'New Page',
          description: 'New Page',
          icon: IconModel(
              codePoint: Icons.favorite_border.codePoint,
              fontFamily: Icons.settings.fontFamily),
          action: GotoPage(app, pageID: event.pageModel.documentID)),
    ));
    apply();
  });

  on<MenuDefCreateAddMenuItemForDialog>((event, emit) {
    emit(_newStateWithNewItem(
      MenuItemModel(
          documentID: newRandomKey(),
          text: 'New Dialog',
          description: 'New Dialog',
          icon: IconModel(
              codePoint: Icons.favorite_border.codePoint,
              fontFamily: Icons.settings.fontFamily),
          action: OpenDialog(app, dialogID: event.dialogModel.documentID)),
    ));
    apply();
  });

  on<MenuDefCreateAddMenuItemForWorkflow>((event, emit) {
    emit(_newStateWithNewItem(
      MenuItemModel(
          documentID: newRandomKey(),
          text: 'New Workflow',
          description: 'New Workflow',
          icon: IconModel(
              codePoint: Icons.favorite_border.codePoint,
              fontFamily: Icons.settings.fontFamily),
          action: WorkflowActionModel(app, workflow: event.workflowModel)),
    ));
    apply();
  });

  on<MenuDefMoveMenuItem>((event, emit) {
    MenuDefCreateInitialised theState = state as MenuDefCreateInitialised;
    List<MenuItemModel> menuItems = List.of(theState.menuDefModel.menuItems!);
    int positionToMove = menuItems.indexOf(event.menuItemModel);
    if (event.moveMenuItemDirection == MoveMenuItemDirection.up) {
      if (positionToMove > 0) {
        menuItems.swap(positionToMove - 1, positionToMove);
      }
    } else if (event.moveMenuItemDirection == MoveMenuItemDirection.down) {
      if (positionToMove < menuItems.length - 1) {
        menuItems.swap(positionToMove + 1, positionToMove);
      }
    }
    emit(_newStateWithItems(menuItems,
        currentlySelected: event.menuItemModel));
    apply();
  });

  on<MenuDefUpdateMenuItem>((event, emit) {
    MenuDefCreateInitialised theState = state as MenuDefCreateInitialised;
    List<MenuItemModel> menuItems = List.of(theState.menuDefModel.menuItems!);
    int positionToReplace = menuItems.indexOf(event.beforeMenuItemModel);
    menuItems.replace(positionToReplace, event.afterMenuItemModel);
    emit(_newStateWithItems(menuItems,
        currentlySelected: event.afterMenuItemModel));
    apply();
  });
}