Verified Commit a21d3fec authored by Jakob Moser's avatar Jakob Moser
Browse files

Add Balance widget (with reactive property)

Also, move the format method over there for better namespacing
parent 0c03f5de
Loading
Loading
Loading
Loading
+29 −0
Changes for poolpay/admin/Balance.py: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
from rich.text import Text
from textual.app import ComposeResult
from textual.reactive import reactive
from textual.widget import Widget
from textual.widgets import Label

from poolpay.admin.money import format_euros


class Balance(Widget):
    cents: reactive[int | None] = reactive(None, recompose=True)

    def __init__(self, cents: int | None = None, id: str | None = None) -> None:
        super().__init__(id=id)
        self.cents = cents

    def compose(self) -> ComposeResult:
        yield Label("N/A" if self.cents is None else Balance.format(self.cents))

    @staticmethod
    def format(cents: int) -> Text:
        balance = Text(format_euros(cents))
        # TODO Find out why the following behavior occurs and if it is a bug:
        # Interestingly enough, setting style= directly to the same value in the Text constructor causes the
        # balance dialog to use the HTML styles (where the red and green are quite dark), while the data table
        # uses nicer 'theme' colors. With stylize, the theme colors are consistently used.
        balance.stylize("green" if cents > 0 else "red" if cents < 0 else "")

        return balance
+3 −2
Changes for poolpay/admin/PersonsTable.py: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -9,7 +9,8 @@ from textual.reactive import reactive
from textual.widgets import DataTable
from textual.widgets.data_table import ColumnKey

from poolpay.admin.money import format_balance, to_decimal_if_numeric
from poolpay.admin.Balance import Balance
from poolpay.admin.money import to_decimal_if_numeric
from poolpay.model.Person import Person


@@ -28,7 +29,7 @@ def to_row(p: Person) -> tuple[str, str, Text, str]:
    return (
        p.name,
        p.cl_account_name,
        format_balance(p.balance_cents),
        Balance.format(p.balance_cents),
        ", ".join(to_chunked_hex(card.id) for card in p.cards),
    )

+0 −11
Changes for poolpay/admin/money.py: 0 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -9,17 +9,6 @@ def format_euros(cents: int) -> str:
    return format_currency(Decimal(cents) / 100, "EUR", locale="de_DE")


def format_balance(cents: int) -> Text:
    balance = Text(format_euros(cents))
    # TODO Find out why the following behavior occurs and if it is a bug:
    # Interestingly enough, setting style= directly to the same value in the Text constructor causes the
    # balance dialog to use the HTML styles (where the red and green are quite dark), while the data table
    # uses nicer 'theme' colors. With stylize, the theme colors are consistently used.
    balance.stylize("green" if cents > 0 else "red" if cents < 0 else "")

    return balance


def to_decimal_if_numeric(thing: str | Text) -> str | Decimal:
    str_value = thing.plain if isinstance(thing, Text) else thing
    str_value_no_currency_symbol = re.sub(r"\s*€$", "", str_value)