fix(tgbot-front): start fixing navigation callback processing function
This commit is contained in:
parent
b368ecc43b
commit
d69f5fcddf
1 changed files with 85 additions and 49 deletions
|
|
@ -85,56 +85,66 @@ void BotHandlers::processCallbackImpl(TgBot::CallbackQuery::Ptr query) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<HandlerResult, UserContext> BotHandlers::newStateNavigation(const TgBot::CallbackQuery::Ptr& query, const UserContext& ctx) {
|
void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query) {
|
||||||
const std::string& data = query->data;
|
int64_t userId = query->from->id;
|
||||||
switch (ctx.state) {
|
auto it = userContexts.find(userId);
|
||||||
case UserState::MAIN_MENU:
|
if (it == userContexts.end()) {
|
||||||
if(data == BotConstants::Callback::MY_TITLES) {
|
// TODO: log
|
||||||
UserContext newCtx{.state = UserState::VIEWING_MY_TITLES, .cursor = BotConstants::CURSOR_NOT_INIT};
|
std::cout << "Error: Не нашел пользователя " << userId;
|
||||||
HandlerResult result = returnMyTitles(query->from->id);
|
return;
|
||||||
|
|
||||||
return {result, newCtx};
|
|
||||||
}
|
|
||||||
case UserState::VIEWING_MY_TITLES:
|
|
||||||
if(data == BotConstants::Callback::LIST_PREV) {
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (data == BotConstants::Callback::LIST_NEXT) {
|
|
||||||
|
|
||||||
}
|
|
||||||
case UserState::AWAITING_TITLE_NAME:
|
|
||||||
if(data == BotConstants::Callback::LIST_PREV) {
|
|
||||||
|
|
||||||
}
|
|
||||||
case UserState::VIEWING_TITLE_PAGE:
|
|
||||||
if(data == BotConstants::Callback::LIST_PREV) {
|
|
||||||
|
|
||||||
}
|
|
||||||
case UserState::AWAITING_REVIEW:
|
|
||||||
if(data == BotConstants::Callback::LIST_PREV) {
|
|
||||||
|
|
||||||
}
|
|
||||||
case UserState::VIEWING_REVIEW_LIST:
|
|
||||||
if(data == BotConstants::Callback::LIST_PREV) {
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (data == BotConstants::Callback::LIST_NEXT) {
|
|
||||||
|
|
||||||
}
|
|
||||||
case UserState::VIEWING_REVIEW:
|
|
||||||
if(data == BotConstants::Callback::LIST_PREV) {
|
|
||||||
|
|
||||||
}
|
|
||||||
case UserState::VIEWING_DESCRIPTION:
|
|
||||||
if(data == BotConstants::Callback::LIST_PREV) {
|
|
||||||
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
UserContext newCtx{.state = UserState::ERROR, .cursor = BotConstants::CURSOR_NOT_INIT};
|
|
||||||
HandlerResult result = {"", nullptr};
|
|
||||||
|
|
||||||
return {result, newCtx};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UserContext& ctx = it->second;
|
||||||
|
const auto& current = ctx.history.back(); // текущий экран
|
||||||
|
const std::string& data = query->data;
|
||||||
|
|
||||||
|
// Пагинация (в списках)
|
||||||
|
if ((data == BotConstants::Callback::LIST_PREV || data == BotConstants::Callback::LIST_NEXT)
|
||||||
|
&& (current.state == UserState::VIEWING_MY_TITLES || current.state == UserState::VIEWING_REVIEW_LIST ||
|
||||||
|
current.state == UserState::VIEWING_FOUND_TITLES)) {
|
||||||
|
|
||||||
|
int64_t newPayload = current.payload;
|
||||||
|
if (data == BotConstants::Callback::LIST_PREV && newPayload > 0) {
|
||||||
|
reducePayload(newPayload, current.state);
|
||||||
|
} else if (data == BotConstants::Callback::LIST_NEXT) {
|
||||||
|
increasePayload(newPayload, current.state);
|
||||||
|
} else {
|
||||||
|
if (data == BotConstants::Callback::LIST_PREV) {
|
||||||
|
std::cout << "Error: navigation:prev callback for 1st page" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// TODO: log
|
||||||
|
std::cout << "Error: navigation:prev unknown error" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.history.back().payload = newPayload;
|
||||||
|
|
||||||
|
auto result = renderCurrent(ctx);
|
||||||
|
editMessage(query, result);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Обработка back по интерфейсу
|
||||||
|
if (data == BotConstants::Callback::NAV_BACK) {
|
||||||
|
if (!popState(ctx)) {
|
||||||
|
botApi.answerCallbackQuery(query->id, "Некуда возвращаться", true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto result = renderCurrent(ctx);
|
||||||
|
editMessage(query, result);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Переходы вперёд (push)
|
||||||
|
auto newStepOpt = computeNextStep(query, current);
|
||||||
|
if (!newStepOpt.has_value()) {
|
||||||
|
sendError(query->message->chat->id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.history.push_back(*newStepOpt);
|
||||||
|
auto result = renderCurrent(ctx);
|
||||||
|
editMessage(query, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BotHandlers::pushState(UserContext& ctx, UserState newState, int64_t payload) {
|
void BotHandlers::pushState(UserContext& ctx, UserState newState, int64_t payload) {
|
||||||
|
|
@ -146,3 +156,29 @@ bool BotHandlers::popState(UserContext& ctx) {
|
||||||
ctx.history.pop_back();
|
ctx.history.pop_back();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BotHandlers::reducePayload(int64_t& payload, const UserState curState) {
|
||||||
|
if (curState == UserState::VIEWING_MY_TITLES ||
|
||||||
|
curState == UserState::VIEWING_FOUND_TITLES) {
|
||||||
|
payload -= BotConstants::DISP_TITLES_NUM;
|
||||||
|
} else if (curState == UserState::VIEWING_REVIEW_LIST) {
|
||||||
|
payload -= BotConstants::DISP_REVIEW_NUM;
|
||||||
|
} else {
|
||||||
|
// TODO: log
|
||||||
|
payload = BotConstants::NULL_PAYLOAD;
|
||||||
|
std::cerr << "Error: reducePayload" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BotHandlers::increasePayload(int64_t& payload, const UserState curState) {
|
||||||
|
if (curState == UserState::VIEWING_MY_TITLES ||
|
||||||
|
curState == UserState::VIEWING_FOUND_TITLES) {
|
||||||
|
payload += BotConstants::DISP_TITLES_NUM;
|
||||||
|
} else if (curState == UserState::VIEWING_REVIEW_LIST) {
|
||||||
|
payload += BotConstants::DISP_REVIEW_NUM;
|
||||||
|
} else {
|
||||||
|
// TODO: log
|
||||||
|
payload = BotConstants::NULL_PAYLOAD;
|
||||||
|
std::cerr << "Error: increasePayload" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue