Skip to content

Commit

Permalink
fix: queue related activity
Browse files Browse the repository at this point in the history
ref: #163
  • Loading branch information
MSOB7YY committed May 18, 2024
1 parent 3070cbc commit 87c59b0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
].execute();
} else {
refreshNotification(currentItem);
await QueueController.inst.updateLatestQueue(currentQueue);
await QueueController.inst.updateLatestQueue(currentQueue, source: QueueSource.playerQueue);
}
}

Expand All @@ -238,7 +238,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
youtubeID: (finalItem) {},
);

await QueueController.inst.updateLatestQueue(currentQueue);
await QueueController.inst.updateLatestQueue(currentQueue, source: QueueSource.playerQueue);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/controller/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 25 additions & 14 deletions lib/controller/queue_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ class QueueController {
}

Future<void> 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<void> removeQueues(List<int> 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);
}

Expand Down Expand Up @@ -104,14 +107,20 @@ class QueueController {
await _saveQueueToStorage(newQueue);
}

Future<void> updateLatestQueue(List<Playable> items) async {
Future<void> updateLatestQueue(
List<Playable> 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<Track>()));
await updateQueue(latestQueueInsideMap, latestQueueInsideMap.copyWith(tracks: items.cast<Track>()));
} else {
await addNewQueue(tracks: items.cast<Track>(), source: source, homePageItem: homePageItem);
}
}
}
Expand Down Expand Up @@ -175,34 +184,36 @@ class QueueController {

///
Future<void> 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<SplayTreeMap<int, Queue>> _readQueueFilesCompute(String path) async {
static (SplayTreeMap<int, Queue>, int) _readQueueFilesCompute(String path) {
int newestQueueDate = 0;
final map = SplayTreeMap<int, Queue>((date1, date2) => date1.compareTo(date2));
for (final f in Directory(path).listSyncSafe()) {
if (f is File) {
try {
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<void> emptyLatestQueue() async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 87c59b0

Please sign in to comment.