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

PlaylistView/SearchAlbumView are responsive now ArtistProfile album view & tracks view are paginated now
101 lines
3.6 KiB
Dart
101 lines
3.6 KiB
Dart
import 'package:flutter/material.dart' hide Page;
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
|
import 'package:spotify/spotify.dart';
|
|
import 'package:spotube/components/Playlist/PlaylistCard.dart';
|
|
import 'package:spotube/hooks/usePagingController.dart';
|
|
import 'package:spotube/provider/SpotifyDI.dart';
|
|
|
|
class CategoryCard extends HookWidget {
|
|
final Category category;
|
|
final Iterable<PlaylistSimple>? playlists;
|
|
const CategoryCard(
|
|
this.category, {
|
|
Key? key,
|
|
this.playlists,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
category.name ?? "Unknown",
|
|
style: Theme.of(context).textTheme.headline5,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
HookConsumer(
|
|
builder: (context, ref, child) {
|
|
SpotifyApi spotifyApi = ref.watch(spotifyProvider);
|
|
final scrollController = useScrollController();
|
|
final pagingController =
|
|
usePagingController<int, PlaylistSimple>(firstPageKey: 0);
|
|
|
|
final _error = useState(false);
|
|
|
|
useEffect(() {
|
|
listener(pageKey) async {
|
|
try {
|
|
if (playlists != null && playlists?.isNotEmpty == true) {
|
|
return pagingController.appendLastPage(playlists!.toList());
|
|
}
|
|
final Page<PlaylistSimple> page = await (category.id !=
|
|
"user-featured-playlists"
|
|
? spotifyApi.playlists.getByCategoryId(category.id!)
|
|
: spotifyApi.playlists.featured)
|
|
.getPage(3, pageKey);
|
|
|
|
if (page.isLast && page.items != null) {
|
|
pagingController.appendLastPage(page.items!.toList());
|
|
} else if (page.items != null) {
|
|
pagingController.appendPage(
|
|
page.items!.toList(), page.nextOffset);
|
|
}
|
|
if (_error.value) _error.value = false;
|
|
} catch (e, stack) {
|
|
if (!_error.value) _error.value = true;
|
|
pagingController.error = e;
|
|
print(
|
|
"[CategoryCard.pagingController.addPageRequestListener] $e");
|
|
print(stack);
|
|
}
|
|
}
|
|
|
|
pagingController.addPageRequestListener(listener);
|
|
return () {
|
|
pagingController.removePageRequestListener(listener);
|
|
};
|
|
}, [_error]);
|
|
|
|
if (_error.value) return const Text("Something Went Wrong");
|
|
return SizedBox(
|
|
height: 245,
|
|
child: Scrollbar(
|
|
controller: scrollController,
|
|
child: PagedListView<int, PlaylistSimple>(
|
|
shrinkWrap: true,
|
|
pagingController: pagingController,
|
|
scrollController: scrollController,
|
|
scrollDirection: Axis.horizontal,
|
|
builderDelegate: PagedChildBuilderDelegate<PlaylistSimple>(
|
|
itemBuilder: (context, playlist, index) {
|
|
return PlaylistCard(playlist);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
)
|
|
],
|
|
);
|
|
}
|
|
}
|