Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Add AbortWidgetClick exception #325

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/aiogram_dialog/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ class InvalidWidget(DialogsError):

class InvalidWidgetType(InvalidWidget):
pass


class AbortWidgetClick(DialogsError):
pass
13 changes: 9 additions & 4 deletions src/aiogram_dialog/widgets/kbd/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from aiogram.types import CallbackQuery, InlineKeyboardButton

from aiogram_dialog.api.entities import ChatEvent
from aiogram_dialog.api.exceptions import AbortWidgetClick
from aiogram_dialog.api.protocols import DialogManager, DialogProtocol
from aiogram_dialog.widgets.common import ManagedWidget, WhenCondition
from aiogram_dialog.widgets.text import Case, Text
Expand Down Expand Up @@ -62,10 +63,14 @@ async def _process_item_callback(
) -> bool:
# remove prefix and cast "0" as False, "1" as True
checked = data != "0"
await self.on_click.process_event(
callback, self.managed(manager), manager,
)
await self.set_checked(callback, not checked, manager)
try:
await self.on_click.process_event(
callback, self.managed(manager), manager,
)
await self.set_checked(callback, not checked, manager)
except AbortWidgetClick:
pass

return True

def _is_text_checked(
Expand Down
14 changes: 9 additions & 5 deletions src/aiogram_dialog/widgets/kbd/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from aiogram.types import CallbackQuery, InlineKeyboardButton

from aiogram_dialog.api.entities import ChatEvent
from aiogram_dialog.api.exceptions import AbortWidgetClick
from aiogram_dialog.api.protocols import DialogManager, DialogProtocol
from aiogram_dialog.widgets.common import ManagedWidget, WhenCondition
from aiogram_dialog.widgets.text import Case, Text
Expand Down Expand Up @@ -165,11 +166,14 @@ async def _process_click(
manager: DialogManager,
item_id: str,
):
if self.on_item_click:
await self.on_item_click.process_event(
callback, select, manager, self.type_factory(item_id),
)
await self._on_click(callback, select, manager, str(item_id))
try:
if self.on_item_click:
await self.on_item_click.process_event(
callback, select, manager, self.type_factory(item_id),
)
await self._on_click(callback, select, manager, str(item_id))
except AbortWidgetClick:
pass

@abstractmethod
async def _on_click(
Expand Down