fix: Add to Playlist Dialog memory leak #817

This commit is contained in:
Kingkor Roy Tirtho 2023-11-14 19:36:07 +06:00
parent 7b72a90bc6
commit fed36ecdd8
2 changed files with 35 additions and 24 deletions

View File

@ -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';
@ -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>{});
@ -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,

View File

@ -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,