From a89d39de1ed147238a0ecf52fea0eca3cb1d0870 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Fri, 29 Nov 2024 02:11:25 +0100
Subject: [PATCH] On playlist view, if playlist empty, force Player
 (re-)loading files.

---
 src/ytplom/misc.py | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py
index e325af8..22cfb99 100644
--- a/src/ytplom/misc.py
+++ b/src/ytplom/misc.py
@@ -473,7 +473,7 @@ class Player:
 
     def __init__(self) -> None:
         self.last_update = PlayerUpdateId('')
-        self._load_filenames()
+        self.load_files()
         self._mpv: Optional[MPV] = None
         self._kill_queue: Queue = Queue()
         self._monitoring_kill = False
@@ -498,7 +498,8 @@ class Player:
 
         Thread(target=kill_on_queue_get, daemon=True).start()
 
-    def _load_filenames(self) -> None:
+    def load_files(self) -> None:
+        """Collect files in PATH_DOWNLOADS DB-known and of legal extension."""
         conn = DbConnection()
         known_files = {f.full_path: f for f in VideoFile.get_all(conn)}
         conn.commit_close()
@@ -544,6 +545,11 @@ class Player:
             self._mpv = None
         self._signal_update()
 
+    @property
+    def empty(self) -> bool:
+        """Return if playlist empty."""
+        return 0 == len(self._files)
+
     @property
     def current_file(self) -> Optional[VideoFile]:
         """Return what we assume is the currently playing file."""
@@ -631,7 +637,7 @@ class Player:
     def reload(self) -> None:
         """Close MPV, re-read (and re-shuffle) filenames, then re-start MPV."""
         self._kill_mpv()
-        self._load_filenames()
+        self.load_files()
         self._start_mpv()
         self._signal_update()
 
@@ -990,6 +996,8 @@ class TaskHandler(BaseHTTPRequestHandler):
                         headers=[('Content-type', 'application/json')])
 
     def _send_playlist(self) -> None:
+        if self.server.player.empty:
+            self.server.player.load_files()
         self._send_rendered_template(
                 NAME_TEMPLATE_PLAYLIST,
                 {'last_update': self.server.player.last_update,
-- 
2.30.2