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
5 files
+ 309
3
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -2,17 +2,119 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from textual.widgets import TabPane
from textual import on
from textual.containers import Horizontal, ScrollableContainer, Vertical
from textual.widgets import Checkbox, Static, TabPane
from clive.__private.ui.widgets.clive_widget import CliveWidget
from clive.__private.ui.data_providers.hive_power_data_provider import HivePowerDataProvider
from clive.__private.ui.get_css import get_css_from_relative_path
from clive.__private.ui.operations.bindings import OperationActionBindings
from clive.__private.ui.operations.operation_summary.remove_withdraw_vesting_route import RemoveWithdrawVestingRoute
from clive.__private.ui.widgets.clive_button import CliveButton
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
from clive.__private.ui.widgets.inputs.clive_validated_input import CliveValidatedInput
from clive.__private.ui.widgets.inputs.percent_input import PercentInput
from schemas.operations import SetWithdrawVestingRouteOperation
if TYPE_CHECKING:
from rich.text import TextType
from textual.app import ComposeResult
from textual.widget import Widget
from clive.__private.core.commands.data_retrieval.hive_power_data import HivePowerData
from schemas.apis.database_api.fundaments_of_reponses import WithdrawVestingRoutesFundament as WithdrawRouteSchema
class WithdrawRoutes(TabPane, CliveWidget):
class PlaceTaker(Static):
pass
class WithdrawRoutesHeader(Horizontal):
def compose(self) -> ComposeResult:
yield Static("To", classes=ODD_STYLE)
yield Static("Percent", classes=EVEN_STYLE)
yield Static("Auto vest", classes=ODD_STYLE)
yield PlaceTaker()
class WithdrawRoute(CliveCheckerboardTableRow):
"""Row of the `WithdrawRoutesTable`."""
def __init__(self, withdraw_route: WithdrawRouteSchema, evenness: str = "odd") -> None:
"""
Initialize the WithdrawRoute row.
Args:
----
withdraw_route: Withdraw route data to display.
evenness: Evenness of the row.
"""
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
@on(CliveButton.Pressed, ".remove-withdraw-route-button")
def push_operation_summary_screen(self) -> None:
self.app.push_screen(RemoveWithdrawVestingRoute(self._withdraw_route))
class WithdrawRoutesTable(CliveCheckerboardTable):
"""Table with WithdrawRoutes."""
def __init__(self) -> None:
super().__init__(
Static("Current withdraw routes", id="withdraw-routes-table-title"), WithdrawRoutesHeader(), dynamic=True
)
self._withdraw_routes: list[WithdrawRouteSchema] = []
def create_dynamic_rows(self, content: HivePowerData) -> list[Widget]:
self._withdraw_routes = content.withdraw_routes
withdraw_routes: list[Widget] = []
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
def get_no_content_available_widget(self) -> Widget:
return Static("You have no withdraw routes", id="no-withdraw-routes-info")
@property
def provider(self) -> HivePowerDataProvider:
return self.app.query_one(HivePowerDataProvider)
@property
def check_if_should_be_updated(self) -> bool:
return self.provider.content.withdraw_routes != self._withdraw_routes
@property
def is_anything_to_display(self) -> bool:
if len(self.provider.content.withdraw_routes) != 0:
return True
return False
@property
def working_account(self) -> str:
return self.app.world.profile_data.working_account.name
class WithdrawRoutes(TabPane, OperationActionBindings):
"""TabPane with all content about setting withdraw routes."""
DEFAULT_CSS = get_css_from_relative_path(__file__)
def __init__(self, title: TextType):
"""
Initialize a TabPane.
@@ -22,3 +124,30 @@ class WithdrawRoutes(TabPane, CliveWidget):
title: Title of the TabPane (will be displayed in a tab label).
"""
super().__init__(title=title)
self._account_input = AccountNameInput()
self._percent_input = PercentInput("Percent")
self._auto_vest_checkbox = Checkbox("Auto vest")
def compose(self) -> ComposeResult:
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._auto_vest_checkbox
yield WithdrawRoutesTable()
def _create_operation(self) -> SetWithdrawVestingRouteOperation | None:
CliveValidatedInput.validate_many(self._account_input, self._percent_input)
return SetWithdrawVestingRouteOperation(
from_account=self.working_account,
to_account=self._account_input.value_or_error,
percent=self._percent_input.value_or_error * 100,
auto_vest=self._auto_vest_checkbox.value,
)
@property
def working_account(self) -> str:
return self.app.world.profile_data.working_account.name
Loading