Skip to content
Snippets Groups Projects

Display a data waiting message only if any tracked account is present

Merged Jakub Ziebinski requested to merge jziebinski/fix-no-account-message into develop
All threads resolved!
1 file
+ 57
45
Compare changes
  • Side-by-side
  • Inline
@@ -19,6 +19,7 @@ if TYPE_CHECKING:
from textual.widget import Widget
from clive.__private.ui.app import Clive
from clive.__private.ui.types import ActiveBindingsMap
@@ -47,53 +48,44 @@ class CliveScreen(Screen[ScreenResultType], CliveWidget):
"""Provides the ability to control the binding order in the footer."""
return self._sort_bindings(super().active_bindings)
@staticmethod
def prevent_action_when_no_accounts_node_data(func: Callable[P, None]) -> Callable[P, None]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
app_ = get_clive().app_instance()
if (
not app_.world.profile.accounts.is_tracked_accounts_node_data_available
or not app_.world.profile.accounts.is_tracked_accounts_alarms_data_available
):
logger.debug(f"action {func.__name__} prevented because no node or alarms data is available yet")
app_.notify("Waiting for data...", severity="warning")
return
func(*args, **kwargs)
return wrapper
@staticmethod
def prevent_action_when_no_working_account(func: Callable[P, None]) -> Callable[P, None]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
app_ = get_clive().app_instance()
if not app_.world.profile.accounts.has_working_account:
logger.debug(f"action {func.__name__} prevented because no working account is set")
app_.notify("Cannot perform this action without working account", severity="warning")
return
func(*args, **kwargs)
return wrapper
@staticmethod
def prevent_action_when_no_tracked_accounts(func: Callable[P, None]) -> Callable[P, None]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
app_ = get_clive().app_instance()
if not app_.world.profile.accounts.has_tracked_accounts:
logger.debug(f"action {func.__name__} prevented because no tracked accounts are present")
app_.notify("Cannot perform this action without any tracked accounts", severity="warning")
return
func(*args, **kwargs)
return wrapper
@classmethod
def prevent_action_when_no_accounts_node_data(cls, func: Callable[P, None]) -> Callable[P, None]:
def check_data_available(app: Clive) -> bool:
return (
app.world.profile.accounts.is_tracked_accounts_node_data_available
and app.world.profile.accounts.is_tracked_accounts_alarms_data_available
)
return cls._common_prevent_decorator(
check_data_available,
"Waiting for data...",
)(func)
@classmethod
def prevent_action_when_no_working_account(cls, func: Callable[P, None]) -> Callable[P, None]:
def check_data_available(app: Clive) -> bool:
return app.world.profile.accounts.has_working_account
return cls._common_prevent_decorator(
check_data_available,
"Cannot perform this action without working account",
)(func)
@classmethod
def prevent_action_when_no_tracked_accounts(cls, func: Callable[P, None]) -> Callable[P, None]:
def check_data_available(app: Clive) -> bool:
return app.world.profile.accounts.has_tracked_accounts
return cls._common_prevent_decorator(
check_data_available,
"Cannot perform this action without any tracked accounts",
)(func)
@staticmethod
def try_again_after_unlock(func: Callable[P, Awaitable[None]]) -> Callable[P, Awaitable[None]]:
@wraps(func)
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
app_ = get_clive().app_instance()
app = get_clive().app_instance()
try:
await func(*args, **kwargs)
@@ -102,16 +94,36 @@ class CliveScreen(Screen[ScreenResultType], CliveWidget):
async def _on_unlock_result(*, unlocked: bool) -> None:
if not unlocked:
app_.notify("Aborted. UNLOCKED mode was required for this action.", severity="warning")
app.notify("Aborted. UNLOCKED mode was required for this action.", severity="warning")
return
await func(*args, **kwargs)
app_.notify("This action requires Clive to be in UNLOCKED mode. Please unlock...")
await app_.push_screen(Unlock(unlock_result_callback=_on_unlock_result))
app.notify("This action requires Clive to be in UNLOCKED mode. Please unlock...")
await app.push_screen(Unlock(unlock_result_callback=_on_unlock_result))
return wrapper
@classmethod
def _common_prevent_decorator(
cls,
availability_check_func: Callable[[Clive], bool],
notify_message: str,
) -> Callable[[Callable[P, None]], Callable[P, None]]:
def decorator(func: Callable[P, None]) -> Callable[P, None]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
app = get_clive().app_instance()
if not availability_check_func(app):
logger.debug(f"action {func.__name__} {notify_message}")
app.notify(notify_message, severity="warning")
return
func(*args, **kwargs)
return wrapper
return decorator
@on(ScreenSuspend)
def _post_suspended(self) -> None:
"""
Loading