mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-12 23:45:18 +00:00

commit e160d4f561ff2e945fd67bf12b223c012da58b1e Author: Kingkor Roy Tirtho <krtirtho@gmail.com> Date: Sat Sep 14 10:48:08 2024 +0600 fix: pagination issues in playlist and album pages
71 lines
1.7 KiB
Dart
71 lines
1.7 KiB
Dart
part of '../spotify.dart';
|
|
|
|
class PlaylistTracksState extends PaginatedState<Track> {
|
|
PlaylistTracksState({
|
|
required super.items,
|
|
required super.offset,
|
|
required super.limit,
|
|
required super.hasMore,
|
|
});
|
|
|
|
@override
|
|
PlaylistTracksState copyWith({
|
|
List<Track>? items,
|
|
int? offset,
|
|
int? limit,
|
|
bool? hasMore,
|
|
}) {
|
|
return PlaylistTracksState(
|
|
items: items ?? this.items,
|
|
offset: offset ?? this.offset,
|
|
limit: limit ?? this.limit,
|
|
hasMore: hasMore ?? this.hasMore,
|
|
);
|
|
}
|
|
}
|
|
|
|
class PlaylistTracksNotifier extends AutoDisposeFamilyPaginatedAsyncNotifier<
|
|
Track, PlaylistTracksState, String> {
|
|
PlaylistTracksNotifier() : super();
|
|
|
|
@override
|
|
fetch(arg, offset, limit) async {
|
|
final tracks = await spotify.playlists
|
|
.getTracksByPlaylistId(arg)
|
|
.getPage(limit, offset);
|
|
|
|
/// Filter out tracks with null id because some personal playlists
|
|
/// may contain local tracks that are not available in the Spotify catalog
|
|
final items = tracks.items
|
|
?.where((track) => track.id != null && track.type == "track")
|
|
.toList() ??
|
|
<Track>[];
|
|
|
|
return (
|
|
items: items,
|
|
hasMore: !tracks.isLast,
|
|
nextOffset: tracks.nextOffset,
|
|
);
|
|
}
|
|
|
|
@override
|
|
build(arg) async {
|
|
ref.cacheFor();
|
|
|
|
ref.watch(spotifyProvider);
|
|
final (items: tracks, :hasMore, :nextOffset) = await fetch(arg, 0, 20);
|
|
|
|
return PlaylistTracksState(
|
|
items: tracks,
|
|
offset: nextOffset,
|
|
limit: 20,
|
|
hasMore: hasMore,
|
|
);
|
|
}
|
|
}
|
|
|
|
final playlistTracksProvider = AutoDisposeAsyncNotifierProviderFamily<
|
|
PlaylistTracksNotifier, PlaylistTracksState, String>(
|
|
() => PlaylistTracksNotifier(),
|
|
);
|