|
| 1 | +from textual.app import App, on |
| 2 | +from textual.containers import Grid, Horizontal, Vertical |
| 3 | +from textual.screen import Screen |
| 4 | +from textual.widgets import ( |
| 5 | + Button, |
| 6 | + DataTable, |
| 7 | + Footer, |
| 8 | + Header, |
| 9 | + Input, |
| 10 | + Label, |
| 11 | + Static, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class ContactsApp(App): |
| 16 | + CSS_PATH = "rpcontacts.tcss" |
| 17 | + BINDINGS = [ |
| 18 | + ("m", "toggle_dark", "Toggle dark mode"), |
| 19 | + ("a", "add", "Add"), |
| 20 | + ("d", "delete", "Delete"), |
| 21 | + ("c", "clear_all", "Clear All"), |
| 22 | + ("q", "request_quit", "Quit"), |
| 23 | + ] |
| 24 | + |
| 25 | + def __init__(self, db): |
| 26 | + super().__init__() |
| 27 | + self.db = db |
| 28 | + |
| 29 | + def compose(self): |
| 30 | + yield Header() |
| 31 | + contacts_list = DataTable(classes="contacts-list") |
| 32 | + contacts_list.focus() |
| 33 | + contacts_list.add_columns("Name", "Phone", "Email") |
| 34 | + contacts_list.cursor_type = "row" |
| 35 | + contacts_list.zebra_stripes = True |
| 36 | + add_button = Button("Add", variant="success", id="add") |
| 37 | + add_button.focus() |
| 38 | + buttons_panel = Vertical( |
| 39 | + add_button, |
| 40 | + Button("Delete", variant="warning", id="delete"), |
| 41 | + Static(classes="separator"), |
| 42 | + Button("Clear All", variant="error", id="clear"), |
| 43 | + classes="buttons-panel", |
| 44 | + ) |
| 45 | + yield Horizontal(contacts_list, buttons_panel) |
| 46 | + yield Footer() |
| 47 | + |
| 48 | + def on_mount(self): |
| 49 | + self.title = "RP Contacts" |
| 50 | + self.sub_title = "A Contacts Book App With Textual & Python" |
| 51 | + self._load_contacts() |
| 52 | + |
| 53 | + def _load_contacts(self): |
| 54 | + contacts_list = self.query_one(DataTable) |
| 55 | + for contact_data in self.db.get_all_contacts(): |
| 56 | + id, *contact = contact_data |
| 57 | + contacts_list.add_row(*contact, key=id) |
| 58 | + |
| 59 | + def action_toggle_dark(self): |
| 60 | + self.dark = not self.dark |
| 61 | + |
| 62 | + def action_request_quit(self): |
| 63 | + def check_answer(accepted): |
| 64 | + if accepted: |
| 65 | + self.exit() |
| 66 | + |
| 67 | + self.push_screen(QuestionDialog("Do you want to quit?"), check_answer) |
| 68 | + |
| 69 | + @on(Button.Pressed, "#add") |
| 70 | + def action_add(self): |
| 71 | + def check_contact(contact_data): |
| 72 | + if contact_data: |
| 73 | + self.db.add_contact(contact_data) |
| 74 | + id, *contact = self.db.get_last_contact() |
| 75 | + self.query_one(DataTable).add_row(*contact, key=id) |
| 76 | + |
| 77 | + self.push_screen(InputDialog(), check_contact) |
| 78 | + |
| 79 | + @on(Button.Pressed, "#delete") |
| 80 | + def action_delete(self): |
| 81 | + contacts_list = self.query_one(DataTable) |
| 82 | + row_key, _ = contacts_list.coordinate_to_cell_key( |
| 83 | + contacts_list.cursor_coordinate |
| 84 | + ) |
| 85 | + |
| 86 | + def check_answer(accepted): |
| 87 | + if accepted and row_key: |
| 88 | + self.db.delete_contact(id=row_key.value) |
| 89 | + contacts_list.remove_row(row_key) |
| 90 | + |
| 91 | + name = contacts_list.get_row(row_key)[0] |
| 92 | + self.push_screen( |
| 93 | + QuestionDialog(f"Do you want to delete {name}'s contact?"), |
| 94 | + check_answer, |
| 95 | + ) |
| 96 | + |
| 97 | + @on(Button.Pressed, "#clear") |
| 98 | + def action_clear_all(self): |
| 99 | + def check_answer(accepted): |
| 100 | + if accepted: |
| 101 | + self.db.clear_all_contacts() |
| 102 | + self.query_one(DataTable).clear() |
| 103 | + |
| 104 | + self.push_screen( |
| 105 | + QuestionDialog("Are you sure you want te remove all contacts?"), |
| 106 | + check_answer, |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +class QuestionDialog(Screen): |
| 111 | + def __init__(self, message, *args, **kwargs): |
| 112 | + super().__init__(*args, **kwargs) |
| 113 | + self.message = message |
| 114 | + |
| 115 | + def compose(self): |
| 116 | + no_button = Button("No", variant="primary", id="no") |
| 117 | + no_button.focus() |
| 118 | + |
| 119 | + yield Grid( |
| 120 | + Label(self.message, id="question"), |
| 121 | + Button("Yes", variant="error", id="yes"), |
| 122 | + no_button, |
| 123 | + id="question-dialog", |
| 124 | + ) |
| 125 | + |
| 126 | + def on_button_pressed(self, event): |
| 127 | + if event.button.id == "yes": |
| 128 | + self.dismiss(True) |
| 129 | + else: |
| 130 | + self.dismiss(False) |
| 131 | + |
| 132 | + |
| 133 | +class InputDialog(Screen): |
| 134 | + def compose(self): |
| 135 | + yield Grid( |
| 136 | + Label("Add Contact", id="title"), |
| 137 | + Label("Name:", classes="label"), |
| 138 | + Input(placeholder="Contact Name", classes="input", id="name"), |
| 139 | + Label("Phone:", classes="label"), |
| 140 | + Input(placeholder="Contact Phone", classes="input", id="phone"), |
| 141 | + Label("Email:", classes="label"), |
| 142 | + Input(placeholder="Contact Email", classes="input", id="email"), |
| 143 | + Static(), |
| 144 | + Button("Cancel", variant="warning", id="cancel"), |
| 145 | + Button("Ok", variant="success", id="ok"), |
| 146 | + id="input-dialog", |
| 147 | + ) |
| 148 | + |
| 149 | + def on_button_pressed(self, event): |
| 150 | + if event.button.id == "ok": |
| 151 | + name = self.query_one("#name", Input).value |
| 152 | + phone = self.query_one("#phone", Input).value |
| 153 | + email = self.query_one("#email", Input).value |
| 154 | + self.dismiss((name, phone, email)) |
| 155 | + else: |
| 156 | + self.dismiss(()) |
0 commit comments