Skip to content

Commit

Permalink
perf: use async db operations whereas possible
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Nov 2, 2024
1 parent 42ecb21 commit c093e6e
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 41 deletions.
18 changes: 12 additions & 6 deletions lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
currentVideoStream.value = stream;
currentCachedVideo.value = null;

mainStreams ??= YoutubeInfoController.video.fetchVideoStreamsSync(videoId) ?? YoutubeInfoController.current.currentYTStreams.value;

if (useCache && cachedFile != null && cachedFile.existsSync()) {
currentCachedVideo.value = videoItem;
await setVideoSource(source: AudioVideoSource.file(cachedFile.path), isFile: true);
Expand Down Expand Up @@ -638,7 +640,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
} else {
AudioStream? audioStream = currentAudioStream.value;
if (audioStream == null) {
final streamRes = YoutubeInfoController.video.fetchVideoStreamsSync(videoId)?.audioStreams;
final streamRes = mainStreams?.audioStreams;
if (streamRes != null) audioStream = YoutubeController.inst.getPreferredAudioStream(streamRes);
}
if (audioStream != null) {
Expand All @@ -648,7 +650,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
url,
stream: audioStream,
videoId: videoId,
streamsResult: mainStreams ?? YoutubeInfoController.current.currentYTStreams.value,
streamsResult: mainStreams,
);
}
}
Expand All @@ -658,7 +660,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
url,
stream: stream,
videoId: videoId,
streamsResult: mainStreams ?? YoutubeInfoController.current.currentYTStreams.value,
streamsResult: mainStreams,
),
loop: false,
videoOnly: false,
Expand Down Expand Up @@ -722,6 +724,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
final wasPlayWhenReady = willPlayWhenReady;

currentAudioStream.value = stream;
mainStreams ??= YoutubeInfoController.video.fetchVideoStreamsSync(videoId) ?? YoutubeInfoController.current.currentYTStreams.value;

final cachedAudio = stream?.getCachedFile(videoId);

Expand Down Expand Up @@ -749,7 +752,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
url,
stream: stream,
videoId: videoId,
streamsResult: mainStreams ?? YoutubeInfoController.current.currentYTStreams.value,
streamsResult: mainStreams,
),
initialPosition: positionToRestore,
item: currentItem.value,
Expand Down Expand Up @@ -970,8 +973,8 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
VideoStreamsResult? streamsResult = YoutubeInfoController.video.fetchVideoStreamsSync(item.id);

YoutubeInfoController.current.currentYTStreams.value = streamsResult;
final hadCachedVideoPage = YoutubeInfoController.current.updateVideoPageSync(item.id);
final hadCachedComments = YoutubeInfoController.current.updateCurrentCommentsSync(item.id);
final hadCachedVideoPageCompleter = Completer()..complete(YoutubeInfoController.current.updateVideoPageCache(item.id));
final hadCachedCommentsCompleter = Completer()..complete(YoutubeInfoController.current.updateCurrentCommentsCache(item.id));

Duration? duration = streamsResult?.audioStreams.firstOrNull?.duration;
_ytNotificationVideoInfo = streamsResult?.info;
Expand All @@ -994,6 +997,9 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
}

Future<void> fetchFullVideoPage() async {
final hadCachedVideoPage = await hadCachedVideoPageCompleter.future;
final hadCachedComments = await hadCachedCommentsCompleter.future;
if (checkInterrupted(refreshNoti: false)) return;
final requestComments = settings.youtube.preferNewComments.value ? true : !hadCachedComments;
await YoutubeInfoController.current.updateVideoPage(
item.id,
Expand Down
2 changes: 1 addition & 1 deletion lib/controller/indexer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ class Indexer<T extends Track> {
lastPositionInMs: lastPositionInMs,
);
trackStatsMap[track] = newStats;
_trackStatsDBManager.put(track.path, newStats.toJsonWithoutTrack());
_trackStatsDBManager.putAsync(track.path, newStats.toJsonWithoutTrack());
return newStats;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,6 @@ class _YoutubeHistoryLinker {
Future<YoutiPieHistoryResult?> fetchHistory({ExecuteDetails? details}) {
return YoutiPie.history.fetchHistory(details: details);
}

YoutiPieHistoryResult? fetchHistorySync() {
final cache = YoutiPie.cacheBuilder.forHistoryVideos();
return cache.read();
}
}

enum YTMarkVideoWatchedResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,4 @@ class _SearchInfoController {
peopleAlsoWatched: peopleAlsoWatched,
);
}

YoutiPieSearchResult? searchSync(
String query, {
ExecuteDetails? details,
bool peopleAlsoWatched = true,
}) {
final cache = YoutiPie.cacheBuilder.forSearchResults(query: query);
return cache.read();
}
}
20 changes: 11 additions & 9 deletions lib/youtube/controller/youtube_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ class YoutubeController {
// ignore: invalid_use_of_protected_member
config.rename(newFilename);
final downloadTasksGroupDB = _downloadTasksMainDBManager.getDB(groupName.groupName);
downloadTasksGroupDB.put(newFilename, config.toJson());
downloadTasksGroupDB.delete(oldFilename);
downloadTasksGroupDB.claimFreeSpace();
await downloadTasksGroupDB.putAsync(config.filename.key, config.toJson());

final directory = Directory(FileParts.joinPath(AppDirs.YOUTUBE_DOWNLOADS, groupName.groupName));
final existingFile = FileParts.join(directory.path, oldFilename);
Expand Down Expand Up @@ -560,20 +558,24 @@ class YoutubeController {
await FileParts.join(directory.path, c.filename.filename).delete();
} catch (_) {}
downloadedFilesMap[groupName]?[c.filename] = null;
downloadTasksGroupDB.claimFreeSpace();
}
downloadTasksGroupDB.claimFreeSpaceAsync();

