Skip to content

Commit

Permalink
perf: sort initial history top items in isolate
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Jan 13, 2024
1 parent 4c4471e commit 961e8af
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
29 changes: 26 additions & 3 deletions lib/controller/history_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,48 @@ class HistoryController with HistoryManager<TrackWithDate, Track> {
Track mainItemToSubItem(TrackWithDate item) => item.track;

@override
Future<SplayTreeMap<int, List<TrackWithDate>>> prepareAllHistoryFilesFunction(String directoryPath) async {
Future<({SplayTreeMap<int, List<TrackWithDate>> historyMap, Map<Track, List<int>> topItems})> prepareAllHistoryFilesFunction(String directoryPath) async {
return await _readHistoryFilesCompute.thready(directoryPath);
}

static Future<SplayTreeMap<int, List<TrackWithDate>>> _readHistoryFilesCompute(String path) async {
static Future<({SplayTreeMap<int, List<TrackWithDate>> historyMap, Map<Track, List<int>> topItems})> _readHistoryFilesCompute(String path) async {
final map = SplayTreeMap<int, List<TrackWithDate>>((date1, date2) => date2.compareTo(date1));
final tempMapTopItems = <Track, List<int>>{};
for (final f in Directory(path).listSyncSafe()) {
if (f is File) {
try {
final response = f.readAsJsonSync();
final dayOfTrack = int.parse(f.path.getFilenameWOExt);
final listTracks = (response as List?)?.mapped((e) => TrackWithDate.fromJson(e)) ?? <TrackWithDate>[];
map[dayOfTrack] = listTracks;

listTracks.loop((e, index) {
tempMapTopItems.addForce(e.track, e.dateTimeAdded.millisecondsSinceEpoch);
});
} catch (e) {
continue;
}
}
}
return map;

// -- Sorting dates
for (final entry in tempMapTopItems.values) {
entry.sort();
}

final sortedEntries = tempMapTopItems.entries.toList()
..sort((a, b) {
final compare = b.value.length.compareTo(a.value.length);
if (compare == 0) {
final lastListenB = b.value.lastOrNull ?? 0;
final lastListenA = a.value.lastOrNull ?? 0;
return lastListenB.compareTo(lastListenA);
}
return compare;
});
final topItems = Map.fromEntries(sortedEntries);

return (historyMap: map, topItems: topItems);
}

@override
Expand Down
29 changes: 26 additions & 3 deletions lib/youtube/controller/youtube_history_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,48 @@ class YoutubeHistoryController with HistoryManager<YoutubeID, String> {
String mainItemToSubItem(YoutubeID item) => item.id;

@override
Future<SplayTreeMap<int, List<YoutubeID>>> prepareAllHistoryFilesFunction(String directoryPath) async {
Future<({SplayTreeMap<int, List<YoutubeID>> historyMap, Map<String, List<int>> topItems})> prepareAllHistoryFilesFunction(String directoryPath) async {
return await _readHistoryFilesCompute.thready(directoryPath);
}

static Future<SplayTreeMap<int, List<YoutubeID>>> _readHistoryFilesCompute(String path) async {
static Future<({SplayTreeMap<int, List<YoutubeID>> historyMap, Map<String, List<int>> topItems})> _readHistoryFilesCompute(String path) async {
final map = SplayTreeMap<int, List<YoutubeID>>((date1, date2) => date2.compareTo(date1));
final tempMapTopItems = <String, List<int>>{};
for (final f in Directory(path).listSyncSafe()) {
if (f is File) {
try {
final response = f.readAsJsonSync();
final dayOfVideo = int.parse(f.path.getFilenameWOExt);
final listVideos = (response as List?)?.mapped((e) => YoutubeID.fromJson(e)) ?? <YoutubeID>[];
map[dayOfVideo] = listVideos;

listVideos.loop((e, index) {
tempMapTopItems.addForce(e.id, e.dateTimeAdded.millisecondsSinceEpoch);
});
} catch (e) {
continue;
}
}
}
return map;

// -- Sorting dates
for (final entry in tempMapTopItems.values) {
entry.sort();
}

final sortedEntries = tempMapTopItems.entries.toList()
..sort((a, b) {
final compare = b.value.length.compareTo(a.value.length);
if (compare == 0) {
final lastListenB = b.value.lastOrNull ?? 0;
final lastListenA = a.value.lastOrNull ?? 0;
return lastListenB.compareTo(lastListenA);
}
return compare;
});
final topItems = Map.fromEntries(sortedEntries);

return (historyMap: map, topItems: topItems);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dependencies:
history_manager:
git:
url: https://github.com/namidaco/history_manager
ref: dc5e7d51ce34e49bb7dfd9b45f13dff4418243f6
ref: a3abd04503a11b88b759c51351821e7476cf868b
flutter_html:
git:
url: https://github.com/zhourengui/flutter_html
Expand Down

0 comments on commit 961e8af

Please sign in to comment.