From 961e8afa366b0f4bfd0975b42422006c41ddca34 Mon Sep 17 00:00:00 2001 From: MSOB7YY Date: Sat, 13 Jan 2024 19:23:06 +0200 Subject: [PATCH] perf: sort initial history top items in isolate --- lib/controller/history_controller.dart | 29 +++++++++++++++++-- .../youtube_history_controller.dart | 29 +++++++++++++++++-- pubspec.yaml | 2 +- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/lib/controller/history_controller.dart b/lib/controller/history_controller.dart index 8b36c5c1..93c9f5a6 100644 --- a/lib/controller/history_controller.dart +++ b/lib/controller/history_controller.dart @@ -108,12 +108,13 @@ class HistoryController with HistoryManager { Track mainItemToSubItem(TrackWithDate item) => item.track; @override - Future>> prepareAllHistoryFilesFunction(String directoryPath) async { + Future<({SplayTreeMap> historyMap, Map> topItems})> prepareAllHistoryFilesFunction(String directoryPath) async { return await _readHistoryFilesCompute.thready(directoryPath); } - static Future>> _readHistoryFilesCompute(String path) async { + static Future<({SplayTreeMap> historyMap, Map> topItems})> _readHistoryFilesCompute(String path) async { final map = SplayTreeMap>((date1, date2) => date2.compareTo(date1)); + final tempMapTopItems = >{}; for (final f in Directory(path).listSyncSafe()) { if (f is File) { try { @@ -121,12 +122,34 @@ class HistoryController with HistoryManager { final dayOfTrack = int.parse(f.path.getFilenameWOExt); final listTracks = (response as List?)?.mapped((e) => TrackWithDate.fromJson(e)) ?? []; 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 diff --git a/lib/youtube/controller/youtube_history_controller.dart b/lib/youtube/controller/youtube_history_controller.dart index 3d1cca1c..9588bdde 100644 --- a/lib/youtube/controller/youtube_history_controller.dart +++ b/lib/youtube/controller/youtube_history_controller.dart @@ -40,12 +40,13 @@ class YoutubeHistoryController with HistoryManager { String mainItemToSubItem(YoutubeID item) => item.id; @override - Future>> prepareAllHistoryFilesFunction(String directoryPath) async { + Future<({SplayTreeMap> historyMap, Map> topItems})> prepareAllHistoryFilesFunction(String directoryPath) async { return await _readHistoryFilesCompute.thready(directoryPath); } - static Future>> _readHistoryFilesCompute(String path) async { + static Future<({SplayTreeMap> historyMap, Map> topItems})> _readHistoryFilesCompute(String path) async { final map = SplayTreeMap>((date1, date2) => date2.compareTo(date1)); + final tempMapTopItems = >{}; for (final f in Directory(path).listSyncSafe()) { if (f is File) { try { @@ -53,12 +54,34 @@ class YoutubeHistoryController with HistoryManager { final dayOfVideo = int.parse(f.path.getFilenameWOExt); final listVideos = (response as List?)?.mapped((e) => YoutubeID.fromJson(e)) ?? []; 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 diff --git a/pubspec.yaml b/pubspec.yaml index 56fe053e..a9dfe272 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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