Skip to content

Commit

Permalink
Global shelf
Browse files Browse the repository at this point in the history
  • Loading branch information
Serwios committed Aug 10, 2024
1 parent 345d496 commit 1f77890
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/archivision/community/model/Reply.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum Reply {
DISLIKE("-"),
SETTINGS("Налаштування"),
STATS("Статистика"),
BACK("Завершити");
BACK("Завершити"),
SEARCH("Пошук");

private final String replyOption;
Reply(String replyOption) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Consumer<Long>> 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
Expand All @@ -74,25 +76,17 @@ public boolean shouldValidateInput() {
return true;
}

private boolean isLiked(String msg) {
return "+".equals(msg);
}

private boolean isDisliked(String msg) {
return "-".equals(msg);
}

@Override
public Set<String> getOptions() {
return Stream.of(Reply.LIKE, Reply.DISLIKE, Reply.SETTINGS, Reply.STATS)
return Stream.of(LIKE, DISLIKE, SETTINGS, STATS)
.map(Reply::toString)
.collect(toSet());
}

@Override
public void onStateChanged(Long chatId) {
profileSender.showProfile(chatId);
messageSender.sendMsgWithMarkup(chatId, "Пошук", keyboardBuilder.matchButtons());
messageSender.sendMsgWithMarkup(chatId, SEARCH.toString(), keyboardBuilder.matchButtons());
profileSender.sendNextProfile(chatId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

}
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1f77890

Please sign in to comment.