RhythmBox/lib/screens/playlist/view.dart

245 lines
9.5 KiB
Dart
Raw Normal View History

2024-08-28 12:34:35 +00:00
import 'dart:math';
2024-08-26 12:26:07 +00:00
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
2024-08-28 12:34:35 +00:00
import 'package:rhythm_box/providers/audio_player.dart';
import 'package:rhythm_box/providers/history.dart';
2024-08-26 12:26:07 +00:00
import 'package:rhythm_box/providers/spotify.dart';
2024-08-28 12:34:35 +00:00
import 'package:rhythm_box/services/audio_player/audio_player.dart';
2024-08-26 12:26:07 +00:00
import 'package:rhythm_box/widgets/auto_cache_image.dart';
2024-08-28 17:45:33 +00:00
import 'package:rhythm_box/widgets/sized_container.dart';
2024-08-26 12:26:07 +00:00
import 'package:rhythm_box/widgets/tracks/playlist_track_list.dart';
import 'package:spotify/spotify.dart';
class PlaylistViewScreen extends StatefulWidget {
final String playlistId;
final Playlist? playlist;
const PlaylistViewScreen({
super.key,
required this.playlistId,
this.playlist,
});
@override
State<PlaylistViewScreen> createState() => _PlaylistViewScreenState();
}
class _PlaylistViewScreenState extends State<PlaylistViewScreen> {
late final SpotifyProvider _spotify = Get.find();
2024-08-28 12:34:35 +00:00
late final AudioPlayerProvider _playback = Get.find();
bool get _isCurrentPlaylist => _playlist != null
? _playback.state.value.containsCollection(_playlist!.id!)
: false;
2024-08-26 12:26:07 +00:00
bool _isLoading = true;
2024-08-29 08:42:48 +00:00
bool _isLoadingTracks = true;
2024-08-28 12:34:35 +00:00
bool _isUpdating = false;
2024-08-26 12:26:07 +00:00
Playlist? _playlist;
2024-08-29 08:42:48 +00:00
List<Track>? _tracks;
2024-08-26 12:26:07 +00:00
Future<void> _pullPlaylist() async {
2024-08-29 08:42:48 +00:00
if (widget.playlistId == 'user-liked-tracks') {
_playlist = Playlist()
..name = 'Liked Music'
..description = 'Your favorite music'
..type = 'playlist'
..collaborative = false
..public = false
..id = 'user-liked-tracks';
} else {
_playlist = await _spotify.api.playlists.get(widget.playlistId);
}
2024-08-26 12:26:07 +00:00
setState(() => _isLoading = false);
}
2024-08-29 08:42:48 +00:00
Future<void> _pullTracks() async {
if (widget.playlistId == 'user-liked-tracks') {
_tracks = (await _spotify.api.tracks.me.saved.all())
.map((x) => x.track!)
.toList();
} else {
_tracks = (await _spotify.api.playlists
.getTracksByPlaylistId(widget.playlistId)
.all())
.toList();
}
setState(() => _isLoadingTracks = false);
}
2024-08-26 12:26:07 +00:00
@override
void initState() {
super.initState();
_pullPlaylist();
2024-08-29 08:42:48 +00:00
_pullTracks();
2024-08-26 12:26:07 +00:00
}
@override
Widget build(BuildContext context) {
const radius = BorderRadius.all(Radius.circular(8));
return Material(
color: Theme.of(context).colorScheme.surface,
child: Scaffold(
appBar: AppBar(
title: const Text('Playlist'),
2024-08-28 17:45:33 +00:00
centerTitle: MediaQuery.of(context).size.width >= 720,
2024-08-26 12:26:07 +00:00
),
body: Builder(
builder: (context) {
if (_isLoading) {
return const Center(
child: CircularProgressIndicator(),
);
}
2024-08-28 17:45:33 +00:00
return CenteredContainer(
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Material(
2024-08-26 12:26:07 +00:00
borderRadius: radius,
2024-08-28 17:45:33 +00:00
elevation: 2,
child: ClipRRect(
borderRadius: radius,
2024-08-29 08:42:48 +00:00
child: (_playlist?.images?.isNotEmpty ?? false)
? AutoCacheImage(
_playlist!.images!.first.url!,
width: 160.0,
height: 160.0,
)
: const SizedBox(
width: 160,
height: 160,
child: Icon(Icons.image),
),
2024-08-26 12:26:07 +00:00
),
),
2024-08-28 17:45:33 +00:00
const Gap(24),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_playlist!.name ?? 'Playlist',
style: Theme.of(context)
.textTheme
.headlineSmall,
maxLines: 2,
overflow: TextOverflow.fade,
),
Text(
_playlist!.description ?? 'A Playlist',
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
const Gap(8),
Text(
2024-08-29 08:42:48 +00:00
"${NumberFormat.compactCurrency(symbol: '', decimalDigits: 2).format(_playlist!.followers?.total! ?? 0)} saves",
2024-08-28 17:45:33 +00:00
),
Text(
'#${_playlist!.id}',
style: GoogleFonts.robotoMono(fontSize: 10),
),
],
),
2024-08-26 12:26:07 +00:00
),
2024-08-28 17:45:33 +00:00
],
).paddingOnly(left: 24, right: 24, top: 24),
const Gap(8),
Wrap(
spacing: 8,
children: [
Obx(
() => ElevatedButton.icon(
icon: (_isCurrentPlaylist &&
_playback.isPlaying.value)
? const Icon(Icons.pause_outlined)
: const Icon(Icons.play_arrow),
label: const Text('Play'),
onPressed: _isUpdating
? null
: () async {
if (_isCurrentPlaylist &&
_playback.isPlaying.value) {
audioPlayer.pause();
return;
} else if (_isCurrentPlaylist &&
!_playback.isPlaying.value) {
audioPlayer.resume();
return;
}
setState(() => _isUpdating = true);
2024-08-29 08:42:48 +00:00
await _playback.load(_tracks!,
2024-08-28 17:45:33 +00:00
autoPlay: true);
_playback.addCollection(_playlist!.id!);
Get.find<PlaybackHistoryProvider>()
.addPlaylists([_playlist!]);
setState(() => _isUpdating = false);
},
),
),
TextButton.icon(
icon: const Icon(Icons.shuffle),
label: const Text('Shuffle'),
2024-08-28 12:34:35 +00:00
onPressed: _isUpdating
? null
: () async {
setState(() => _isUpdating = true);
2024-08-28 17:45:33 +00:00
audioPlayer.setShuffle(true);
await _playback.load(
2024-08-29 08:42:48 +00:00
_tracks!,
2024-08-28 17:45:33 +00:00
autoPlay: true,
initialIndex:
2024-08-29 08:42:48 +00:00
Random().nextInt(_tracks!.length),
2024-08-28 17:45:33 +00:00
);
2024-08-28 12:34:35 +00:00
_playback.addCollection(_playlist!.id!);
Get.find<PlaybackHistoryProvider>()
.addPlaylists([_playlist!]);
setState(() => _isUpdating = false);
},
),
2024-08-28 17:45:33 +00:00
],
).paddingSymmetric(horizontal: 24),
const Gap(24),
],
),
),
SliverToBoxAdapter(
child: Text(
2024-08-29 08:42:48 +00:00
'Songs (${_playlist!.tracks?.total ?? (_tracks?.length ?? 0)})',
2024-08-28 17:45:33 +00:00
style: Theme.of(context).textTheme.titleLarge,
).paddingOnly(left: 28, right: 28, bottom: 4),
2024-08-26 12:26:07 +00:00
),
2024-08-29 08:42:48 +00:00
PlaylistTrackList(
isLoading: _isLoadingTracks,
playlistId: widget.playlistId,
tracks: _tracks,
),
2024-08-28 17:45:33 +00:00
],
),
2024-08-26 12:26:07 +00:00
);
},
),
),
);
}
}