diff --git a/src/main/java/com/archivision/community/model/Reply.java b/src/main/java/com/archivision/community/model/Reply.java index ac278f9..ced7fc7 100644 --- a/src/main/java/com/archivision/community/model/Reply.java +++ b/src/main/java/com/archivision/community/model/Reply.java @@ -12,7 +12,8 @@ public enum Reply { DISLIKE("-"), SETTINGS("Налаштування"), STATS("Статистика"), - BACK("Завершити"); + BACK("Завершити"), + SEARCH("Пошук"); private final String replyOption; Reply(String replyOption) { diff --git a/src/main/java/com/archivision/community/state/impl/MatchStateHandler.java b/src/main/java/com/archivision/community/state/impl/MatchStateHandler.java index 5d64bc0..ea449fe 100644 --- a/src/main/java/com/archivision/community/state/impl/MatchStateHandler.java +++ b/src/main/java/com/archivision/community/state/impl/MatchStateHandler.java @@ -15,43 +15,45 @@ import org.springframework.stereotype.Component; import org.telegram.telegrambots.meta.api.objects.Message; +import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import java.util.stream.Stream; +import static com.archivision.community.model.Reply.*; import static java.util.stream.Collectors.toSet; @Component @Slf4j public class MatchStateHandler extends AbstractStateHandler implements WithReplyOptions { - private final UserInteractionService userInteractionService; private final ProfileSender profileSender; + private final Map> messageHandlerStrategy; public MatchStateHandler(InputValidator inputValidator, UserService userService, MessageSender messageSender, KeyboardBuilderService keyboardBuilder, UserCache userCache, UserInteractionService userLikeService, ProfileSender profileSender) { super(inputValidator, userService, messageSender, keyboardBuilder, userCache); - this.userInteractionService = userLikeService; this.profileSender = profileSender; + + this.messageHandlerStrategy = Map.of( + LIKE.toString(), userLikeService::handleLikeAction, + DISLIKE.toString(), userLikeService::handleDislikeAction, + SETTINGS.toString(), chatId -> { + messageSender.sendMsgWithMarkup(chatId, SETTINGS.toString(), keyboardBuilder.subscriptions()); + userService.changeState(chatId, UserFlowState.SETTINGS); + }, + STATS.toString(), chatId -> { + messageSender.sendMsgWithMarkup(chatId, STATS.toString(), keyboardBuilder.backButton()); + userService.changeState(chatId, UserFlowState.STATS); + } + ); } @Override public void doHandle(Message message) { - Long chatId = message.getChatId(); - String messageText = message.getText(); - if (isLiked(messageText)) { - userInteractionService.handleLikeAction(chatId); - } - if (isDisliked(messageText)) { - userInteractionService.handleDislikeAction(chatId); - } - if (messageText.equals(Reply.SETTINGS.toString())) { - messageSender.sendMsgWithMarkup(chatId, "Налаштування", keyboardBuilder.subscriptions()); - userService.changeState(chatId, UserFlowState.SETTINGS); - } - if (messageText.equals(Reply.STATS.toString())) { - messageSender.sendMsgWithMarkup(chatId, "Статистика", keyboardBuilder.backButton()); - userService.changeState(chatId, UserFlowState.STATS); - } + messageHandlerStrategy + .get(message.getText()) + .accept(message.getChatId()); } @Override @@ -74,17 +76,9 @@ public boolean shouldValidateInput() { return true; } - private boolean isLiked(String msg) { - return "+".equals(msg); - } - - private boolean isDisliked(String msg) { - return "-".equals(msg); - } - @Override public Set getOptions() { - return Stream.of(Reply.LIKE, Reply.DISLIKE, Reply.SETTINGS, Reply.STATS) + return Stream.of(LIKE, DISLIKE, SETTINGS, STATS) .map(Reply::toString) .collect(toSet()); } @@ -92,7 +86,7 @@ public Set getOptions() { @Override public void onStateChanged(Long chatId) { profileSender.showProfile(chatId); - messageSender.sendMsgWithMarkup(chatId, "Пошук", keyboardBuilder.matchButtons()); + messageSender.sendMsgWithMarkup(chatId, SEARCH.toString(), keyboardBuilder.matchButtons()); profileSender.sendNextProfile(chatId); } } diff --git a/src/main/java/com/archivision/community/state/impl/SettingsStateHandler.java b/src/main/java/com/archivision/community/state/impl/SettingsStateHandler.java index 1658558..9ef549e 100644 --- a/src/main/java/com/archivision/community/state/impl/SettingsStateHandler.java +++ b/src/main/java/com/archivision/community/state/impl/SettingsStateHandler.java @@ -17,6 +17,8 @@ @Slf4j @Component public class SettingsStateHandler extends AbstractStateHandler implements Validatable { + private final SubscriptionService subscriptionService; + public SettingsStateHandler(InputValidator inputValidator, UserService userService, MessageSender messageSender, @@ -27,18 +29,14 @@ public SettingsStateHandler(InputValidator inputValidator, this.subscriptionService = subscriptionService; } - private final SubscriptionService subscriptionService; - @Override public void doHandle(Message message) { - String text = message.getText(); - Long chatId = message.getChatId(); log.info("message on settings state"); - formMarkupBasedOnDonationType(text, chatId); + formMarkupBasedOnDonationType(message.getText(), message.getChatId()); } private void formMarkupBasedOnDonationType(String subscriptionType, Long chatId) { - InlineKeyboardMarkup markup = keyboardBuilder.inlineBtn(subscriptionType, getPaymentUrl(chatId, subscriptionType)); + final InlineKeyboardMarkup markup = keyboardBuilder.inlineBtn(subscriptionType, getPaymentUrl(chatId, subscriptionType)); messageSender.sendMsgWithInline(chatId, markup); } @@ -64,6 +62,6 @@ public boolean isInputValid(Message message) { @Override public void onValidationError(Message message) { - + log.error("Validation error in state: {}. Message={}", getState(), message.getText()); } } diff --git a/src/main/java/com/archivision/community/state/impl/StatsStateHandler.java b/src/main/java/com/archivision/community/state/impl/StatsStateHandler.java index ad99a79..b2af8be 100644 --- a/src/main/java/com/archivision/community/state/impl/StatsStateHandler.java +++ b/src/main/java/com/archivision/community/state/impl/StatsStateHandler.java @@ -24,7 +24,7 @@ public class StatsStateHandler extends AbstractStateHandler implements Validatab Popularity index: %s """; - private UserStatsService userStatsService; + private final UserStatsService userStatsService; public StatsStateHandler(InputValidator inputValidator, UserService userService, MessageSender messageSender, KeyboardBuilderService keyboardBuilder, UserCache userCache, UserStatsService userStatsService) {