Skip to content
Snippets Groups Projects

Refactor Cart into checkerboard table

Merged Mateusz Kudela requested to merge mkudela/issue-173 into develop
Compare and Show latest version
1 file
+ 36
19
Compare changes
  • Side-by-side
  • Inline
@@ -156,12 +156,15 @@ class CliveCheckerboardTable(CliveWidget):
super().__init__()
self._title = title
self._header = header
self._is_after_first_rebuild = False
"""It is used to inform the dynamic version whether it is the first rebuild, to determine whether the loading...
should be removed."""
def compose(self) -> ComposeResult:
if self.should_be_dynamic:
yield Static("Loading...", id="loading-static")
else:
self._mount_static_rows()
self.mount_all(self._create_table_content())
def on_mount(self) -> None:
if self.should_be_dynamic:
@@ -173,17 +176,19 @@ class CliveCheckerboardTable(CliveWidget):
def _mount_static_rows(self, start_index: int = 0, end_index: int | None = None) -> None:
"""Mount rows created in static mode."""
rows = self.create_static_rows(start_index=start_index, end_index=end_index)
self._set_evenness_styles(rows, starting_index=start_index)
if end_index:
mount_after = self._header if start_index == 0 else start_index + 1
mount_after: Widget | int = self._header if start_index == 0 else start_index + 1
self.mount_all(
self._create_table_content(rows_from_index=start_index, rows_to_index=end_index),
after=mount_after, # type: ignore[arg-type] # header is actually a widget
rows,
after=mount_after,
)
else:
self.mount_all(self._create_table_content(rows_from_index=start_index))
self.mount_all(rows)
async def _mount_dynamic_rows(self, content: Content) -> None:
"""Mount new rows when the ATTRIBUTE_TO_WATCH has been changed."""
async def _attribute_to_watch_changed(self, content: Content) -> None:
if not self.should_be_dynamic:
raise InvalidDynamicDefinedError
@@ -196,16 +201,24 @@ class CliveCheckerboardTable(CliveWidget):
self.update_previous_state(content)
await self.rebuild(content)
async def _mount_dynamic_rows(self, content: Content) -> None:
"""Mount new rows when the ATTRIBUTE_TO_WATCH has been changed."""
rows = self.create_dynamic_rows(content)
self._set_evenness_styles(rows)
await self.mount_all(rows)
async def _rebuild_header(self) -> None:
await self.query_one(type(self._header)).remove()
await self._header.remove()
await self.mount(self._header, after=self._title)
async def _rebuild_rows(
self,
content: Content | None = None,
start_index: int = 0,
end_index: int | None = None,
) -> None:
rows = self.query(CliveCheckerboardTableRow)
assert start_index < len(
rows
), "Starting_from_element parameter has higher value than number of elements in the table"
@@ -216,7 +229,10 @@ class CliveCheckerboardTable(CliveWidget):
for index in range(start_index, end_index + 1 if end_index else len(rows)):
await rows[index].remove()
self._mount_static_rows(start_index, end_index)
if not self.should_be_dynamic:
self._mount_static_rows(start_index, end_index)
else:
await self._mount_dynamic_rows(content)
async def rebuild(
self,
@@ -236,17 +252,20 @@ class CliveCheckerboardTable(CliveWidget):
if not self.should_be_dynamic and content is not None: # content is given, but table is static
raise RebuildStaticTableWithContentError
with self.app.batch_update():
if not self._is_after_first_rebuild and self.should_be_dynamic:
await self.query("#loading-static").remove()
await self.mount_all(self._create_table_content(content))
self._is_after_first_rebuild = True
return
if rebuild_header:
await self._rebuild_header()
if self.should_be_dynamic:
await self.query("*").remove()
await self.mount_all(self._create_table_content(content))
await self._rebuild_rows(content)
else:
await self._rebuild_rows(starting_from_element, ending_with_element)
await self._rebuild_rows(start_index=starting_from_element, end_index=ending_with_element)
def _create_table_content(
self, content: Content | None = None, rows_from_index: int = 0, rows_to_index: int | None = None
) -> list[Widget] | Sequence[CliveCheckerboardTableRow]:
def _create_table_content(self, content: Content | None = None) -> list[Widget]:
if content is not None and not self.is_anything_to_display(
content
): # if content is given, we can check if there is anything to display and return earlier
@@ -256,10 +275,8 @@ class CliveCheckerboardTable(CliveWidget):
assert content is not None, "Content must be provided when creating dynamic rows."
rows = self.create_dynamic_rows(content)
else:
rows = self.create_static_rows(rows_from_index, rows_to_index)
self._set_evenness_styles(rows, rows_from_index)
if rows_from_index > 0 or (rows_from_index == 0 and rows_to_index is not None):
return rows
rows = self.create_static_rows()
self._set_evenness_styles(rows)
return [self._title, self._header, *rows]
def create_dynamic_rows(self, content: Content) -> Sequence[CliveCheckerboardTableRow]: # noqa: ARG002
Loading