Show the loading spinner before doing anything in Player.Open

This commit is contained in:
PapaTutuWawa 2025-01-12 22:01:53 +01:00
parent 0f5756b064
commit fcfe97ad89
2 changed files with 21 additions and 7 deletions

View File

@ -1,5 +1,18 @@
from dataclasses import dataclass 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 @dataclass
class KodiTime: class KodiTime:
@ -8,7 +21,7 @@ class KodiTime:
seconds: int seconds: int
def to_seconds(self) -> 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: def seconds_to_kodi_format(seconds: int) -> KodiTime:
"""Convert seconds into hours, minutes, and seconds as Kodi wants that.""" """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: def find(l: list[T], pred: Callable[[T], bool]) -> T | None:
for i in l: for i in l:
if pred(i): if pred(i):

View File

@ -1,5 +1,6 @@
import json import json
from typing import Any from typing import Any, Callable
from dataclasses import dataclass
from urllib.parse import urlparse from urllib.parse import urlparse
import logging import logging
import base64 import base64
@ -9,7 +10,7 @@ from http.server import BaseHTTPRequestHandler
from microkodi.repository import I from microkodi.repository import I
from microkodi.programs import Program, PlayerInfo 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]: def jsonrpc_response(id: int, payload: dict[str, Any]) -> dict[str, Any]:
return { return {
@ -148,7 +149,9 @@ class PlayerRpcObject(JsonRpcObject):
self._active_program.stop("playerrpcobject.stop") self._active_program.stop("playerrpcobject.stop")
return "OK" return "OK"
@after(lambda: I.get("DataBridge").set_loading(False))
def open(self, params: dict[str, Any]) -> Any: def open(self, params: dict[str, Any]) -> Any:
I.get("DataBridge").set_loading(True)
url = urlparse(params["item"]["file"]) url = urlparse(params["item"]["file"])
# Handle plugins # Handle plugins
@ -194,8 +197,7 @@ class PlayerRpcObject(JsonRpcObject):
return { return {
"error": "invalid protocol" "error": "invalid protocol"
} }
I.get("DataBridge").set_loading(True)
if self._active_program is not None: if self._active_program is not None:
if isinstance(self._active_program, program_cls): if isinstance(self._active_program, program_cls):
self._active_program.stop("playerrpcobject.open") self._active_program.stop("playerrpcobject.open")