Skip to content

Commit

Permalink
added select any hint
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Aug 23, 2024
1 parent e562598 commit f4c32e4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
11 changes: 11 additions & 0 deletions shvatka/tgbot/dialogs/level_scn/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from aiogram_dialog import Data, DialogManager
from aiogram_dialog.widgets.kbd import Button
from dataclass_factory import Factory
from dishka import AsyncContainer

from shvatka.core.models import dto
from shvatka.core.models.dto import scn
Expand All @@ -16,6 +17,7 @@
)
from shvatka.infrastructure.db.dao.holder import HolderDao
from shvatka.tgbot import states
from shvatka.tgbot.views.hint_sender import HintSender


def check_level_id(name_id: str) -> str:
Expand Down Expand Up @@ -142,6 +144,15 @@ async def start_edit_time_hint(c: CallbackQuery, widget: Any, manager: DialogMan
)


async def edit_single_hint(c: CallbackQuery, widget: Any, manager: DialogManager, hint_index: str):
dcf: Factory = manager.middleware_data["dcf"]
dishka: AsyncContainer = manager.middleware_data["dishka_container"]
hint = dcf.load(manager.start_data.get("time_hint"), scn.TimeHint)
hint_sender = await dishka.get(HintSender)
await hint_sender.send_hint(hint.hint[int(hint_index)], c.message.chat.id)



async def start_add_time_hint(c: CallbackQuery, button: Button, manager: DialogManager):
dcf: Factory = manager.middleware_data["dcf"]
hints = dcf.load(manager.dialog_data.get("time_hints", []), list[scn.TimeHint])
Expand Down
35 changes: 31 additions & 4 deletions shvatka/tgbot/dialogs/time_hint/dialogs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import MessageInput
from aiogram_dialog.widgets.kbd import Select, Button, Group, Back, Cancel
from aiogram_dialog.widgets.kbd import Select, Button, Group, Back, Cancel, SwitchTo, ScrollingGroup
from aiogram_dialog.widgets.text import Const, Format, Case, Jinja

from shvatka.tgbot import states
from .getters import get_available_times, get_hints
from .handlers import process_time_message, select_time, process_hint, on_finish, hint_on_start, hint_edit_on_start
from .handlers import process_time_message, select_time, process_hint, on_finish, hint_on_start, hint_edit_on_start, \
process_edit_time_message
from shvatka.tgbot.dialogs.preview_data import TIMES_PRESET
from ..level_scn.handlers import start_edit_time_hint, edit_single_hint

time_hint = Dialog(
Window(
Expand Down Expand Up @@ -62,9 +64,34 @@
"Подсказка выходящая в {{time}}:"
"{{hints | hints}}"
),
Cancel(text=Const("Вернуться, не нужна подсказка")),
SwitchTo(
Const("Изменить время"),
id="change_time",
state=states.TimeHintEditSG.time,
),
ScrollingGroup(
Select(
Jinja("{{item[1] | single_hint}}"),
id="hints",
item_id_getter=lambda x: x[0],
items="numerated_hints",
on_click=edit_single_hint,
),
id="hints_sg",
width=1,
height=10,
),
Cancel(text=Const("Вернуться, ничего не менять")),
getter=get_hints,
state=states.TimeHintEditSG.details,
),
Window(
Jinja(
"Введи новое время выхода подсказки"
),
MessageInput(func=process_edit_time_message),
getter=get_hints,
state=states.TimeHintEditSG.time,
),
on_start=hint_edit_on_start,
)
)
1 change: 1 addition & 0 deletions shvatka/tgbot/dialogs/time_hint/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def get_hints(dialog_manager: DialogManager, **_):
time_ = dialog_data["time"]
return {
"hints": hints,
"numerated_hints": list(enumerate(hints)),
"time": time_,
"has_hints": len(hints) > 0,
}
10 changes: 10 additions & 0 deletions shvatka/tgbot/dialogs/time_hint/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ async def select_time(c: CallbackQuery, widget: Any, manager: DialogManager, ite
await set_time(int(item_id), manager)


async def process_edit_time_message(m: Message, dialog_: Any, manager: DialogManager) -> None:
try:
time_ = int(m.text)
except ValueError:
await m.answer("Некорректный формат времени. Пожалуйста введите время в формате ЧЧ:ММ")
return
manager.dialog_data["time"] = time_
await manager.switch_to(states.TimeHintEditSG.details)


async def process_time_message(m: Message, dialog_: Any, manager: DialogManager) -> None:
try:
time_ = int(m.text)
Expand Down
1 change: 1 addition & 0 deletions shvatka/tgbot/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TimeHintSG(StatesGroup):

class TimeHintEditSG(StatesGroup):
details = State()
time = State()


class LevelListSG(StatesGroup):
Expand Down

0 comments on commit f4c32e4

Please sign in to comment.