RhythmBox/lib/screens/explore.dart

96 lines
2.9 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-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';
import 'package:rhythm_box/widgets/playlist/playlist_section.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-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-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-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-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-08-29 17:38:02 +00:00
setState(() => _isLoading['featured'] = false);
_recentlyPlaylist = (await _history.fetch())
.where((x) => x.playlist != null)
.map((x) => x.playlist!)
.toList();
setState(() => _isLoading['recently'] = false);
_newReleasesPlaylist =
(await _spotify.api.browse.newReleases(country: market).getPage(20))
.items
?.map((album) => album.toAlbum())
.toList();
setState(() => _isLoading['newReleases'] = false);
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-29 17:38:02 +00:00
body: ListView(
children: [
if (_newReleasesPlaylist?.isNotEmpty ?? false)
PlaylistSection(
isLoading: _isLoading['newReleases']!,
title: 'New Releases',
list: _newReleasesPlaylist,
),
2024-08-30 04:56:28 +00:00
if (_newReleasesPlaylist?.isNotEmpty ?? false) const Gap(16),
2024-08-29 17:38:02 +00:00
if (_recentlyPlaylist?.isNotEmpty ?? false)
PlaylistSection(
isLoading: _isLoading['recently']!,
title: 'Recent Played',
list: _recentlyPlaylist,
),
2024-08-30 04:56:28 +00:00
if (_recentlyPlaylist?.isNotEmpty ?? false) const Gap(16),
2024-08-29 17:38:02 +00:00
PlaylistSection(
isLoading: _isLoading['featured']!,
title: 'Featured',
list: _featuredPlaylist,
2024-08-28 17:45:33 +00:00
),
2024-08-30 04:56:28 +00:00
const Gap(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
}
}