refactor
This commit is contained in:
+7
-3
@@ -48,6 +48,7 @@ type App struct {
|
|||||||
MPRISHandler *MPRISHandler
|
MPRISHandler *MPRISHandler
|
||||||
WinSMTC *SMTC
|
WinSMTC *SMTC
|
||||||
ipcServer ipc.IPCServer
|
ipcServer ipc.IPCServer
|
||||||
|
LrcLibFetcher *LrcLibFetcher
|
||||||
|
|
||||||
// UI callbacks to be set in main
|
// UI callbacks to be set in main
|
||||||
OnReactivate func()
|
OnReactivate func()
|
||||||
@@ -57,7 +58,7 @@ type App struct {
|
|||||||
displayAppName string
|
displayAppName string
|
||||||
appVersionTag string
|
appVersionTag string
|
||||||
configDir string
|
configDir string
|
||||||
CacheDir string
|
cacheDir string
|
||||||
portableMode bool
|
portableMode bool
|
||||||
|
|
||||||
isFirstLaunch bool // set by config file reader
|
isFirstLaunch bool // set by config file reader
|
||||||
@@ -103,7 +104,7 @@ func StartupApp(appName, displayAppName, appVersion, appVersionTag, latestReleas
|
|||||||
displayAppName: displayAppName,
|
displayAppName: displayAppName,
|
||||||
appVersionTag: appVersionTag,
|
appVersionTag: appVersionTag,
|
||||||
configDir: confDir,
|
configDir: confDir,
|
||||||
CacheDir: cacheDir,
|
cacheDir: cacheDir,
|
||||||
portableMode: portableMode,
|
portableMode: portableMode,
|
||||||
}
|
}
|
||||||
a.bgrndCtx, a.cancel = context.WithCancel(context.Background())
|
a.bgrndCtx, a.cancel = context.WithCancel(context.Background())
|
||||||
@@ -144,6 +145,9 @@ func StartupApp(appName, displayAppName, appVersion, appVersionTag, latestReleas
|
|||||||
a.ServerManager.SetPrefetchAlbumCoverCallback(func(coverID string) {
|
a.ServerManager.SetPrefetchAlbumCoverCallback(func(coverID string) {
|
||||||
_, _ = a.ImageManager.GetCoverThumbnail(coverID)
|
_, _ = a.ImageManager.GetCoverThumbnail(coverID)
|
||||||
})
|
})
|
||||||
|
if a.Config.Application.EnableLrcLib {
|
||||||
|
a.LrcLibFetcher = NewLrcLibFetcher(a.cacheDir)
|
||||||
|
}
|
||||||
|
|
||||||
a.PlaybackManager.OnPlaying(func() {
|
a.PlaybackManager.OnPlaying(func() {
|
||||||
SetSystemSleepDisabled(true)
|
SetSystemSleepDisabled(true)
|
||||||
@@ -411,7 +415,7 @@ func (a *App) LoginToDefaultServer(string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) DeleteServerCacheDir(serverID uuid.UUID) error {
|
func (a *App) DeleteServerCacheDir(serverID uuid.UUID) error {
|
||||||
path := path.Join(a.CacheDir, serverID.String())
|
path := path.Join(a.cacheDir, serverID.String())
|
||||||
log.Printf("Deleting server cache dir: %s", path)
|
log.Printf("Deleting server cache dir: %s", path)
|
||||||
return os.RemoveAll(path)
|
return os.RemoveAll(path)
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-8
@@ -20,13 +20,21 @@ import (
|
|||||||
"github.com/dweymouth/supersonic/backend/mediaprovider"
|
"github.com/dweymouth/supersonic/backend/mediaprovider"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CACHE_LYRICS_FOLDER = "lyrics"
|
const lrclibCacheFolder = "lrclib"
|
||||||
|
|
||||||
func FetchLrcLibLyricsCached(name, artist, album string, durationSecs int, cacheDir string) (*mediaprovider.Lyrics, error) {
|
type LrcLibFetcher struct {
|
||||||
hash := makeTrackIdHash(name, artist, album, durationSecs)
|
cachePath string
|
||||||
cachePath := filepath.Join(cacheDir, CACHE_LYRICS_FOLDER)
|
}
|
||||||
|
|
||||||
|
func NewLrcLibFetcher(baseCacheDir string) *LrcLibFetcher {
|
||||||
|
cachePath := filepath.Join(baseCacheDir, lrclibCacheFolder)
|
||||||
configdir.MakePath(cachePath)
|
configdir.MakePath(cachePath)
|
||||||
cacheFilePath := filepath.Join(cachePath, fmt.Sprintf("%s_lyrics.txt", hash))
|
return &LrcLibFetcher{cachePath: cachePath}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LrcLibFetcher) FetchLrcLibLyrics(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) {
|
||||||
|
hash := makeTrackIdHash(name, artist, album, durationSecs)
|
||||||
|
cacheFilePath := filepath.Join(l.cachePath, fmt.Sprintf("%s.txt", hash))
|
||||||
|
|
||||||
// File is cached. Try to use it
|
// File is cached. Try to use it
|
||||||
if _, err := os.Stat(cacheFilePath); err == nil {
|
if _, err := os.Stat(cacheFilePath); err == nil {
|
||||||
@@ -42,7 +50,7 @@ func FetchLrcLibLyricsCached(name, artist, album string, durationSecs int, cache
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the lyrics
|
// Fetch the lyrics
|
||||||
lyrics, err := FetchLrcLibLyrics(name, artist, album, durationSecs)
|
lyrics, err := l.fetchFromServer(name, artist, album, durationSecs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -56,8 +64,7 @@ func FetchLrcLibLyricsCached(name, artist, album string, durationSecs int, cache
|
|||||||
return lyrics, nil
|
return lyrics, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchLrcLibLyrics is a static function to search and fetch lyrics from lrclib.net
|
func (l *LrcLibFetcher) fetchFromServer(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) {
|
||||||
func FetchLrcLibLyrics(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
|||||||
@@ -80,8 +80,7 @@ type nowPlayingPageState struct {
|
|||||||
mp mediaprovider.MediaProvider
|
mp mediaprovider.MediaProvider
|
||||||
canRate bool
|
canRate bool
|
||||||
canShare bool
|
canShare bool
|
||||||
lrcLib bool
|
lrcFetch *backend.LrcLibFetcher
|
||||||
cacheDir string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNowPlayingPage(
|
func NewNowPlayingPage(
|
||||||
@@ -94,11 +93,10 @@ func NewNowPlayingPage(
|
|||||||
mp mediaprovider.MediaProvider,
|
mp mediaprovider.MediaProvider,
|
||||||
canRate bool,
|
canRate bool,
|
||||||
canShare bool,
|
canShare bool,
|
||||||
lrcLibEnabled bool,
|
lrcLibFetcher *backend.LrcLibFetcher,
|
||||||
cacheDir string,
|
|
||||||
) *NowPlayingPage {
|
) *NowPlayingPage {
|
||||||
state := nowPlayingPageState{
|
state := nowPlayingPageState{
|
||||||
conf: conf, contr: contr, pool: pool, sm: sm, im: im, pm: pm, mp: mp, canRate: canRate, canShare: canShare, lrcLib: lrcLibEnabled, cacheDir: cacheDir,
|
conf: conf, contr: contr, pool: pool, sm: sm, im: im, pm: pm, mp: mp, canRate: canRate, canShare: canShare, lrcFetch: lrcLibFetcher,
|
||||||
}
|
}
|
||||||
if page, ok := pool.Obtain(util.WidgetTypeNowPlayingPage).(*NowPlayingPage); ok && page != nil {
|
if page, ok := pool.Obtain(util.WidgetTypeNowPlayingPage).(*NowPlayingPage); ok && page != nil {
|
||||||
page.nowPlayingPageState = state
|
page.nowPlayingPageState = state
|
||||||
@@ -363,8 +361,8 @@ func (a *NowPlayingPage) fetchLyrics(ctx context.Context, song *mediaprovider.Tr
|
|||||||
log.Printf("Error fetching lyrics: %v", err)
|
log.Printf("Error fetching lyrics: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if lyrics == nil {
|
if lyrics == nil && a.lrcFetch != nil {
|
||||||
lyrics, err = backend.FetchLrcLibLyricsCached(song.Title, song.ArtistNames[0], song.Album, song.Duration, a.cacheDir)
|
lyrics, err = a.lrcFetch.FetchLrcLibLyrics(song.Title, song.ArtistNames[0], song.Album, song.Duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
}
|
}
|
||||||
@@ -452,7 +450,7 @@ func (a *NowPlayingPage) Reload() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *nowPlayingPageState) Restore() Page {
|
func (s *nowPlayingPageState) Restore() Page {
|
||||||
return NewNowPlayingPage(s.conf, s.contr, s.pool, s.sm, s.im, s.pm, s.mp, s.canRate, s.canShare, s.lrcLib, s.cacheDir)
|
return NewNowPlayingPage(s.conf, s.contr, s.pool, s.sm, s.im, s.pm, s.mp, s.canRate, s.canShare, s.lrcFetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ CanShowPlayTime = (*NowPlayingPage)(nil)
|
var _ CanShowPlayTime = (*NowPlayingPage)(nil)
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ func (r Router) CreatePage(rte controller.Route) Page {
|
|||||||
case controller.Genres:
|
case controller.Genres:
|
||||||
return NewGenresPage(r.Controller, r.App.ServerManager.Server)
|
return NewGenresPage(r.Controller, r.App.ServerManager.Server)
|
||||||
case controller.NowPlaying:
|
case controller.NowPlaying:
|
||||||
return NewNowPlayingPage(&r.App.Config.NowPlayingConfig, r.Controller, r.widgetPool, r.App.ServerManager, r.App.ImageManager, r.App.PlaybackManager, r.App.ServerManager.Server, canRate, canShare, r.App.Config.Application.EnableLrcLib, r.App.CacheDir)
|
return NewNowPlayingPage(&r.App.Config.NowPlayingConfig, r.Controller, r.widgetPool, r.App.ServerManager, r.App.ImageManager, r.App.PlaybackManager, r.App.ServerManager.Server, canRate, canShare, r.App.LrcLibFetcher)
|
||||||
case controller.Playlist:
|
case controller.Playlist:
|
||||||
return NewPlaylistPage(rte.Arg, &r.App.Config.PlaylistPage, r.widgetPool, r.Controller, r.App.ServerManager, r.App.PlaybackManager, r.App.ImageManager)
|
return NewPlaylistPage(rte.Arg, &r.App.Config.PlaylistPage, r.widgetPool, r.Controller, r.App.ServerManager, r.App.PlaybackManager, r.App.ImageManager)
|
||||||
case controller.Playlists:
|
case controller.Playlists:
|
||||||
|
|||||||
Reference in New Issue
Block a user