reminder-bot/bot/keyboards/pagination.py

94 lines
2.5 KiB
Python

"""Pagination keyboards for reminder lists."""
from typing import List
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.filters.callback_data import CallbackData
from bot.db.models import Reminder
class PaginationCallback(CallbackData, prefix="page"):
"""Callback for pagination."""
action: str # prev, next, select
page: int
reminder_id: int = 0
def get_reminders_list_keyboard(
reminders: List[Reminder],
page: int = 0,
page_size: int = 5,
) -> InlineKeyboardMarkup:
"""
Get keyboard with paginated reminders list.
Args:
reminders: List of all reminders
page: Current page number (0-indexed)
page_size: Number of items per page
Returns:
InlineKeyboardMarkup with reminder buttons and navigation
"""
total_pages = (len(reminders) + page_size - 1) // page_size
start_idx = page * page_size
end_idx = min(start_idx + page_size, len(reminders))
buttons = []
# Add buttons for reminders on current page
for reminder in reminders[start_idx:end_idx]:
status_icon = "" if reminder.is_active else ""
button_text = f"{status_icon} #{reminder.id} {reminder.text[:30]}..."
buttons.append([
InlineKeyboardButton(
text=button_text,
callback_data=PaginationCallback(
action="select",
page=page,
reminder_id=reminder.id
).pack()
)
])
# Add navigation buttons if needed
nav_buttons = []
if page > 0:
nav_buttons.append(
InlineKeyboardButton(
text="⬅️ Назад",
callback_data=PaginationCallback(
action="prev",
page=page - 1
).pack()
)
)
# Page counter
if total_pages > 1:
nav_buttons.append(
InlineKeyboardButton(
text=f"{page + 1}/{total_pages}",
callback_data="noop"
)
)
if page < total_pages - 1:
nav_buttons.append(
InlineKeyboardButton(
text="Далее ➡️",
callback_data=PaginationCallback(
action="next",
page=page + 1
).pack()
)
)
if nav_buttons:
buttons.append(nav_buttons)
keyboard = InlineKeyboardMarkup(inline_keyboard=buttons)
return keyboard