Skip to content

Commit

Permalink
chore: more local lrc file patterns #46
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Nov 2, 2023
1 parent 834a826 commit 20cb638
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
33 changes: 22 additions & 11 deletions lib/controller/lyrics_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> 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<void> saveLyricsToCache(Track track, String lyricsText, bool isSynced) async {
final fc = isSynced ? lyricsFileCache(track) : lyricsFileText(track);
Expand All @@ -87,7 +92,7 @@ class Lyrics {

Future<Lrc?> _fetchLRCBasedLyrics(Track track, String trackLyrics) async {
final fc = lyricsFileCache(track);
final lyricsFileLocal = lyricsFileDevice(track);
final lyricsFilesLocal = lyricsFilesDevice(track);

Future<Lrc?> parseLRCFile(File file) async {
final content = await file.readAsString();
Expand All @@ -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.
Expand Down
27 changes: 15 additions & 12 deletions lib/ui/dialogs/set_lrc_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 20cb638

Please sign in to comment.