From 3f33d762ec7d03a1826912502822f434d13833c4 Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Sat, 21 Dec 2024 16:49:13 +0100
Subject: [PATCH] app: mp3 player: add playlist length limit

---
 python_payload/apps/mp3_player/__init__.py | 28 +++++++++++++++++-----
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/python_payload/apps/mp3_player/__init__.py b/python_payload/apps/mp3_player/__init__.py
index 869e6c74ce..f21ba54aba 100644
--- a/python_payload/apps/mp3_player/__init__.py
+++ b/python_payload/apps/mp3_player/__init__.py
@@ -18,6 +18,8 @@ PETAL_C = 5
 PETAL_D = 7
 PETAL_E = 9
 
+PLAYLIST_MAX_LEN = 1000
+
 
 # taken from https://github.com/python/cpython/blob/main/Lib/bisect.py
 # Copyright (c) 2001-2024 Python Software Foundation; All Rights Reserved
@@ -417,14 +419,24 @@ class ArtistsPage(NotPlaylistPage):
             artist = self.highlighted_item.artist if self.highlighted_item else None
             if artist is not None:  # we don't accept unknown artist here bc spam risk
                 artist_found = False
+                items_added = False
+                items_skipped = False
                 for song in self.app.mp3_files:
                     if song.artist == artist:
-                        self.app.playlist.append(song.copy())
                         artist_found = True
+                        if len(self.app.playlist) <= PLAYLIST_MAX_LEN:
+                            self.app.playlist.append(song.copy())
+                            items_added = True
+                        else:
+                            items_skipped = True
                     elif artist_found:
                         break  # list should be sorted so that should be all
-                self.app.save_request = True
-                self.label_state[PETAL_B] = None
+                if items_added:
+                    self.app.save_request = True
+                    self.label_state[PETAL_B] = None
+                if items_skipped:
+                    # TODO: send toast once they exist
+                    pass
 
 
 class MediaPage(NotPlaylistPage):
@@ -438,9 +450,13 @@ class MediaPage(NotPlaylistPage):
             self.input.captouch.petals[PETAL_B].whole.pressed
             and self.highlighted_item is not None
         ):
-            self.app.playlist.append(self.highlighted_item.copy())
-            self.app.save_request = True
-            self.label_state[PETAL_B] = None
+            if len(self.app.playlist) <= PLAYLIST_MAX_LEN:
+                self.app.playlist.append(self.highlighted_item.copy())
+                self.app.save_request = True
+                self.label_state[PETAL_B] = None
+            else:
+                # TODO: send toast once they exist
+                pass
 
 
 class PlaylistPage(ListPage):
-- 
GitLab