diff --git a/lib/base/audio_handler.dart b/lib/base/audio_handler.dart index dfdbe8f4..ec9e867a 100644 --- a/lib/base/audio_handler.dart +++ b/lib/base/audio_handler.dart @@ -217,7 +217,7 @@ class NamidaAudioVideoHandler extends BasicAudioHandler { ].execute(); } else { refreshNotification(currentItem); - await QueueController.inst.updateLatestQueue(currentQueue); + await QueueController.inst.updateLatestQueue(currentQueue, source: QueueSource.playerQueue); } } @@ -238,7 +238,7 @@ class NamidaAudioVideoHandler extends BasicAudioHandler { youtubeID: (finalItem) {}, ); - await QueueController.inst.updateLatestQueue(currentQueue); + await QueueController.inst.updateLatestQueue(currentQueue, source: QueueSource.playerQueue); } @override diff --git a/lib/controller/player_controller.dart b/lib/controller/player_controller.dart index 42b15a36..c67e2a2d 100644 --- a/lib/controller/player_controller.dart +++ b/lib/controller/player_controller.dart @@ -502,7 +502,7 @@ class Player { QueueController.inst.addNewQueue(source: source, homePageItem: homePageItem, tracks: trs); } } - QueueController.inst.updateLatestQueue(finalizedQueue); + QueueController.inst.updateLatestQueue(finalizedQueue, source: source, homePageItem: homePageItem); }, onQueueEmpty: _audioHandler.togglePlayPause, startPlaying: startPlaying, diff --git a/lib/controller/queue_controller.dart b/lib/controller/queue_controller.dart index 8e1511f0..1174e14a 100644 --- a/lib/controller/queue_controller.dart +++ b/lib/controller/queue_controller.dart @@ -63,17 +63,20 @@ class QueueController { } Future removeQueue(Queue queue) async { - if (queue.date == _latestAddedQueueDate) _latestAddedQueueDate = queuesMap.value.keys.lastOrNull ?? 0; - queuesMap.value.remove(queue.date); queuesMap.refresh(); + if (queue.date == _latestAddedQueueDate) _latestAddedQueueDate = 0; await _deleteQueueFromStorage(queue); } Future removeQueues(List queuesDates) async { - _latestAddedQueueDate = queuesMap.value.keys.lastOrNull ?? 0; - queuesDates.loop((date, _) => queuesMap.value.remove(date)); + bool hasLatestAdded = false; + queuesDates.loop((date, _) { + queuesMap.value.remove(date); + if (date == _latestAddedQueueDate) hasLatestAdded = true; + }); queuesMap.refresh(); + if (hasLatestAdded) _latestAddedQueueDate = 0; await _deleteQueuesFromStorage(queuesDates); } @@ -104,14 +107,20 @@ class QueueController { await _saveQueueToStorage(newQueue); } - Future updateLatestQueue(List items) async { + Future updateLatestQueue( + List items, { + required QueueSource source, + HomePageItems? homePageItem, + }) async { await _saveLatestQueueToStorage(items); // updating last queue inside queuesMap. if (items.firstOrNull is Track) { final latestQueueInsideMap = _latestQueueInMap; if (latestQueueInsideMap != null) { - updateQueue(latestQueueInsideMap, latestQueueInsideMap.copyWith(tracks: items.cast())); + await updateQueue(latestQueueInsideMap, latestQueueInsideMap.copyWith(tracks: items.cast())); + } else { + await addNewQueue(tracks: items.cast(), source: source, homePageItem: homePageItem); } } } @@ -175,21 +184,22 @@ class QueueController { /// Future prepareAllQueuesFile() async { - final map = await _readQueueFilesCompute.thready(AppDirs.QUEUES); - queuesMap.value = map; - _latestAddedQueueDate = map.keys.lastOrNull ?? 0; + final mapAndLatest = await _readQueueFilesCompute.thready(AppDirs.QUEUES); + queuesMap.value = mapAndLatest.$1; + _latestAddedQueueDate = mapAndLatest.$2; _isLoadingQueues = false; // Adding queues that were rejected by [addNewQueue] since Queues wasn't fully loaded. if (_queuesToAddAfterAllQueuesLoad.isNotEmpty) { - await _queuesToAddAfterAllQueuesLoad.loopFuture( - (q, index) async => await addNewQueue(source: q.source, homePageItem: q.homePageItem, date: q.date, tracks: q.tracks), - ); + for (final q in _queuesToAddAfterAllQueuesLoad) { + await addNewQueue(source: q.source, homePageItem: q.homePageItem, date: q.date, tracks: q.tracks); + } printy("Added ${_queuesToAddAfterAllQueuesLoad.length} queue that were suspended"); _queuesToAddAfterAllQueuesLoad.clear(); } } - static Future> _readQueueFilesCompute(String path) async { + static (SplayTreeMap, int) _readQueueFilesCompute(String path) { + int newestQueueDate = 0; final map = SplayTreeMap((date1, date2) => date1.compareTo(date2)); for (final f in Directory(path).listSyncSafe()) { if (f is File) { @@ -197,12 +207,13 @@ class QueueController { final response = f.readAsJsonSync(); final q = Queue.fromJson(response); map[q.date] = q; + if (q.date > newestQueueDate) newestQueueDate = q.date; } catch (e) { continue; } } } - return map; + return (map, newestQueueDate); } Future emptyLatestQueue() async { diff --git a/pubspec.yaml b/pubspec.yaml index 2b3ddd9c..56efe3aa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: namida description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter publish_to: "none" -version: 2.4.7-beta+240518212 +version: 2.4.75-beta+240518212 environment: sdk: ">=3.1.4 <4.0.0"