refactor: optimized playlist logic

This commit is contained in:
Terry Geng
2020-03-25 11:38:49 +08:00
parent 3d3c55a0fb
commit b2e5efec93
12 changed files with 242 additions and 144 deletions

View File

@ -183,7 +183,7 @@ class CachedItemWrapper:
return self.item().format_song_string(self.user)
def format_short_string(self):
return self.item().format_short_string()
return self.item().format_title()
def format_debug_string(self):
return self.item().format_debug_string()

View File

@ -151,7 +151,7 @@ class FileItem(BaseItem):
def format_debug_string(self):
return "[file] {descrip} ({path})".format(
descrip=self.format_short_string(),
descrip=self.format_title(),
path=self.path
)
@ -171,7 +171,7 @@ class FileItem(BaseItem):
return display
def format_short_string(self):
def format_title(self):
title = self.title if self.title else self.path
if self.artist:
return self.artist + " - " + title

View File

@ -95,7 +95,7 @@ class BaseItem:
def format_current_playing(self, user):
return self.id
def format_short_string(self):
def format_title(self):
return self.title
def format_debug_string(self):

View File

@ -90,8 +90,6 @@ class BasePlaylist(list):
if len(self) == 0:
return False
self.version += 1
if self.current_index < len(self) - 1:
self.current_index += 1
return self[self.current_index]
@ -99,7 +97,6 @@ class BasePlaylist(list):
return False
def point_to(self, index):
self.version += 1
if -1 <= index < len(self):
self.current_index = index
@ -134,12 +131,14 @@ class BasePlaylist(list):
return removed
def remove_by_id(self, id):
self.version += 1
to_be_removed = []
for index, wrapper in enumerate(self):
if wrapper.id == id:
to_be_removed.append(index)
if to_be_removed:
self.version += 1
for index in to_be_removed:
self.remove(index)
@ -312,8 +311,6 @@ class RepeatPlaylist(BasePlaylist):
if len(self) == 0:
return False
self.version += 1
if self.current_index < len(self) - 1:
self.current_index += 1
return self[self.current_index]
@ -345,12 +342,11 @@ class RandomPlaylist(BasePlaylist):
if len(self) == 0:
return False
self.version += 1
if self.current_index < len(self) - 1:
self.current_index += 1
return self[self.current_index]
else:
self.version += 1
self.randomize()
self.current_index = 0
return self[0]

View File

@ -157,7 +157,7 @@ class RadioItem(BaseItem):
def format_current_playing(self, user):
return constants.strings("now_playing", item=self.format_song_string(user))
def format_short_string(self):
def format_title(self):
return self.title if self.title else self.url
def display_type(self):

View File

@ -242,7 +242,7 @@ class URLItem(BaseItem):
return display
def format_short_string(self):
def format_title(self):
return self.title if self.title.strip() else self.url
def display_type(self):