mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
148 lines
4.9 KiB
Dart
148 lines
4.9 KiB
Dart
import 'package:flutter/services.dart';
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:spotube/components/shared/heart_button.dart';
|
|
import 'package:spotube/components/shared/track_table/track_collection_view.dart';
|
|
import 'package:spotube/components/shared/track_table/tracks_table_view.dart';
|
|
import 'package:spotube/extensions/constrains.dart';
|
|
import 'package:spotube/models/logger.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:spotify/spotify.dart';
|
|
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
|
|
import 'package:spotube/services/queries/queries.dart';
|
|
|
|
import 'package:spotube/utils/service_utils.dart';
|
|
import 'package:spotube/utils/type_conversion_utils.dart';
|
|
|
|
class PlaylistView extends HookConsumerWidget {
|
|
final logger = getLogger(PlaylistView);
|
|
final PlaylistSimple playlist;
|
|
PlaylistView(this.playlist, {Key? key}) : super(key: key);
|
|
|
|
Future<void> playPlaylist(
|
|
List<Track> tracks,
|
|
WidgetRef ref, {
|
|
Track? currentTrack,
|
|
}) async {
|
|
final proxyPlaylist = ref.read(ProxyPlaylistNotifier.provider);
|
|
final playback = ref.read(ProxyPlaylistNotifier.notifier);
|
|
final sortBy = ref.read(trackCollectionSortState(playlist.id!));
|
|
final sortedTracks = ServiceUtils.sortTracks(tracks, sortBy);
|
|
currentTrack ??= sortedTracks.first;
|
|
final isPlaylistPlaying = proxyPlaylist.containsTracks(tracks);
|
|
if (!isPlaylistPlaying) {
|
|
await playback.load(
|
|
sortedTracks,
|
|
initialIndex: sortedTracks.indexWhere((s) => s.id == currentTrack?.id),
|
|
autoPlay: true,
|
|
);
|
|
} else if (isPlaylistPlaying &&
|
|
currentTrack.id != null &&
|
|
currentTrack.id != proxyPlaylist.activeTrack?.id) {
|
|
await playback.jumpToTrack(currentTrack);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context, ref) {
|
|
final proxyPlaylist = ref.watch(ProxyPlaylistNotifier.provider);
|
|
final playlistNotifier = ref.watch(ProxyPlaylistNotifier.notifier);
|
|
|
|
final mediaQuery = MediaQuery.of(context);
|
|
|
|
final meSnapshot = useQueries.user.me(ref);
|
|
final tracksSnapshot = useQueries.playlist.tracksOfQuery(ref, playlist.id!);
|
|
|
|
final isPlaylistPlaying = useMemoized(
|
|
() => proxyPlaylist.containsTracks(tracksSnapshot.data ?? []),
|
|
[playlistNotifier, tracksSnapshot.data],
|
|
);
|
|
|
|
final titleImage = useMemoized(
|
|
() => TypeConversionUtils.image_X_UrlString(
|
|
playlist.images,
|
|
placeholder: ImagePlaceholder.collection,
|
|
),
|
|
[playlist.images]);
|
|
|
|
return TrackCollectionView(
|
|
id: playlist.id!,
|
|
isPlaying: isPlaylistPlaying,
|
|
title: playlist.name!,
|
|
titleImage: titleImage,
|
|
tracksSnapshot: tracksSnapshot,
|
|
description: playlist.description,
|
|
isOwned: playlist.owner?.id != null &&
|
|
playlist.owner!.id == meSnapshot.data?.id,
|
|
onPlay: ([track]) {
|
|
if (tracksSnapshot.hasData) {
|
|
if (!isPlaylistPlaying) {
|
|
playPlaylist(
|
|
tracksSnapshot.data!,
|
|
ref,
|
|
currentTrack: track,
|
|
);
|
|
} else if (isPlaylistPlaying && track != null) {
|
|
playPlaylist(
|
|
tracksSnapshot.data!,
|
|
ref,
|
|
currentTrack: track,
|
|
);
|
|
} else {
|
|
playlistNotifier
|
|
.removeTracks(tracksSnapshot.data!.map((e) => e.id!));
|
|
}
|
|
}
|
|
},
|
|
onAddToQueue: () {
|
|
if (tracksSnapshot.hasData && !isPlaylistPlaying) {
|
|
playlistNotifier.addTracks(tracksSnapshot.data!);
|
|
}
|
|
},
|
|
bottomSpace: mediaQuery.isSm || mediaQuery.isMd,
|
|
showShare: playlist.id != "user-liked-tracks",
|
|
routePath: "/playlist/${playlist.id}",
|
|
onShare: () {
|
|
final data = "https://open.spotify.com/playlist/${playlist.id}";
|
|
Clipboard.setData(
|
|
ClipboardData(text: data),
|
|
).then((_) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
width: 300,
|
|
behavior: SnackBarBehavior.floating,
|
|
content: Text(
|
|
"Copied $data to clipboard",
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
);
|
|
});
|
|
},
|
|
heartBtn: PlaylistHeartButton(playlist: playlist),
|
|
onShuffledPlay: ([track]) {
|
|
final tracks = [...?tracksSnapshot.data]..shuffle();
|
|
|
|
if (tracksSnapshot.hasData) {
|
|
if (!isPlaylistPlaying) {
|
|
playPlaylist(
|
|
tracks,
|
|
ref,
|
|
currentTrack: track,
|
|
);
|
|
} else if (isPlaylistPlaying && track != null) {
|
|
playPlaylist(
|
|
tracks,
|
|
ref,
|
|
currentTrack: track,
|
|
);
|
|
} else {
|
|
// TODO: Remove the ability to stop the playlist
|
|
// playlistNotifier.stop();
|
|
}
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|