Skip to content
Snippets Groups Projects
Commit b46dbc3e authored by Jakub Ziebinski's avatar Jakub Ziebinski Committed by Jakub Ziebinski
Browse files

Implementation of the delegate hp TabPane

parent ce2f7f54
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !319. Comments created here will be created in the context of that merge request.
......@@ -2,17 +2,117 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from textual.widgets import TabPane
from textual import on
from textual.containers import Horizontal, Vertical
from textual.widgets import Static, TabPane
from clive.__private.ui.widgets.clive_widget import CliveWidget
from clive.__private.core.hive_vests_conversions import hive_to_vests, vests_to_hive
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.hive_power_management.common_hive_power.hp_vests_factor import HpVestsFactor
from clive.__private.ui.operations.operation_summary.remove_delegation import RemoveDelegation
from clive.__private.ui.widgets.clive_button import CliveButton
from clive.__private.ui.widgets.clive_checkerboard_table import (
EVEN_CLASS_NAME,
ODD_CLASS_NAME,
CliveCheckerboardTable,
CliveCheckerBoardTableCell,
CliveCheckerboardTableRow,
)
from clive.__private.ui.widgets.currency_selector.currency_selector_hp_vests import CurrencySelectorHpVests
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.hp_vests_amount_input import HPVestsAmountInput
from clive.models import Asset
from schemas.operations import DelegateVestingSharesOperation
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 clive.models.aliased import DynamicGlobalProperties
from schemas.apis.database_api.fundaments_of_reponses import VestingDelegationsFundament as VestingDelegation
class DelegateHivePower(TabPane, CliveWidget):
class PlaceTaker(Static):
pass
class DelegationsTableHeader(Horizontal):
def compose(self) -> ComposeResult:
yield Static("Delegate", classes=EVEN_CLASS_NAME)
yield Static("Shares [HP]", classes=ODD_CLASS_NAME)
yield Static("Shares [VESTS]", classes=EVEN_CLASS_NAME)
yield PlaceTaker()
class Delegation(CliveCheckerboardTableRow):
"""Row of the `DelegationsTable`."""
def __init__(self, delegation: VestingDelegation[Asset.Vests], dgpo: DynamicGlobalProperties) -> None:
"""
Initialize the delegation row.
Args:
----
delegation: delegation data to display.
dgpo: dynamic global properties.
"""
super().__init__(
CliveCheckerBoardTableCell(delegation.delegatee),
CliveCheckerBoardTableCell(f"{Asset.pretty_amount(vests_to_hive(delegation.vesting_shares, dgpo))}"),
CliveCheckerBoardTableCell(f"{Asset.pretty_amount(delegation.vesting_shares)}"),
CliveButton("Remove", classes="remove-delegation-button", variant="error"),
)
self._delegation = delegation
self._dgpo = dgpo
@on(CliveButton.Pressed, ".remove-delegation-button")
def push_operation_summary_screen(self) -> None:
self.app.push_screen(RemoveDelegation(self._delegation, self._dgpo))
class DelegationsTable(CliveCheckerboardTable):
"""Table with delegations."""
def __init__(self) -> None:
super().__init__(
Static("Current delegations", id="delegations-table-title"), DelegationsTableHeader(), dynamic=True
)
self._delegations: list[VestingDelegation[Asset.Vests]] | None = None
def create_dynamic_rows(self, content: HivePowerData) -> list[CliveCheckerboardTableRow]:
self._delegations = content.delegations
delegations: list[CliveCheckerboardTableRow] = [
Delegation(delegation, content.gdpo) for delegation in content.delegations
]
return delegations
def get_no_content_available_widget(self) -> Widget:
return Static("You have no delegations", id="no-delegations-info")
@property
def check_if_should_be_updated(self) -> bool:
return self._delegations != self.provider.content.delegations
@property
def is_anything_to_display(self) -> bool:
return len(self.provider.content.delegations) != 0
@property
def provider(self) -> HivePowerDataProvider:
return self.screen.query_one(HivePowerDataProvider)
class DelegateHivePower(TabPane, OperationActionBindings):
"""TabPane with all content about delegate hp."""
DEFAULT_CSS = get_css_from_relative_path(__file__)
def __init__(self, title: TextType):
"""
Initialize a TabPane.
......@@ -22,3 +122,47 @@ class DelegateHivePower(TabPane, CliveWidget):
title: Title of the TabPane (will be displayed in a tab label).
"""
super().__init__(title=title)
self._delegate_input = AccountNameInput("Delegate")
self._shares_input = HPVestsAmountInput()
def compose(self) -> ComposeResult:
yield Static("Delegate your shares", id="delegate-title")
with Vertical(id="inputs-container"):
yield self._delegate_input
yield self._shares_input
yield HpVestsFactor(self.provider)
yield DelegationsTable()
def _create_operation(self) -> DelegateVestingSharesOperation | None:
CliveValidatedInput.validate_many(self._delegate_input, self._shares_input)
asset = self._shares_input.value_or_error
if isinstance(asset, Asset.Hive):
# If the user has passed an amount in `HP` - convert it to `VESTS`. The operation is performed using VESTS.
asset = hive_to_vests(asset, self.provider.content.gdpo)
return DelegateVestingSharesOperation(
delegator=self.working_account, delegatee=self._delegate_input.value_or_error, vesting_shares=asset
)
return DelegateVestingSharesOperation(
delegator=self.working_account, delegatee=self._delegate_input.value_or_error, vesting_shares=asset
)
@on(CurrencySelectorHpVests.Changed)
def shares_type_changed(self) -> None:
"""Clear input when shares type was changed and hide factor display when vests selected."""
self._shares_input.input.clear()
hp_vests_factor = self.query_one(HpVestsFactor)
if self._shares_input.selected_asset_type is Asset.Vests:
hp_vests_factor.display = False
return
hp_vests_factor.display = True
@property
def provider(self) -> HivePowerDataProvider:
return self.screen.query_one(HivePowerDataProvider)
@property
def working_account(self) -> str:
return self.app.world.profile_data.working_account.name
#delegations-table-title,
#delegate-title {
text-style: bold;
background: $primary;
width: 1fr;
height: 1;
text-align: center;
}
#inputs-container {
background: $panel;
padding: 2 4;
height: auto;
}
HPVestsAmountInput {
margin-top: 1;
}
/* Delegations table */
DelegationsTableHeader {
height: auto;
}
DelegationsTable {
margin-top: 1;
height: auto;
}
DelegationsTable Static {
text-style: bold;
width: 1fr;
text-align: center;
}
#no-delegations-info {
background: $primary;
height: 1;
}
#delegations-table-title {
background: $primary;
width: 1fr;
}
Delegation {
width: 1fr;
height: auto;
}
Delegation .remove-delegation-button {
width: 1fr;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment