Skip to content

Commit

Permalink
perf: StringBuffer for building lrc obtained from search
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Sep 17, 2024
1 parent 0df242c commit 7063e7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lib/controller/lyrics_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -333,25 +333,25 @@ class _LRCSearchManager with PortsProvider<SendPort> {
final responseBody = await utf8.decodeStream(response.asBroadcastStream());
final fetched = <LyricsModel>[];
final jsonLists = (jsonDecode(responseBody) as List<dynamic>?) ?? [];
for (final jsonRes in jsonLists) {
jsonLists.loop((jsonRes) {
final syncedLyrics = jsonRes?["syncedLyrics"] as String? ?? '';
final plain = jsonRes?["plainLyrics"] as String? ?? '';
if (syncedLyrics != '') {
// lrc
final lines = <String>[];
final lrcBuffer = StringBuffer();
final artist = jsonRes['artistName'] ?? details?.artist ?? '';
final album = jsonRes['albumName'] ?? details?.album ?? '';
final title = jsonRes['trackName'] ?? details?.title ?? '';
final durMS = jsonRes['duration'] is num ? ((jsonRes['duration'] as num) * 1000).round() : details?.durationMS ?? 0;

if (artist != '') lines.add('[ar:$artist]');
if (album != '') lines.add('[al:$album]');
if (title != '') lines.add('[ti:$title]');
if (durMS > 0) lines.add('[length:${formatTime(durMS)}]');
for (final l in syncedLyrics.split('\n')) {
lines.add(l);
}
final resultedLRC = lines.join('\n');
if (artist != '') lrcBuffer.writeln('[ar:$artist]');
if (album != '') lrcBuffer.writeln('[al:$album]');
if (title != '') lrcBuffer.writeln('[ti:$title]');
if (durMS > 0) lrcBuffer.writeln('[length:${formatTime(durMS)}]');
lrcBuffer.write(syncedLyrics);

final resultedLRC = lrcBuffer.toString();

fetched.add(LyricsModel(
lyrics: resultedLRC,
isInCache: false,
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.4.0-beta+240917209
version: 4.4.05-beta+240917209

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

0 comments on commit 7063e7c

Please sign in to comment.