перенес реализации клавиатур

This commit is contained in:
Struchkov Mark 2023-04-06 20:24:05 +03:00
parent 369edb829c
commit 70b06f0964
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
17 changed files with 174 additions and 43 deletions

View File

@ -38,7 +38,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.56</godfather.core.ver>
<godfather.core.ver>0.0.57</godfather.core.ver>
<!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
<telegrambots.ver>6.3.0</telegrambots.ver>

View File

@ -1,15 +1,15 @@
package dev.struchkov.godfather.telegram.main.core.util;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine;
import dev.struchkov.godfather.telegram.domain.keyboard.button.SimpleButton;
import java.util.Arrays;
import java.util.List;
import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton;
import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine;
import static dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine.keyBoardLine;
import static dev.struchkov.godfather.telegram.domain.keyboard.button.SimpleButton.simpleButton;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
public final class InlineKeyBoards {
@ -25,7 +25,7 @@ public final class InlineKeyBoards {
* Возвращает клавиатуру формата 1х2, с кнопками "Да | Нет"
*/
public static InlineKeyBoard lineYesOrNo() {
return InlineKeyBoard.inlineKeyBoard(simpleLine(YES_BUTTON, NO_BUTTON));
return InlineKeyBoard.inlineKeyBoard(keyBoardLine(YES_BUTTON, NO_BUTTON));
}
/**
@ -37,7 +37,7 @@ public final class InlineKeyBoards {
public static InlineKeyBoard verticalMenuString(List<String> labelButtons) {
final InlineKeyBoard.InlineKeyBoardBuilder keyBoard = InlineKeyBoard.builder();
for (String labelButton : labelButtons) {
keyBoard.line(simpleLine(simpleButton(labelButton, labelButton)));
keyBoard.line(keyBoardLine(simpleButton(labelButton, labelButton)));
}
return keyBoard.build();
}
@ -128,7 +128,7 @@ public final class InlineKeyBoards {
public static InlineKeyBoard verticalMenuButton(KeyBoardButton... buttons) {
final InlineKeyBoard.InlineKeyBoardBuilder keyBoard = InlineKeyBoard.builder();
for (KeyBoardButton simpleButton : buttons) {
keyBoard.line(simpleLine(simpleButton));
keyBoard.line(keyBoardLine(simpleButton));
}
return keyBoard.build();
}

View File

@ -1,8 +1,8 @@
package dev.struchkov.godfather.telegram.main.core.util;
import dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine;
import dev.struchkov.godfather.telegram.domain.keyboard.MarkupKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine;
import dev.struchkov.godfather.telegram.domain.keyboard.button.SimpleButton;
import java.util.Arrays;
import java.util.List;

View File

@ -49,10 +49,18 @@ public class UnitTrigger {
return predicate(mail -> Attachments.findFirstCommand(mail.getAttachments()).isPresent());
}
public static UniPredicate<Mail> isClickButton() {
public static UniPredicate<Mail> isButtonClick() {
return predicate(mail -> Attachments.findFirstButtonClick(mail.getAttachments()).isPresent());
}
public static UniPredicate<Mail> isButtonClickArg(String argType) {
return predicate(
mail -> Attachments.findFirstButtonClick(mail.getAttachments())
.flatMap(click -> click.getArgByType(argType))
.isPresent()
);
}
public static UniPredicate<Mail> isLinks() {
return predicate(mail -> Attachments.findFirstLink(mail.getAttachments()).isPresent());
}

View File

@ -27,7 +27,7 @@ public class UnitTrigger {
};
}
public static Predicate<Mail> isClickButton() {
public static Predicate<Mail> isButtonClick() {
return mail -> Attachments.findFirstButtonClick(mail.getAttachments()).isPresent();
}
@ -35,4 +35,10 @@ public class UnitTrigger {
return mail -> Attachments.findFirstLink(mail.getAttachments()).isPresent();
}
public static Predicate<Mail> isButtonClickArg(String argType) {
return mail -> Attachments.findFirstButtonClick(mail.getAttachments())
.flatMap(click -> click.getArgByType(argType))
.isPresent();
}
}

View File

@ -2,13 +2,13 @@ package dev.struchkov.godfather.telegram.domain;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine;
import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine;
import dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton;
import static dev.struchkov.godfather.telegram.domain.keyboard.button.SimpleButton.simpleButton;
public final class UnitPaginationUtil {

View File

@ -0,0 +1,24 @@
package dev.struchkov.godfather.telegram.domain.attachment;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ButtonArg {
private String type;
private String value;
public static ButtonArg buttonArg(String type, String value) {
return new ButtonArg(type, value);
}
}

View File

@ -1,9 +1,7 @@
package dev.struchkov.godfather.telegram.domain.attachment;
import dev.struchkov.godfather.main.domain.content.Attachment;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Collection;
@ -23,7 +21,7 @@ public class ButtonClickAttachment extends Attachment {
*/
private String messageId;
private String rawCallBackData;
private Map<String, Arg> args = new HashMap<>();
private Map<String, ButtonArg> args = new HashMap<>();
public ButtonClickAttachment() {
super(TelegramAttachmentType.BUTTON_CLICK.name());
@ -31,32 +29,21 @@ public class ButtonClickAttachment extends Attachment {
public void addClickArg(String type, String value) {
isNotNull(type, value);
args.put(type, new Arg(type, value));
args.put(type, ButtonArg.buttonArg(type, value));
}
public Optional<Arg> getArgByType(String type) {
public Optional<ButtonArg> getArgByType(String type) {
isNotNull(type);
return Optional.ofNullable(args.get(type));
}
public Arg getArgByTypeOrThrow(String type) {
public ButtonArg getArgByTypeOrThrow(String type) {
isNotNull(type);
return Optional.of(args.get(type)).orElseThrow(notFoundException("Аргумент типа {0} не найден.", type));
}
public Collection<Arg> getClickArgs() {
public Collection<ButtonArg> getClickArgs() {
return args.values();
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class Arg {
private String type;
private String value;
}
}

View File

@ -5,8 +5,8 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.telegram.domain.keyboard.button.ContactButton;
import dev.struchkov.godfather.telegram.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.telegram.domain.keyboard.button.UrlButton;
import dev.struchkov.godfather.telegram.domain.keyboard.button.WebAppButton;

View File

@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine;
import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine;
import dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine;
import java.io.IOException;

View File

@ -13,7 +13,8 @@ import lombok.Singular;
import java.util.List;
import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine;
import static dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine.keyBoardLine;
@Getter
@Setter
@ -36,7 +37,7 @@ public class InlineKeyBoard implements KeyBoard {
}
public static InlineKeyBoard inlineKeyBoard(KeyBoardButton... buttons) {
return builder().line(simpleLine(buttons)).build();
return builder().line(keyBoardLine(buttons)).build();
}
@Override

View File

@ -14,7 +14,7 @@ import lombok.Singular;
import java.util.List;
import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine;
import static dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine.keyBoardLine;
import static dev.struchkov.haiti.utils.Checker.checkEmpty;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
@ -55,7 +55,7 @@ public class MarkupKeyBoard implements KeyBoard {
public static MarkupKeyBoard markupKeyBoard(KeyBoardButton... buttons) {
final MarkupKeyBoardBuilder builder = new MarkupKeyBoardBuilder();
for (KeyBoardButton button : buttons) {
builder.line(simpleLine(button));
builder.line(keyBoardLine(button));
}
return builder.build();
}

View File

@ -0,0 +1,38 @@
package dev.struchkov.godfather.telegram.domain.keyboard;
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.Arrays;
import java.util.List;
/**
* Строка в меню клавиатуры {@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 static SimpleKeyBoardLine keyBoardLine(KeyBoardButton... keyBoardButton) {
return new SimpleKeyBoardLine(Arrays.stream(keyBoardButton).toList());
}
}

View File

@ -0,0 +1,67 @@
package dev.struchkov.godfather.telegram.domain.keyboard.button;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.telegram.domain.attachment.ButtonArg;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Arrays;
import java.util.stream.Collectors;
import static dev.struchkov.haiti.utils.Inspector.Utils.nullPointer;
import static dev.struchkov.haiti.utils.Inspector.isNotNull;
/**
* Абстрактная сущность кнопки для клавиатуры.
*
* @author upagge [08/07/2019]
*/
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode
public class SimpleButton implements KeyBoardButton {
public static final String TYPE = "SIMPLE";
/**
* Надпись на кнопке.
*/
protected String label;
/**
* Данные, которые возвращаются при нажатии.
*/
protected String callbackData;
protected SimpleButton(String label, String callbackData) {
this.label = label;
this.callbackData = callbackData;
}
public static SimpleButton simpleButton(String label, String callbackData) {
isNotNull(label, nullPointer("label"));
return new SimpleButton(label, callbackData);
}
public static SimpleButton simpleButton(String label, ButtonArg... args) {
isNotNull(label, nullPointer("label"));
return new SimpleButton(
label, Arrays.stream(args)
.map(buttonArg -> buttonArg.getType() + ":" + buttonArg.getValue())
.collect(Collectors.joining("[", "]", ";"))
);
}
public static SimpleButton simpleButton(String label) {
isNotNull(label, nullPointer("label"));
return new SimpleButton(label, null);
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -10,8 +10,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine;
import static dev.struchkov.godfather.telegram.domain.UnitPaginationUtil.navigableLine;
import static dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine.keyBoardLine;
public class UnitPage<T> {
@ -85,7 +85,7 @@ public class UnitPage<T> {
}
public UnitPage<T> additionLine(KeyBoardButton button) {
additionalLines.add(simpleLine(button));
additionalLines.add(keyBoardLine(button));
return this;
}

View File

@ -10,8 +10,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine;
import static dev.struchkov.godfather.telegram.domain.UnitPaginationUtil.navigableLine;
import static dev.struchkov.godfather.telegram.domain.keyboard.SimpleKeyBoardLine.keyBoardLine;
public class UnitPage<T> {
@ -85,7 +85,7 @@ public class UnitPage<T> {
}
public UnitPage<T> additionLine(KeyBoardButton button) {
additionalLines.add(simpleLine(button));
additionalLines.add(keyBoardLine(button));
return this;
}

View File

@ -3,10 +3,10 @@ package dev.struchkov.godfather.telegram.main.sender.util;
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 dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.MarkupKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.button.ContactButton;
import dev.struchkov.godfather.telegram.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.telegram.domain.keyboard.button.UrlButton;
import dev.struchkov.godfather.telegram.domain.keyboard.button.WebAppButton;
import dev.struchkov.haiti.context.exception.ConvertException;