34 lines
1.3 KiB
Python
34 lines
1.3 KiB
Python
from typing import Any, Callable
|
|
|
|
import library_api
|
|
from dates import check_date
|
|
from tui.content_pane import ContentPane
|
|
from tui.dialogs import FormDialog, FormField
|
|
|
|
|
|
class LibraryPane(ContentPane):
|
|
reload_message = "Reloaded from the library"
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
self.border_title = f"[b]kitty[/b] {library_api.api_url()}/api/library"
|
|
|
|
def load_entries(self, load: Callable[[], list[dict]]) -> list[dict]:
|
|
try:
|
|
return load()
|
|
except (OSError, ValueError) as error:
|
|
self.notify(str(error), title="Library", severity="error", timeout=10)
|
|
return []
|
|
|
|
def edit_dates(self, title: str, entry: dict, save: Callable[[str | None, str | None], Any], focus_key: str):
|
|
fields = [
|
|
FormField("started", "Started (YYYY-MM-DD, empty to clear)", value=entry.get("started", ""), placeholder="2026-01-31"),
|
|
FormField("finished", "Finished (YYYY-MM-DD, empty to clear)", value=entry.get("finished", ""), placeholder="2026-02-14"),
|
|
]
|
|
|
|
def saved(values: dict | None):
|
|
if values is not None:
|
|
self.run_now(lambda: save(check_date(values["started"]), check_date(values["finished"])), f"Saved dates for {title}", focus_key)
|
|
|
|
self.app.push_screen(FormDialog(f"History for {title}", fields), saved)
|