Рефакторинг клавиатур

This commit is contained in:
Struchkov Mark 2022-05-26 02:19:22 +03:00
parent 89b2688e52
commit 0c3b098e55
12 changed files with 67 additions and 34 deletions

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.5</version> <version>0.0.6</version>
</parent> </parent>
<artifactId>bot-context</artifactId> <artifactId>bot-context</artifactId>

View File

@ -1,8 +1,6 @@
package dev.struchkov.godfather.context.domain; package dev.struchkov.godfather.context.domain;
import dev.struchkov.godfather.context.domain.content.Message;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoard; import dev.struchkov.godfather.context.domain.keyboard.KeyBoard;
import dev.struchkov.godfather.context.service.usercode.ProcessingData;
/** /**
* Контейнер, которые содержит данные, которые будут отправлены пользователю как ответ на его запрос. * Контейнер, которые содержит данные, которые будут отправлены пользователю как ответ на его запрос.
@ -11,30 +9,33 @@ import dev.struchkov.godfather.context.service.usercode.ProcessingData;
*/ */
public class BoxAnswer { public class BoxAnswer {
/**
* Клавиатура - меню.
*/
private final KeyBoard keyBoard;
/**
* Флаг означающий, что надо перезаписать наше последнее отправленное сообщение, вместо отправки нового.
*/
private final boolean replace;
/** /**
* Обычное текстовое сообщение. * Обычное текстовое сообщение.
*/ */
private String message; private String message;
/**
* Клавиатура - меню.
*/
private KeyBoard keyBoard;
private boolean replace;
private BoxAnswer(Builder builder) { private BoxAnswer(Builder builder) {
message = builder.message; message = builder.message;
keyBoard = builder.keyBoard; keyBoard = builder.keyBoard;
replace = builder.replace; replace = builder.replace;
} }
public static BoxAnswer of(String message) { public static BoxAnswer boxAnswer(String message) {
return BoxAnswer.builder().message(message).build(); return BoxAnswer.builder().message(message).build();
} }
public static <T extends Message> ProcessingData<T> boxAnswer(String messageText) { public static BoxAnswer boxAnswer(String messageText, KeyBoard keyBoard) {
return message -> of(messageText); return BoxAnswer.builder().message(messageText).keyBoard(keyBoard).build();
} }
public static Builder builder() { public static Builder builder() {
@ -66,7 +67,6 @@ public class BoxAnswer {
'}'; '}';
} }
public static final class Builder { public static final class Builder {
private String message; private String message;
private KeyBoard keyBoard; private KeyBoard keyBoard;
@ -94,4 +94,5 @@ public class BoxAnswer {
return new BoxAnswer(this); return new BoxAnswer(this);
} }
} }
} }

View File

@ -28,11 +28,11 @@ public class SimpleButton implements KeyBoardButton {
this.callbackData = callbackData; this.callbackData = callbackData;
} }
public static SimpleButton of(@NotNull String label, @NotNull String callbackData) { public static SimpleButton simpleButton(@NotNull String label, @NotNull String callbackData) {
return new SimpleButton(label, callbackData); return new SimpleButton(label, callbackData);
} }
public static SimpleButton of(@NotNull String label) { public static SimpleButton simpleButton(@NotNull String label) {
return new SimpleButton(label, label); return new SimpleButton(label, label);
} }

View File

@ -29,7 +29,7 @@ public class SimpleKeyBoard implements KeyBoard {
lines = builder.lines; lines = builder.lines;
} }
public static SimpleKeyBoard single(KeyBoardLine line) { public static SimpleKeyBoard simpleKeyboard(KeyBoardLine line) {
return new SimpleKeyBoard(List.of(line)); return new SimpleKeyBoard(List.of(line));
} }
@ -37,8 +37,8 @@ public class SimpleKeyBoard implements KeyBoard {
return new Builder(); return new Builder();
} }
public SimpleKeyBoard single(KeyBoardButton keyBoardButton) { public SimpleKeyBoard simpleKeyboard(KeyBoardButton keyBoardButton) {
return single(SimpleKeyBoardLine.single(keyBoardButton)); return simpleKeyboard(SimpleKeyBoardLine.simpleLine(keyBoardButton));
} }
@Override @Override

