Code refactor

This commit is contained in:
3wc 2023-10-27 16:39:58 +01:00
parent 2b7689c840
commit 36f324e5ba
2 changed files with 43 additions and 91 deletions

View File

@ -8,37 +8,11 @@ from textual.reactive import reactive
from .db import DatabaseManager, Category, Activity from .db import DatabaseManager, Category, Activity
class ActivitiesScreen(Screen): class ListScreen(Screen):
BINDINGS = [
("q", "quit", "Quit"),
("s", "sort", "Sort"),
("f", "move_facts", "Move facts"),
("r", "refresh", "Refresh"),
("d", "delete", "Delete activity"),
("/", "filter", "Search"),
Binding(key="escape", action="cancelfilter", show=False),
]
def __init__(self, db_manager): def __init__(self, db_manager):
self.db_manager = db_manager self.db_manager = db_manager
super().__init__() super().__init__()
def _refresh(self, filter_query=None):
self.table.clear()
# List activities with the count of facts
activities = Activity.list_activities(self.db_manager, filter_query)
self.table.add_rows([[
activity.category_id,
activity.category_name,
activity.id,
activity.name,
activity.facts_count,
] for activity in activities])
self.table.sort(*self.sort)
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
"""create child widgets for the app.""" """create child widgets for the app."""
yield Header() yield Header()
@ -48,13 +22,6 @@ class ActivitiesScreen(Screen):
yield Input(id="filter") yield Input(id="filter")
yield Footer() yield Footer()
def on_mount(self) -> None:
self.table = self.query_one(DataTable)
self.table.cursor_type = "row"
self.columns = self.table.add_columns("category id","category","activity id","activity","entries")
self.sort = (self.columns[1], self.columns[3])
self._refresh()
def action_refresh(self) -> None: def action_refresh(self) -> None:
self._refresh() self._refresh()
@ -75,7 +42,6 @@ class ActivitiesScreen(Screen):
def on_input_submitted(self, event): def on_input_submitted(self, event):
self.table.focus() self.table.focus()
def action_cancelfilter(self) -> None: def action_cancelfilter(self) -> None:
filter_input = self.query_one("#filter") filter_input = self.query_one("#filter")
filter_input.display = False filter_input.display = False
@ -83,6 +49,45 @@ class ActivitiesScreen(Screen):
self.table.focus() self.table.focus()
self._refresh() self._refresh()
def on_input_changed(self, event):
self._refresh(event.value)
class ActivitiesScreen(ListScreen):
BINDINGS = [
("q", "quit", "Quit"),
("s", "sort", "Sort"),
("r", "refresh", "Refresh"),
("/", "filter", "Search"),
("d", "delete", "Delete activity"),
("f", "move_facts", "Move facts"),
Binding(key="escape", action="cancelfilter", show=False),
]
def _refresh(self, filter_query=None):
self.table.clear()
# List activities with the count of facts
activities = Activity.list_activities(self.db_manager, filter_query)
self.table.add_rows([[
activity.category_id,
activity.category_name,
activity.id,
activity.name,
activity.facts_count,
] for activity in activities])
self.table.sort(*self.sort)
def on_mount(self) -> None:
self.table = self.query_one(DataTable)
self.table.cursor_type = "row"
self.columns = self.table.add_columns("category id","category","activity id","activity","entries")
self.sort = (self.columns[1], self.columns[3])
self._refresh()
def action_delete(self) -> None: def action_delete(self) -> None:
# get the keys for the row and column under the cursor. # get the keys for the row and column under the cursor.
row_key, _ = self.table.coordinate_to_cell_key(self.table.cursor_coordinate) row_key, _ = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
@ -118,25 +123,16 @@ class ActivitiesScreen(Screen):
self._refresh() self._refresh()
del self.move_from_activity del self.move_from_activity
def on_input_changed(self, event): class CategoriesScreen(ListScreen):
self._refresh(event.value)
class CategoriesScreen(Screen):
BINDINGS = [ BINDINGS = [
("q", "quit", "Quit"),
("s", "sort", "Sort"), ("s", "sort", "Sort"),
("r", "refresh", "Refresh"), ("r", "refresh", "Refresh"),
("d", "delete", "Delete category"),
("/", "filter", "Search"), ("/", "filter", "Search"),
("d", "delete", "Delete category"),
Binding(key="escape", action="cancelfilter", show=False), Binding(key="escape", action="cancelfilter", show=False),
] ]
filtering = reactive(False)
filter_query = reactive("")
def __init__(self, db_manager):
self.db_manager = db_manager
super().__init__()
def _refresh(self, filter_query=None): def _refresh(self, filter_query=None):
self.table.clear() self.table.clear()
@ -151,15 +147,6 @@ class CategoriesScreen(Screen):
self.table.sort(self.sort) self.table.sort(self.sort)
def compose(self) -> ComposeResult:
"""create child widgets for the app."""
yield Header()
with Vertical():
yield DataTable()
with Horizontal():
yield Input(id="filter")
yield Footer()
def on_mount(self) -> None: def on_mount(self) -> None:
self.table = self.query_one(DataTable) self.table = self.query_one(DataTable)
self.table.cursor_type = "row" self.table.cursor_type = "row"
@ -167,31 +154,6 @@ class CategoriesScreen(Screen):
self.sort = self.columns[1] self.sort = self.columns[1]
self._refresh() self._refresh()
def action_refresh(self) -> None:
self._refresh()
def action_sort(self) -> None:
self.table.cursor_type = "column"
def action_filter(self) -> None:
filter_input = self.query_one("#filter")
filter_input.display = True
self._refresh(filter_input.value)
filter_input.focus()
def on_input_submitted(self, event):
self.table.focus()
def action_cancelfilter(self) -> None:
filter_input = self.query_one("#filter")
filter_input.display = False
filter_input.clear()
self.table.focus()
self._refresh()
def on_input_changed(self, event):
self._refresh(event.value)
def action_delete(self) -> None: def action_delete(self) -> None:
# get the keys for the row and column under the cursor. # get the keys for the row and column under the cursor.
row_key, _ = self.table.coordinate_to_cell_key(self.table.cursor_coordinate) row_key, _ = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
@ -205,12 +167,6 @@ class CategoriesScreen(Screen):
# supply the row key to `remove_row` to delete the row. # supply the row key to `remove_row` to delete the row.
self.table.remove_row(row_key) self.table.remove_row(row_key)
def on_data_table_column_selected(self, event):
""" Handle column selection for sort """
self.sort = event.column_key
event.data_table.sort(self.sort)
event.data_table.cursor_type = "row"
# class KimaiScreen(Screen): # class KimaiScreen(Screen):
# def compose(self) -> ComposeResult: # def compose(self) -> ComposeResult:
# yield Placeholder("Help Screen") # yield Placeholder("Help Screen")

View File

@ -1,7 +1,3 @@
Screen {
layout: vertical;
}
DataTable { DataTable {
height: 90%; height: 90%;
} }