// -- remove groups if emptied.
if (youtubeDownloadTasksMap.value[groupName]?.isEmpty == true) {
youtubeDownloadTasksMap.value.remove(groupName);
downloadTasksGroupDB.deleteEverything().catchError((_) {});
}
} else {
itemsConfig.loop((c) {
youtubeDownloadTasksMap.value[groupName]![c.filename] = c;
youtubeDownloadTasksInQueueMap[groupName]![c.filename] = true;
downloadTasksGroupDB.put(c.filename.filename, c.toJson());
});
await downloadTasksGroupDB.putAllAsync(
itemsConfig,
(c) {
youtubeDownloadTasksMap.value[groupName]![c.filename] = c;
youtubeDownloadTasksInQueueMap[groupName]![c.filename] = true; // hehe
final key = c.filename.key;
return MapEntry(key, c.toJson());
},
);
}

youtubeDownloadTasksMap.refresh();
Expand Down
19 changes: 12 additions & 7 deletions lib/youtube/controller/youtube_current_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,30 @@ class _YoutubeCurrentInfoController {
_isCurrentCommentsFromCache.value = null;
}

bool updateVideoPageSync(String videoId) {
Future<bool> updateVideoPageCache(String videoId) async {
final vidcache = YoutiPie.cacheBuilder.forVideoPage(videoId: videoId);
final vidPageCached = vidcache.read();
final vidPageCached = await vidcache.readAsync();
if (!_canSafelyModifyMetadata(videoId)) return false;
_currentVideoPage.value = vidPageCached;

final chId = vidPageCached?.channelInfo?.id ?? YoutubeInfoController.utils.getVideoChannelID(videoId);
final chPage = chId == null ? null : YoutiPie.cacheBuilder.forChannel(channelId: chId).read();
final chPage = chId == null ? null : await YoutiPie.cacheBuilder.forChannel(channelId: chId).readAsync();
if (!_canSafelyModifyMetadata(videoId)) return false;
_currentChannelPage.value = chPage;

final relatedcache = YoutiPie.cacheBuilder.forRelatedVideos(videoId: videoId);
_currentRelatedVideos.value = relatedcache.read() ?? vidPageCached?.relatedVideosResult;
final relatedVideos = await relatedcache.readAsync() ?? vidPageCached?.relatedVideosResult;
if (!_canSafelyModifyMetadata(videoId)) return false;
_currentRelatedVideos.value = relatedVideos;
return vidPageCached != null;
}

bool updateCurrentCommentsSync(String videoId) {
Future<bool> updateCurrentCommentsCache(String videoId) async {
final commcache = YoutiPie.cacheBuilder.forComments(videoId: videoId);
final comms = commcache.read();
final comms = await commcache.readAsync();
if (!_canSafelyModifyMetadata(videoId)) return false;
_currentComments.value = comms;
if (_currentComments.value != null) _isCurrentCommentsFromCache.value = true;
_isCurrentCommentsFromCache.value = _currentComments.value != null;
return comms != null;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/youtube/controller/youtube_info_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class YoutubeInfoController {
final currentId = Player.inst.currentVideo?.id;
current.resetAll();
if (currentId != null) {
current.updateVideoPageSync(currentId);
current.updateCurrentCommentsSync(currentId);
current.updateVideoPageCache(currentId);
current.updateCurrentCommentsCache(currentId);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/youtube/pages/yt_search_results_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class YoutubeSearchResultsPageState extends State<YoutubeSearchResultsPage> with
result = await YoutubeInfoController.search.search(newSearch, details: ExecuteDetails.forceRequest());
_cachedSearchResults = false;
} else {
result = YoutubeInfoController.search.searchSync(newSearch);
result = await YoutubeInfoController.search.search(newSearch);
_cachedSearchResults = result != null;
}

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: 4.6.1-beta+241027217
version: 4.6.13-beta+241102000

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit c093e6e

Please sign in to comment.