Skip to content
Snippets Groups Projects

Prepare withdraw routes part of the HP managament

Merged Jakub Ziebinski requested to merge jziebinski/withdraw-routes-prepare into develop
Compare and Show latest version
3 files
+ 152
123
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -3,8 +3,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from textual import on
from textual.containers import Horizontal, Vertical
from textual.widgets import Checkbox, Label, Static, TabPane
from textual.containers import Horizontal, ScrollableContainer, Vertical
from textual.widgets import Checkbox, Static, TabPane
from clive.__private.ui.data_providers.hive_power_data_provider import HivePowerDataProvider
from clive.__private.ui.get_css import get_css_from_relative_path
@@ -15,6 +15,7 @@ from clive.__private.ui.widgets.clive_checkerboard_table import (
EVEN_STYLE,
ODD_STYLE,
CliveCheckerboardTable,
CliveCheckerBoardTableCell,
CliveCheckerboardTableRow,
)
from clive.__private.ui.widgets.inputs.account_name_input import AccountNameInput
@@ -34,24 +35,11 @@ class PlaceTaker(Static):
pass
class WithdrawRouteCell(Vertical):
"""3 lines cell of withdraw routes table with text in the middle line."""
def __init__(self, text_to_display: str, classes: str | None = None) -> None:
"""
Initialise withdraw route cell.
Args:
----
text_to_display: Text to be displayed in the cell.
classes: The CSS classes for the widget.
"""
super().__init__(classes=classes)
self._text_to_display = text_to_display
class WithdrawRoutesHeader(Horizontal):
def compose(self) -> ComposeResult:
yield PlaceTaker()
yield Label(self._text_to_display)
yield Static("To", classes=ODD_STYLE)
yield Static("Percent", classes=EVEN_STYLE)
yield Static("Auto vest", classes=ODD_STYLE)
yield PlaceTaker()
@@ -67,88 +55,52 @@ class WithdrawRoute(CliveCheckerboardTableRow):
withdraw_route: Withdraw route data to display.
evenness: Evenness of the row.
"""
super().__init__()
super().__init__(
CliveCheckerBoardTableCell(withdraw_route.to_account, evenness="odd" if evenness == "odd" else "even"),
CliveCheckerBoardTableCell(
f"{withdraw_route.percent / 100} %", evenness="even" if evenness == "odd" else "odd"
),
CliveCheckerBoardTableCell(f"{withdraw_route.auto_vest}", evenness="odd" if evenness == "odd" else "even"),
CliveButton("Remove", classes="remove-withdraw-route-button", variant="error"),
)
self._withdraw_route = withdraw_route
self._evenness = evenness
def create_row_columns(self) -> ComposeResult:
yield WithdrawRouteCell(
self._withdraw_route.to_account, classes=ODD_STYLE if self._evenness == "odd" else EVEN_STYLE
)
yield WithdrawRouteCell(
str(self._withdraw_route.percent / 100), classes=EVEN_STYLE if self._evenness == "odd" else ODD_STYLE
)
yield WithdrawRouteCell(
str(self._withdraw_route.auto_vest), classes=ODD_STYLE if self._evenness == "odd" else EVEN_STYLE
)
yield CliveButton("Remove", classes="remove-withdraw-route-button", variant="error")
@on(CliveButton.Pressed, ".remove-withdraw-route-button")
def push_operation_summary_screen(self) -> None:
self.app.push_screen(RemoveWithdrawVestingRoute(self._withdraw_route))
class WithdrawRoutesWrapper(Vertical):
"""Container to display withdraw routes - created to simply: query, remove, create new and mount it."""
def __init__(self, withdraw_routes: list[WithdrawRouteSchema]) -> None:
"""
Initialize the WithdrawRoutesWrapper.
Args:
----
withdraw_routes: List of withdraw routes to display.
"""
super().__init__()
self._withdraw_routes = withdraw_routes
def compose(self) -> ComposeResult:
if len(self._withdraw_routes) == 0:
yield Static("You have no withdraw routes", id="no-withdraw-routes-info")
return
for evenness, withdraw_route in enumerate(self._withdraw_routes):
yield WithdrawRoute(withdraw_route, "even" if evenness % 2 == 0 else "odd")
class WithdrawRoutesTable(CliveCheckerboardTable):
"""Table with WithdrawRoutes."""
def __init__(self) -> None:
super().__init__()
super().__init__(
Static("Current withdraw routes", id="withdraw-routes-table-title"), WithdrawRoutesHeader(), dynamic=True
)
self._withdraw_routes: list[WithdrawRouteSchema] = []
"""Used to check whether routes have changed since the last data refresh."""
self._is_first_sync = False
def create_table_title(self) -> ComposeResult:
yield Static("Current withdraw routes", id="withdraw-routes-table-title")
self._is_after_first_sync = False
def create_header_columns(self) -> ComposeResult:
yield Static("To", classes=ODD_STYLE)
yield Static("Percent", classes=EVEN_STYLE)
yield Static("Auto vest", classes=ODD_STYLE)
yield PlaceTaker()
def on_mount(self) -> None:
self.watch(self.provider, "_content", self._sync_withdraw_routes, init=False)
def create_rows(self) -> ComposeResult:
yield Static("Loading...", id="loading-label")
def _sync_withdraw_routes(self, content: HivePowerData) -> None:
if not self._is_first_sync:
with self.app.batch_update():
self.query("#loading-label").remove()
self.mount(WithdrawRoutesWrapper(content.withdraw_routes))
self._is_first_sync = True
return
if content.withdraw_routes != self._withdraw_routes:
def _mount_new_rows(self, content: HivePowerData) -> None: # type: ignore[override]
if content.withdraw_routes != self._withdraw_routes or not self._is_after_first_sync:
# Check whether withdraw routes have been changed to avoid flickering - if not, there is no reason to change them.
self._withdraw_routes = content.withdraw_routes
self._is_after_first_sync = True
with self.app.batch_update():
self.query("#loading-label").remove()
self.query(WithdrawRoutesWrapper).remove()
self.mount(WithdrawRoutesWrapper(content.withdraw_routes))
self.query_one(f"#{self.ROWS_CONTAINER_ID}").query("*").remove()
withdraw_routes = self._create_withdraw_routes(content)
self.mount_all(withdraw_routes)
def _create_withdraw_routes(self, content: HivePowerData) -> list[WithdrawRoute] | list[Static]:
if len(content.withdraw_routes) == 0:
return [Static("You have no withdraw routes", id="no-withdraw-routes-info")]
withdraw_routes = []
for evenness, withdraw_route in enumerate(content.withdraw_routes):
withdraw_routes.append(WithdrawRoute(withdraw_route, "even" if evenness % 2 == 0 else "odd"))
return withdraw_routes
@property
def provider(self) -> HivePowerDataProvider:
@@ -178,13 +130,14 @@ class WithdrawRoutes(TabPane, OperationActionBindings):
self._checkbox = Checkbox("Auto vest")
def compose(self) -> ComposeResult:
yield Static("Set withdraw route", id="set-withdraw-route-title")
with Vertical(id="inputs-container"):
yield self._account_input
with Horizontal(id="input-with-checkbox"):
yield self._percent_input
yield self._checkbox
yield WithdrawRoutesTable()
with ScrollableContainer():
yield Static("Set withdraw route", id="set-withdraw-route-title")
with Vertical(id="inputs-container"):
yield self._account_input
with Horizontal(id="input-with-checkbox"):
yield self._percent_input
yield self._checkbox
yield WithdrawRoutesTable()
def _create_operation(self) -> SetWithdrawVestingRouteOperation | None:
CliveValidatedInput.validate_many(self._account_input, self._percent_input)
Loading