Skip to content

Commit

Permalink
Merge pull request #346 from Tishka17/feature/calendar_config
Browse files Browse the repository at this point in the history
calendar settings per event
  • Loading branch information
Tishka17 authored Dec 26, 2023
2 parents a162ed6 + c67b182 commit edb9e7f
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 72 deletions.
64 changes: 54 additions & 10 deletions example/mega/bot_dialogs/calendar.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from datetime import date
from typing import Dict

from aiogram import F
from aiogram.types import CallbackQuery
from babel.dates import get_day_names, get_month_names

from aiogram_dialog import (
Dialog, Window, DialogManager,
Dialog, Window, DialogManager, ChatEvent,
)
from aiogram_dialog.widgets.kbd import (
Calendar, CalendarScope, ManagedCalendar, SwitchTo,
)
from aiogram_dialog.widgets.kbd.calendar_kbd import (
CalendarDaysView, CalendarMonthView, CalendarScopeView, CalendarYearsView,
DATE_TEXT, TODAY_TEXT,
)
from aiogram_dialog.widgets.markup.reply_keyboard import ReplyKeyboardFactory
from aiogram_dialog.widgets.text import Const, Text, Format
from . import states
from .common import MAIN_MENU_BUTTON

SELECTED_DAYS_KEY = "selected_dates"


class WeekDay(Text):
async def _render_text(self, data, manager: DialogManager) -> str:
Expand All @@ -28,6 +31,21 @@ async def _render_text(self, data, manager: DialogManager) -> str:
)[selected_date.weekday()].title()


class MarkedDay(Text):
def __init__(self, mark: str, other: Text):
super().__init__()
self.mark = mark
self.other = other

async def _render_text(self, data, manager: DialogManager) -> str:
current_date: date = data["date"]
serial_date = current_date.isoformat()
selected = manager.dialog_data.get(SELECTED_DAYS_KEY, [])
if serial_date in selected:
return self.mark
return await self.other.render_text(data, manager)


class Month(Text):
async def _render_text(self, data, manager: DialogManager) -> str:
selected_date: date = data["date"]
Expand All @@ -41,32 +59,54 @@ class CustomCalendar(Calendar):
def _init_views(self) -> Dict[CalendarScope, CalendarScopeView]:
return {
CalendarScope.DAYS: CalendarDaysView(
self._item_callback_data, self.config,
self._item_callback_data,
date_text=MarkedDay("🔴", DATE_TEXT),
today_text=MarkedDay("⭕", TODAY_TEXT),
header_text="~~~~~ " + Month() + " ~~~~~",
weekday_text=WeekDay(),
next_month_text=Month() + " >>",
prev_month_text="<< " + Month(),
),
CalendarScope.MONTHS: CalendarMonthView(
self._item_callback_data, self.config,
self._item_callback_data,
month_text=Month(),
header_text="~~~~~ " + Format("{date:%Y}") + " ~~~~~",
this_month_text="[" + Month() + "]",
),
CalendarScope.YEARS: CalendarYearsView(
self._item_callback_data, self.config,
self._item_callback_data,
),
}


async def on_date_selected(
callback: CallbackQuery, widget: ManagedCalendar,
async def on_date_clicked(
callback: ChatEvent, widget: ManagedCalendar,
manager: DialogManager,
selected_date: date,
selected_date: date, /
):
await callback.answer(str(selected_date))


async def on_date_selected(
callback: ChatEvent, widget: ManagedCalendar,
manager: DialogManager,
clicked_date: date, /
):
selected = manager.dialog_data.setdefault(SELECTED_DAYS_KEY, [])
serial_date = clicked_date.isoformat()
if serial_date in selected:
selected.remove(serial_date)
else:
selected.append(serial_date)


async def selection_getter(dialog_manager, **_):
selected = dialog_manager.dialog_data.get(SELECTED_DAYS_KEY, [])
return {
"selected": ", ".join(sorted(selected))
}


CALENDAR_MAIN_MENU_BUTTON = SwitchTo(
text=Const("Back"), id="back", state=states.Calendar.MAIN,
)
Expand All @@ -90,19 +130,23 @@ async def on_date_selected(
Const("Default calendar widget"),
Calendar(
id="cal",
on_click=on_date_selected,
on_click=on_date_clicked,
),
CALENDAR_MAIN_MENU_BUTTON,
state=states.Calendar.DEFAULT,
),
Window(
Const("Customized calendar widget"),
Const("Here we use custom text widgets to localize "),
Const("Here we use custom text widgets to localize "
"and store selection"),
Format("\nSelected: {selected}", when=F["selected"]),
Format("\nNo dates selected", when=~F["selected"]),
CustomCalendar(
id="cal",
on_click=on_date_selected,
),
CALENDAR_MAIN_MENU_BUTTON,
getter=selection_getter,
state=states.Calendar.CUSTOM,
),
)
Loading

0 comments on commit edb9e7f

Please sign in to comment.