RhythmBox/lib/screens/explore.dart

158 lines
5.0 KiB
Dart
Raw Normal View History

2024-08-26 05:29:17 +00:00
import 'package:flutter/material.dart';
2024-08-30 04:56:28 +00:00
import 'package:gap/gap.dart';
2024-08-26 12:26:07 +00:00
import 'package:get/get.dart';
2024-08-30 05:43:29 +00:00
import 'package:intl/intl.dart';
import 'package:rhythm_box/providers/auth.dart';
2024-08-29 17:38:02 +00:00
import 'package:rhythm_box/providers/recent_played.dart';
2024-08-26 12:26:07 +00:00
import 'package:rhythm_box/providers/spotify.dart';
2024-08-29 17:38:02 +00:00
import 'package:rhythm_box/providers/user_preferences.dart';
import 'package:rhythm_box/services/album.dart';
import 'package:rhythm_box/services/database/database.dart';
2024-08-30 05:43:29 +00:00
import 'package:rhythm_box/services/spotify/spotify_endpoints.dart';
2024-08-29 17:38:02 +00:00
import 'package:rhythm_box/widgets/playlist/playlist_section.dart';
2024-08-30 05:43:29 +00:00
import 'package:spotify/spotify.dart';
2024-08-26 05:29:17 +00:00
class ExploreScreen extends StatefulWidget {
const ExploreScreen({super.key});
@override
State<ExploreScreen> createState() => _ExploreScreenState();
}
class _ExploreScreenState extends State<ExploreScreen> {
2024-08-26 12:26:07 +00:00
late final SpotifyProvider _spotify = Get.find();
2024-08-29 17:38:02 +00:00
late final RecentlyPlayedProvider _history = Get.find();
2024-08-30 05:43:29 +00:00
late final AuthenticationProvider _auth = Get.find();
2024-08-26 12:26:07 +00:00
2024-08-29 17:38:02 +00:00
final Map<String, bool> _isLoading = {
'featured': true,
'recently': true,
'newReleases': true,
2024-08-30 05:43:29 +00:00
'forYou': true,
2024-08-29 17:38:02 +00:00
};
2024-08-26 12:26:07 +00:00
2024-08-29 17:38:02 +00:00
List<Object>? _featuredPlaylist;
List<Object>? _recentlyPlaylist;
List<Object>? _newReleasesPlaylist;
2024-08-30 05:43:29 +00:00
List<dynamic>? _forYouView;
2024-08-26 12:26:07 +00:00
Future<void> _pullPlaylist() async {
2024-08-29 17:38:02 +00:00
final market = Get.find<UserPreferencesProvider>().state.value.market;
2024-08-30 05:43:29 +00:00
final locale = Get.find<UserPreferencesProvider>().state.value.locale;
2024-08-29 17:38:02 +00:00
2024-08-26 12:26:07 +00:00
_featuredPlaylist =
2024-08-27 06:35:16 +00:00
(await _spotify.api.playlists.featured.getPage(20)).items!.toList();
2024-09-02 12:42:33 +00:00
if (mounted) {
setState(() => _isLoading['featured'] = false);
} else {
return;
}
2024-08-29 17:38:02 +00:00
2024-09-02 12:42:33 +00:00
final idxList = Set();
2024-08-29 17:38:02 +00:00
_recentlyPlaylist = (await _history.fetch())
.where((x) => x.playlist != null)
.map((x) => x.playlist!)
2024-09-02 12:42:33 +00:00
.toList()
..retainWhere((x) => idxList.add(x.id!));
if (mounted) {
setState(() => _isLoading['recently'] = false);
} else {
return;
}
2024-08-29 17:38:02 +00:00
_newReleasesPlaylist =
(await _spotify.api.browse.newReleases(country: market).getPage(20))
.items
?.map((album) => album.toAlbum())
.toList();
2024-09-02 12:42:33 +00:00
if (mounted) {
setState(() => _isLoading['newReleases'] = false);
} else {
return;
}
2024-08-30 05:43:29 +00:00
2024-09-04 15:28:59 +00:00
if (_auth.auth.value != null) {
2024-09-06 04:41:35 +00:00
final customEndpoint = CustomSpotifyEndpoints(
_auth.auth.value?.spotifyAccessToken.value ?? '');
2024-09-04 15:28:59 +00:00
final forYouView = await customEndpoint.getView(
'made-for-x-hub',
market: market,
locale: Intl.canonicalizedLocale(locale.toString()),
);
_forYouView = forYouView['content']?['items'];
}
2024-09-02 12:42:33 +00:00
if (mounted) {
setState(() => _isLoading['forYou'] = false);
} else {
return;
}
2024-08-26 12:26:07 +00:00
}
@override
void initState() {
super.initState();
_pullPlaylist();
}
2024-08-26 05:29:17 +00:00
@override
Widget build(BuildContext context) {
2024-08-26 12:26:07 +00:00
return Material(
color: Theme.of(context).colorScheme.surface,
child: Scaffold(
appBar: AppBar(
title: Text('explore'.tr),
2024-08-28 17:45:33 +00:00
centerTitle: MediaQuery.of(context).size.width >= 720,
2024-08-26 12:26:07 +00:00
),
2024-08-30 05:43:29 +00:00
body: CustomScrollView(
slivers: [
2024-08-29 17:38:02 +00:00
if (_recentlyPlaylist?.isNotEmpty ?? false)
2024-08-30 05:43:29 +00:00
SliverToBoxAdapter(
child: PlaylistSection(
isLoading: _isLoading['recently']!,
title: 'Recent Played',
list: _recentlyPlaylist,
),
),
if (_recentlyPlaylist?.isNotEmpty ?? false) const SliverGap(16),
2024-08-30 15:18:49 +00:00
if (_newReleasesPlaylist?.isNotEmpty ?? false)
SliverToBoxAdapter(
child: PlaylistSection(
isLoading: _isLoading['newReleases']!,
title: 'New Releases',
list: _newReleasesPlaylist,
),
),
if (_newReleasesPlaylist?.isNotEmpty ?? false) const SliverGap(16),
2024-08-30 05:43:29 +00:00
SliverList.builder(
itemCount: _forYouView?.length ?? 0,
itemBuilder: (context, idx) {
final item = _forYouView![idx];
final playlists = item['content']?['items']
?.where((itemL2) => itemL2['type'] == 'playlist')
.map((itemL2) => PlaylistSimple.fromJson(itemL2))
.toList()
.cast<PlaylistSimple>() ??
<PlaylistSimple>[];
if (playlists.isEmpty) return const SizedBox.shrink();
return PlaylistSection(
isLoading: false,
title: item['name'] ?? '',
list: playlists,
).paddingOnly(bottom: 16);
},
),
SliverToBoxAdapter(
child: PlaylistSection(
isLoading: _isLoading['featured']!,
title: 'Featured',
list: _featuredPlaylist,
2024-08-29 17:38:02 +00:00
),
2024-08-28 17:45:33 +00:00
),
2024-08-30 05:43:29 +00:00
const SliverGap(16),
2024-08-29 17:38:02 +00:00
],
2024-08-26 12:26:07 +00:00
),
),
);
2024-08-26 05:29:17 +00:00
}
}