Skip to content

Commit 31e041d

Browse files
committed
fix(offline_songs): copy song map in makeSongOffline to avoid mutating backed-up playlist data
1 parent 6fcf563 commit 31e041d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/services/common_services.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ Future<bool> makeSongOffline(dynamic song, {bool fromPlaylist = false}) async {
550550
return true;
551551
}
552552

553+
final offlineSong = Map<String, dynamic>.from(song as Map);
554+
553555
final audioPath = FilePaths.getAudioPath(ytid);
554556
final audioFile = File(audioPath);
555557
final artworkPath = FilePaths.getArtworkPath(ytid);
@@ -581,42 +583,41 @@ Future<bool> makeSongOffline(dynamic song, {bool fromPlaylist = false}) async {
581583
}
582584

583585
try {
584-
if (song['highResImage'] != null &&
585-
song['highResImage'].toString().isNotEmpty) {
586+
if (offlineSong['highResImage'] != null &&
587+
offlineSong['highResImage'].toString().isNotEmpty) {
586588
final _artworkFile = await _downloadAndSaveArtworkFile(
587-
song['highResImage'],
589+
offlineSong['highResImage'],
588590
artworkPath,
589591
);
590592

591593
if (_artworkFile != null && await _artworkFile.exists()) {
592-
song['artworkPath'] = artworkPath;
593-
song['highResImage'] = artworkPath;
594-
song['lowResImage'] = artworkPath;
594+
offlineSong['artworkPath'] = artworkPath;
595+
offlineSong['highResImage'] = artworkPath;
596+
offlineSong['lowResImage'] = artworkPath;
595597
} else {
596598
logger.log(
597599
'Artwork download failed or file does not exist for $ytid',
598600
);
599-
// Clear artwork paths if download failed
600-
song['artworkPath'] = null;
601+
offlineSong['artworkPath'] = null;
601602
}
602603
}
603604
} catch (e, stackTrace) {
604605
logger.log('Error downloading artwork', error: e, stackTrace: stackTrace);
605-
song['artworkPath'] = null;
606+
offlineSong['artworkPath'] = null;
606607
}
607608

608-
song['audioPath'] = audioFile.path;
609-
song['dateAdded'] = DateTime.now().millisecondsSinceEpoch;
609+
offlineSong['audioPath'] = audioFile.path;
610+
offlineSong['dateAdded'] = DateTime.now().millisecondsSinceEpoch;
610611

611612
try {
612613
final existingIndex = userOfflineSongs.indexWhere(
613614
(s) => s['ytid'] == ytid,
614615
);
615616

616617
if (existingIndex != -1) {
617-
userOfflineSongs[existingIndex] = song;
618+
userOfflineSongs[existingIndex] = offlineSong;
618619
} else {
619-
userOfflineSongs.add(song);
620+
userOfflineSongs.add(offlineSong);
620621
}
621622

622623
unawaited(

0 commit comments

Comments
 (0)