Удалил дефолтную клавиатуру

This commit is contained in:
Struchkov Mark 2023-03-29 01:15:32 +03:00
parent 6f7b67bd79
commit 970695e4b8
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
6 changed files with 42 additions and 236 deletions

View File

@ -1,112 +0,0 @@
package dev.struchkov.godfather.main.context.utils;
import dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoard;
import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine;
import java.util.Arrays;
import java.util.List;
/**
* Используется для быстрого создания клавиаутр {@link SimpleKeyBoard}.
*
* @author upagge [08/07/2019]
*/
public class SimpleKeyBoards {
public static final SimpleButton YES_BUTTON = SimpleButton.simpleButton("Да", "{\"button\": \"yes\"}");
public static final SimpleButton NO_BUTTON = SimpleButton.simpleButton("Нет", "{\"button\": \"no\"}");
private SimpleKeyBoards() {
throw new IllegalStateException();
}
/**
* Возвращает клавиатуру формата 1х2, с кнопками "Да | Нет"
*
* @return {@link SimpleKeyBoard}
*/
public static SimpleKeyBoard keyBoardYesNo() {
return SimpleKeyBoard.builder().line(
SimpleKeyBoardLine.builder().button(YES_BUTTON).button(NO_BUTTON).build()
).build();
}
/**
* Возвращает клавиатуру формата 1хN, где N - это количество элементов в переданном списке
*
* @param labelButtons Список названий для кнопок
* @return {@link SimpleKeyBoard}
*/
public static SimpleKeyBoard verticalMenuString(List<String> labelButtons) {
final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.builder();
for (String labelButton : labelButtons) {
final SimpleButton simpleButton = SimpleButton.simpleButton(labelButton, "{\"button\": \"" + labelButton + "\"}");
keyBoard.line(SimpleKeyBoardLine.builder().button(simpleButton).build());
}
return keyBoard.build();
}
/**
* Возвращает клавиатуру формата 1хN, где N - это количество элементов в переданном списке
*
* @param labelButton Список названий для кнопок
* @return {@link SimpleKeyBoard}
*/
public static SimpleKeyBoard verticalMenuString(String... labelButton) {
return verticalMenuString(Arrays.asList(labelButton));
}
/**
* Возвращает клавиатуру формата 2х(N/2), где N - это количество элементов в переданном списке
*
* @param labelButton Список названий для кнопок
* @return {@link SimpleKeyBoard}
*/
public static SimpleKeyBoard verticalDuoMenuString(String... labelButton) {
return verticalDuoMenuString(Arrays.asList(labelButton));
}
/**
* Возвращает клавиатуру формата 2х(N/2), где N - это количество элементов в переданном списке
*
* @param labelButton Список названий для кнопок
* @return {@link SimpleKeyBoard}
*/
public static SimpleKeyBoard verticalDuoMenuString(List<String> labelButton) {
final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.builder();
boolean flag = true;
SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder();
for (int i = 0; i <= labelButton.size() - 1; i++) {
String label = labelButton.get(i);
keyBoardLine.button(SimpleButton.simpleButton(label));
if (flag) {
if (i == labelButton.size() - 1) {
keyBoard.line(keyBoardLine.build());
} else {
flag = false;
}
} else {
keyBoard.line(keyBoardLine.build());
keyBoardLine = SimpleKeyBoardLine.builder();
flag = true;
}
}
return keyBoard.build();
}
/**
* Возвращает клавиатуру формата 1xN сформированную из списка кнопок, где N - количество кнопок в списке
*
* @param simpleButtons Список кнопок
* @return {@link SimpleKeyBoard}
*/
public static SimpleKeyBoard verticalMenuButton(List<SimpleButton> simpleButtons) {
final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.builder();
for (SimpleButton simpleButton : simpleButtons) {
keyBoard.line(SimpleKeyBoardLine.builder().button(simpleButton).build());
}
return keyBoard.build();
}
}

View File

