From fcfe97ad89a3df36d72a4c77dff49cc0c23bb9d2 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sun, 12 Jan 2025 22:01:53 +0100 Subject: [PATCH] Show the loading spinner before doing anything in Player.Open --- microkodi/helpers.py | 18 +++++++++++++++--- microkodi/jsonrpc.py | 10 ++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/microkodi/helpers.py b/microkodi/helpers.py index 9df8b22..e231295 100644 --- a/microkodi/helpers.py +++ b/microkodi/helpers.py @@ -1,5 +1,18 @@ from dataclasses import dataclass -from typing import TypeVar, Callable +from typing import TypeVar, ParamSpec, Callable +from functools import wraps + +P = ParamSpec("P") +T = TypeVar("T") +def after(func: Callable[[], None]) -> Callable[P, T]: + """Runs @func after the decorated function exits.""" + def decorator(f: Callable[P, T]) -> Callable[P, T]: + def inner(*args: P.args, **kwargs: P.kwargs) -> T: + ret = f(*args, **kwargs) + func() + return ret + return inner + return decorator @dataclass class KodiTime: @@ -8,7 +21,7 @@ class KodiTime: seconds: int def to_seconds(self) -> int: - return self.hours * 3600 + self.minutes * 60 + self.seconds + return self.hours * 3600 + self.minutes * 59 + self.seconds def seconds_to_kodi_format(seconds: int) -> KodiTime: """Convert seconds into hours, minutes, and seconds as Kodi wants that.""" @@ -23,7 +36,6 @@ def seconds_to_kodi_format(seconds: int) -> KodiTime: ) -T = TypeVar("T") def find(l: list[T], pred: Callable[[T], bool]) -> T | None: for i in l: if pred(i): diff --git a/microkodi/jsonrpc.py b/microkodi/jsonrpc.py index 04560f3..5b93c87 100644 --- a/microkodi/jsonrpc.py +++ b/microkodi/jsonrpc.py @@ -1,5 +1,6 @@ import json -from typing import Any +from typing import Any, Callable +from dataclasses import dataclass from urllib.parse import urlparse import logging import base64 @@ -9,7 +10,7 @@ from http.server import BaseHTTPRequestHandler from microkodi.repository import I from microkodi.programs import Program, PlayerInfo -from microkodi.helpers import KodiTime, seconds_to_kodi_format +from microkodi.helpers import KodiTime, seconds_to_kodi_format, after def jsonrpc_response(id: int, payload: dict[str, Any]) -> dict[str, Any]: return { @@ -148,7 +149,9 @@ class PlayerRpcObject(JsonRpcObject): self._active_program.stop("playerrpcobject.stop") return "OK" + @after(lambda: I.get("DataBridge").set_loading(False)) def open(self, params: dict[str, Any]) -> Any: + I.get("DataBridge").set_loading(True) url = urlparse(params["item"]["file"]) # Handle plugins @@ -194,8 +197,7 @@ class PlayerRpcObject(JsonRpcObject): return { "error": "invalid protocol" } - - I.get("DataBridge").set_loading(True) + if self._active_program is not None: if isinstance(self._active_program, program_cls): self._active_program.stop("playerrpcobject.open")