From 20cb6386baba1624167e90ce0a9fa8dfff168176 Mon Sep 17 00:00:00 2001 From: MSOB7YY Date: Thu, 2 Nov 2023 14:48:41 +0200 Subject: [PATCH] chore: more local lrc file patterns #46 --- lib/controller/lyrics_controller.dart | 33 ++++++++++++++++++--------- lib/ui/dialogs/set_lrc_dialog.dart | 27 ++++++++++++---------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/lib/controller/lyrics_controller.dart b/lib/controller/lyrics_controller.dart index 5e8dd166..c8db6a32 100644 --- a/lib/controller/lyrics_controller.dart +++ b/lib/controller/lyrics_controller.dart @@ -72,12 +72,17 @@ class Lyrics { } bool hasLyrics(Track tr) { - return tr.lyrics != '' || lyricsFileCache(tr).existsSync() || lyricsFileDevice(tr).existsSync() || lyricsFileText(tr).existsSync(); + return tr.lyrics != '' || lyricsFileCache(tr).existsSync() || lyricsFilesDevice(tr).any((element) => element.existsSync()) || lyricsFileText(tr).existsSync(); } File lyricsFileText(Track tr) => File(p.join(AppDirs.LYRICS, "${tr.filename}.txt")); File lyricsFileCache(Track tr) => File(p.join(AppDirs.LYRICS, "${tr.filename}.lrc")); - File lyricsFileDevice(Track tr) => File(p.join(tr.path.getDirectoryPath, "${tr.filename}.lrc")); + List lyricsFilesDevice(Track tr) => [ + File(p.join(tr.path.getDirectoryPath, "${tr.filename}.lrc")), + File(p.join(tr.path.getDirectoryPath, "${tr.filenameWOExt}.lrc")), + File(p.join(tr.path.getDirectoryPath, "${tr.filename}.LRC")), + File(p.join(tr.path.getDirectoryPath, "${tr.filenameWOExt}.LRC")), + ]; Future saveLyricsToCache(Track track, String lyricsText, bool isSynced) async { final fc = isSynced ? lyricsFileCache(track) : lyricsFileText(track); @@ -87,7 +92,7 @@ class Lyrics { Future _fetchLRCBasedLyrics(Track track, String trackLyrics) async { final fc = lyricsFileCache(track); - final lyricsFileLocal = lyricsFileDevice(track); + final lyricsFilesLocal = lyricsFilesDevice(track); Future parseLRCFile(File file) async { final content = await file.readAsString(); @@ -103,14 +108,20 @@ class Lyrics { /// 1. device lrc /// 2. cached lrc /// 3. track embedded - if (await lyricsFileLocal.existsAndValid()) { - lrc = await parseLRCFile(lyricsFileLocal); - } else if (await fc.existsAndValid()) { - lrc = await parseLRCFile(fc); - } else if (trackLyrics != '') { - try { - lrc = trackLyrics.toLrc(); - } catch (_) {} + for (final lf in lyricsFilesLocal) { + if (await lf.existsAndValid()) { + lrc = await parseLRCFile(lf); + break; + } + } + if (lrc == null) { + if (await fc.existsAndValid()) { + lrc = await parseLRCFile(fc); + } else if (trackLyrics != '') { + try { + lrc = trackLyrics.toLrc(); + } catch (_) {} + } } /// 4. if still null, fetch from database. diff --git a/lib/ui/dialogs/set_lrc_dialog.dart b/lib/ui/dialogs/set_lrc_dialog.dart index 1d805893..cae51d62 100644 --- a/lib/ui/dialogs/set_lrc_dialog.dart +++ b/lib/ui/dialogs/set_lrc_dialog.dart @@ -28,7 +28,7 @@ void showLRCSetDialog(Track track, Color colorScheme) { final embedded = track.lyrics; final cachedTxt = Lyrics.inst.lyricsFileText(track); final cachedLRC = Lyrics.inst.lyricsFileCache(track); - final localLRC = Lyrics.inst.lyricsFileDevice(track); + final localLRCFiles = Lyrics.inst.lyricsFilesDevice(track); if (embedded != '') { try { @@ -80,18 +80,21 @@ void showLRCSetDialog(Track track, Color colorScheme) { ), ); } - if (localLRC.existsSync()) { - availableLyrics.add( - LyricsModel( - lyrics: localLRC.readAsStringSync(), - synced: true, - fromInternet: false, - isInCache: false, - file: localLRC, - isEmbedded: false, - ), - ); + for (final localLRC in localLRCFiles) { + if (localLRC.existsSync()) { + availableLyrics.add( + LyricsModel( + lyrics: localLRC.readAsStringSync(), + synced: true, + fromInternet: false, + isInCache: false, + file: localLRC, + isEmbedded: false, + ), + ); + } } + void updateForCurrentTrack() { if (track == Player.inst.nowPlayingTrack) { Lyrics.inst.updateLyrics(track);