Skip to content

Commit

Permalink
fix: Add to Playlist Dialog memory leak #817
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Nov 14, 2023
1 parent 7b72a90 commit fed36ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
36 changes: 12 additions & 24 deletions lib/components/shared/dialogs/playlist_add_track_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:async/async.dart';
import 'package:fl_query_hooks/fl_query_hooks.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
Expand All @@ -21,32 +20,21 @@ class PlaylistAddTrackDialog extends HookConsumerWidget {
@override
Widget build(BuildContext context, ref) {
final spotify = ref.watch(spotifyProvider);
final userPlaylists = useQueries.playlist.ofMine(ref);

useEffect(() {
final op = CancelableOperation.fromFuture(
() async {
while (userPlaylists.hasNextPage) {
await userPlaylists.fetchNext();
}
}(),
);

return () {
op.cancel();
};
}, [userPlaylists.hasNextPage]);
final userPlaylists = useQueries.playlist.ofMineAll(ref);

final me = useQueries.user.me(ref);

final filteredPlaylists = useMemoized(
() => userPlaylists.pages
.expand((page) => page.items?.toList() ?? <PlaylistSimple>[])
.where(
(playlist) =>
playlist.owner?.id != null && playlist.owner!.id == me.data?.id,
),
[userPlaylists.pages, me.data?.id],
() =>
userPlaylists.data
?.where(
(playlist) =>
playlist.owner?.id != null &&
playlist.owner!.id == me.data?.id,
)
.toList() ??
[],
[userPlaylists.data, me.data?.id],
);

final playlistsCheck = useState(<String, bool>{});
Expand Down Expand Up @@ -93,7 +81,7 @@ class PlaylistAddTrackDialog extends HookConsumerWidget {
content: SizedBox(
height: 300,
width: 300,
child: userPlaylists.hasNextPage
child: userPlaylists.isLoading
? const Center(child: CircularProgressIndicator())
: ListView.builder(
shrinkWrap: true,
Expand Down
23 changes: 23 additions & 0 deletions lib/services/queries/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ class PlaylistQueries {
);
}

Query<List<PlaylistSimple>, dynamic> ofMineAll(WidgetRef ref) {
return useSpotifyQuery<List<PlaylistSimple>, dynamic>(
"current-user-all-playlists",
(spotify) async {
var page = await spotify.playlists.me.getPage(50);
final playlists = <PlaylistSimple>[];

if (page.isLast == true) {
return page.items?.toList() ?? [];
}

playlists.addAll(page.items ?? []);
while (!page.isLast) {
page = await spotify.playlists.me.getPage(50, page.nextOffset);
playlists.addAll(page.items ?? []);
}

return playlists;
},
ref: ref,
);
}

Future<List<Track>> likedTracks(
SpotifyApi spotify,
WidgetRef ref,
Expand Down

0 comments on commit fed36ec

Please sign in to comment.