@ -13,6 +13,7 @@ import dev.struchkov.haiti.context.exception.NotFoundException;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -30,7 +31,7 @@ public class GeneralAutoResponder<M extends Message> {
protected final PersonSettingService personSettingService;
protected final StorylineService<M> storyLineService;
protected Map<String, Map<Class, ActionUnit>> actionUnitMap = new HashMap<>();
protected Map<String, List<ActionUnit>> actionUnitMap = new HashMap<>();
protected List<Modifiable<M>> modifiable;
protected ErrorHandler errorHandler;
@ -43,7 +44,8 @@ public class GeneralAutoResponder<M extends Message> {
}
public void registrationActionUnit(ActionUnit actionUnit) {
actionUnitMap.computeIfAbsent(actionUnit.getUnitType(), k -> new HashMap<>()).putIfAbsent(actionUnit.getMessageType(), actionUnit);
actionUnitMap.computeIfAbsent(actionUnit.getUnitType(), k -> new ArrayList<>());
actionUnitMap.get(actionUnit.getUnitType()).add(actionUnit);
}
public void initModifiable(List<Modifiable<M>> modifiable) {
@ -154,9 +156,10 @@ public class GeneralAutoResponder<M extends Message> {
final M message = unitRequest.getMessage();
final String typeUnit = unit.getType();
if (actionUnitMap.containsKey(typeUnit)) {
Map<Class, ActionUnit> actionMap = actionUnitMap.get(typeUnit);
if (actionMap.containsKey(message.getClass())) {
final ActionUnit<MainUnit, M> actionUnit = actionMap.get(message.getClass());
List<ActionUnit> actionUnits = actionUnitMap.get(typeUnit);
final Class<? extends Message> messageClass = message.getClass();
final ActionUnit<MainUnit, M> actionUnit = getActionUnit(actionUnits, messageClass);
if (checkNotNull(actionUnit)) {
return actionUnit.action(unitRequest)
.flatMap(
newUnitRequest -> {
@ -181,4 +184,13 @@ public class GeneralAutoResponder<M extends Message> {
throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType());
}
private ActionUnit<MainUnit, M> getActionUnit(List<ActionUnit> actionUnits, Class<? extends Message> messageClass) {
for (ActionUnit actionUnit : actionUnits) {
if (actionUnit.getMessageType().isAssignableFrom(messageClass)) {
return actionUnit;
}
}
return null;
}
}

View File

@ -4,7 +4,6 @@ import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.quarkus.context.service.Sending;
import dev.struchkov.godfather.quarkus.domain.BoxAnswer;
import dev.struchkov.godfather.quarkus.domain.unit.AnswerCheck;
import dev.struchkov.godfather.quarkus.domain.unit.AnswerText;
import dev.struchkov.godfather.quarkus.domain.unit.MainUnit;
import dev.struchkov.godfather.quarkus.domain.unit.UnitRequest;
import io.smallrye.mutiny.Uni;
@ -62,7 +61,7 @@ public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Messa
@Override
public String getUnitType() {
return AnswerText.TYPE;
return AnswerCheck.TYPE;
}
@Override

View File

@ -33,7 +33,7 @@ public class GeneralAutoResponder<M extends Message> {
protected final PersonSettingService personSettingService;
protected final StorylineService<M> storyLineService;
protected Map<String, Map<Class, ActionUnit>> actionUnitMap = new HashMap<>();
protected Map<String, List<ActionUnit>> actionUnitMap = new HashMap<>();
protected List<Modifiable<M>> modifiable;
protected ErrorHandler errorHandler;
protected ExecutorService executorService;
@ -160,15 +160,16 @@ public class GeneralAutoResponder<M extends Message> {
return unitRequest;
}
private UnitRequest<MainUnit<M>, M> getAction(UnitRequest<MainUnit<M>, M> unitRequest) {
private UnitRequest<MainUnit, M> getAction(UnitRequest<MainUnit, M> unitRequest) {
final MainUnit<M> unit = unitRequest.getUnit();
final M message = unitRequest.getMessage();
final String typeUnit = unit.getType();
if (actionUnitMap.containsKey(typeUnit)) {
Map<Class, ActionUnit> actionMap = actionUnitMap.get(typeUnit);
if (actionMap.containsKey(message.getClass())) {
final ActionUnit actionUnit = actionMap.get(message.getClass());
UnitRequest<MainUnit<M>, M> newUnitRequest = actionUnit.action(unitRequest);
List<ActionUnit> actionUnits = actionUnitMap.get(typeUnit);
final Class<? extends Message> messageClass = message.getClass();
final ActionUnit<MainUnit, M> actionUnit = getActionUnit(actionUnits, messageClass);
if (checkNotNull(actionUnit)) {
UnitRequest<MainUnit, M> newUnitRequest = actionUnit.action(unitRequest);
final Optional<MainUnit<M>> optDefaultUnit = storyLineService.getDefaultUnit();
if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) {
storyLineService.save(message.getFromPersonId(), unit.getName(), message);
@ -180,4 +181,13 @@ public class GeneralAutoResponder<M extends Message> {
throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType());
}
private ActionUnit<MainUnit, M> getActionUnit(List<ActionUnit> actionUnits, Class<? extends Message> messageClass) {
for (ActionUnit actionUnit : actionUnits) {
if (actionUnit.getMessageType().isAssignableFrom(messageClass)) {
return actionUnit;
}
}
return null;
}
}

View File

@ -1,74 +0,0 @@
package dev.struchkov.godfather.main.domain.keyboard.simple;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoard;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine;
import java.util.ArrayList;
import java.util.List;
/**
* Сущность клавиатуры, для создания меню с вариантами выбора.
*
* @author upagge [08/07/2019]
*/
public class SimpleKeyBoard implements KeyBoard {
public static final String TYPE = "SIMPLE";
/**
* Строки меню.
*/
protected List<KeyBoardLine> lines = new ArrayList<>();
public SimpleKeyBoard(List<KeyBoardLine> lines) {
this.lines = lines;
}
private SimpleKeyBoard(Builder builder) {
lines = builder.lines;
}
public static SimpleKeyBoard simpleKeyboard(KeyBoardLine line) {
return new SimpleKeyBoard(List.of(line));
}
public static Builder builder() {
return new Builder();
}
public SimpleKeyBoard simpleKeyboard(KeyBoardButton keyBoardButton) {
return simpleKeyboard(SimpleKeyBoardLine.simpleLine(keyBoardButton));
}
@Override
public String getType() {
return TYPE;
}
@Override
public List<KeyBoardLine> getLines() {
return lines;
}
public static final class Builder {
private List<KeyBoardLine> lines = new ArrayList<>();
private Builder() {
}
public Builder lines(List<KeyBoardLine> val) {
lines = val;
return this;
}
public Builder line(KeyBoardLine val) {
lines.add(val);
return this;
}
public SimpleKeyBoard build() {
return new SimpleKeyBoard(this);
}
}
}

View File

@ -2,66 +2,37 @@ package dev.struchkov.godfather.main.domain.keyboard.simple;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.Singular;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Строка в меню клавиатуры {@link SimpleKeyBoard}.
* Строка в меню клавиатуры {@link dev.struchkov.godfather.main.domain.keyboard.KeyBoard}.
*
* @author upagge [08/07/2019]
*/
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class SimpleKeyBoardLine implements KeyBoardLine {
/**
* Кнопки в строке.
*/
@Singular
protected List<KeyBoardButton> buttons;
public SimpleKeyBoardLine(List<KeyBoardButton> buttons) {
this.buttons = buttons;
}
private SimpleKeyBoardLine(Builder builder) {
buttons = builder.buttons;
}
public static SimpleKeyBoardLine simpleLine(KeyBoardButton... keyBoardButton) {
return new SimpleKeyBoardLine(Arrays.stream(keyBoardButton).toList());
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private List<KeyBoardButton> buttons = new ArrayList<>();
private Builder() {
}
public Builder buttons(List<KeyBoardButton> val) {
buttons = val;
return this;
}
public Builder button(KeyBoardButton val) {
buttons.add(val);
return this;
}
public SimpleKeyBoardLine build() {
return new SimpleKeyBoardLine(this);
}
}
}