View File

@ -26,7 +26,7 @@ public class SimpleKeyBoardLine implements KeyBoardLine {
buttons = builder.buttons; buttons = builder.buttons;
} }
public static SimpleKeyBoardLine single(KeyBoardButton keyBoardButton) { public static SimpleKeyBoardLine simpleLine(KeyBoardButton keyBoardButton) {
return new SimpleKeyBoardLine(List.of(keyBoardButton)); return new SimpleKeyBoardLine(List.of(keyBoardButton));
} }

View File

@ -0,0 +1,10 @@
package dev.struchkov.godfather.context.service.usercode;
import dev.struchkov.godfather.context.domain.BoxAnswer;
import dev.struchkov.godfather.context.domain.content.Message;
public interface MessageFunction<M extends Message> {
void build(M message, BoxAnswer.Builder builder);
}

View File

@ -14,8 +14,8 @@ import java.util.List;
*/ */
public class KeyBoards { public class KeyBoards {
public static final SimpleButton YES_BUTTON = SimpleButton.of("Да", "{\"button\": \"yes\"}"); public static final SimpleButton YES_BUTTON = SimpleButton.simpleButton("Да", "{\"button\": \"yes\"}");
public static final SimpleButton NO_BUTTON = SimpleButton.of("Нет", "{\"button\": \"no\"}"); public static final SimpleButton NO_BUTTON = SimpleButton.simpleButton("Нет", "{\"button\": \"no\"}");
private KeyBoards() { private KeyBoards() {
throw new IllegalStateException(); throw new IllegalStateException();
@ -41,7 +41,7 @@ public class KeyBoards {
public static SimpleKeyBoard verticalMenuString(List<String> labelButtons) { public static SimpleKeyBoard verticalMenuString(List<String> labelButtons) {
final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.build(); final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.build();
for (String labelButton : labelButtons) { for (String labelButton : labelButtons) {
final SimpleButton simpleButton = SimpleButton.of(labelButton, "{\"button\": \"" + labelButton + "\"}"); final SimpleButton simpleButton = SimpleButton.simpleButton(labelButton, "{\"button\": \"" + labelButton + "\"}");
keyBoard.line(SimpleKeyBoardLine.builder().button(simpleButton).build()); keyBoard.line(SimpleKeyBoardLine.builder().button(simpleButton).build());
} }
return keyBoard.build(); return keyBoard.build();
@ -79,7 +79,7 @@ public class KeyBoards {
SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder(); SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder();
for (int i = 0; i <= labelButton.size() - 1; i++) { for (int i = 0; i <= labelButton.size() - 1; i++) {
String label = labelButton.get(i); String label = labelButton.get(i);
keyBoardLine.button(SimpleButton.of(label)); keyBoardLine.button(SimpleButton.simpleButton(label));
if (flag) { if (flag) {
if (i == labelButton.size() - 1) { if (i == labelButton.size() - 1) {
keyBoard.line(keyBoardLine.build()); keyBoard.line(keyBoardLine.build());

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.5</version> <version>0.0.6</version>
</parent> </parent>
<artifactId>bot-core</artifactId> <artifactId>bot-core</artifactId>

View File

@ -61,7 +61,7 @@ public class GeneralAutoResponder<T extends Message> extends TimerTask {
this.modifiables = modifiables; this.modifiables = modifiables;
} }
protected void initActionUnit(String typeUnit, ActionUnit<? super MainUnit, T> actionUnit) { public void initActionUnit(String typeUnit, ActionUnit<? super MainUnit, T> actionUnit) {
if (!actionUnitMap.containsKey(typeUnit)) { if (!actionUnitMap.containsKey(typeUnit)) {
actionUnitMap.put(typeUnit, actionUnit); actionUnitMap.put(typeUnit, actionUnit);
} else { } else {

View File

@ -5,11 +5,13 @@ import dev.struchkov.godfather.context.domain.content.Message;
import dev.struchkov.godfather.context.exception.UnitConfigException; import dev.struchkov.godfather.context.exception.UnitConfigException;
import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.context.service.sender.Sending;
import dev.struchkov.godfather.context.service.usercode.Insert; import dev.struchkov.godfather.context.service.usercode.Insert;
import dev.struchkov.godfather.context.service.usercode.MessageFunction;
import dev.struchkov.godfather.context.service.usercode.ProcessingData; import dev.struchkov.godfather.context.service.usercode.ProcessingData;
import dev.struchkov.godfather.core.utils.TypeUnit; import dev.struchkov.godfather.core.utils.TypeUnit;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static dev.struchkov.haiti.utils.Inspector.isNotNull; import static dev.struchkov.haiti.utils.Inspector.isNotNull;
@ -29,12 +31,12 @@ public class AnswerText<M extends Message> extends MainUnit {
/** /**
* Информация, которую необходимо вставить вместо маркеров в строку ответа. * Информация, которую необходимо вставить вместо маркеров в строку ответа.
*/ */
private Insert insert; private final Insert insert;
/** /**
* Объект нестандартной отправки ответа. * Объект нестандартной отправки ответа.
*/ */
private Sending sending; private final Sending sending;
private AnswerText(Builder<M> builder) { private AnswerText(Builder<M> builder) {
super(builder.keyWords, builder.phrase, builder.pattern, builder.matchThreshold, builder.priority, builder.nextUnits, builder.activeType, TypeUnit.TEXT); super(builder.keyWords, builder.phrase, builder.pattern, builder.matchThreshold, builder.priority, builder.nextUnits, builder.activeType, TypeUnit.TEXT);
@ -76,11 +78,31 @@ public class AnswerText<M extends Message> extends MainUnit {
private UnitActiveType activeType; private UnitActiveType activeType;
private Builder() { private Builder() {
} }
public Builder<M> boxAnswer(ProcessingData<M> boxAnswer) { public Builder<M> message(ProcessingData<M> message) {
this.boxAnswer = boxAnswer; this.boxAnswer = message;
return this;
}
public Builder<M> message(MessageFunction<M> function) {
this.boxAnswer = message -> {
final BoxAnswer.Builder builder = BoxAnswer.builder();
function.build(message, builder);
return builder.build();
};
return this;
}
public Builder<M> boxAnswer(Consumer<BoxAnswer.Builder> boxAnswer) {
final BoxAnswer.Builder boxAnswerBuilder = BoxAnswer.builder();
boxAnswer.accept(boxAnswerBuilder);
this.boxAnswer = message -> boxAnswerBuilder.build();
return this;
}
public Builder<M> boxAnswer(BoxAnswer boxAnswer) {
this.boxAnswer = message -> boxAnswer;
return this; return this;
} }

View File

@ -40,7 +40,7 @@ public class AnswerValidityAction implements ActionUnit<AnswerValidity, Message>
.clearKeyWords().keyWords(WORDS_YES_NO) .clearKeyWords().keyWords(WORDS_YES_NO)
.build(); .build();
return AnswerText.builder() return AnswerText.builder()
.boxAnswer(mes -> clarification.getQuestion()) .message(mes -> clarification.getQuestion())
.nextUnit(newValidity) .nextUnit(newValidity)
.build(); .build();
} }

View File

@ -6,7 +6,7 @@
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.5</version> <version>0.0.6</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -32,7 +32,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.ver>0.0.5</godfather.ver> <godfather.ver>0.0.6</godfather.ver>
<godfather.context.ver>${godfather.ver}</godfather.context.ver> <godfather.context.ver>${godfather.ver}</godfather.context.ver>
<godfather.core.ver>${godfather.ver}</godfather.core.ver> <godfather.core.ver>${godfather.ver}</godfather.core.ver>