mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 16:05:18 +00:00

Configuration File Removals: .vscode/c_cpp_properties.json: Removed the entire configuration for C/C++ properties. .vscode/launch.json: Removed the Dart launch configurations for different environments and modes. .vscode/settings.json: Removed settings related to CMake, spell checking, file nesting, and Dart Flutter SDK path. .vscode/snippets.code-snippets: Removed code snippets for Dart, including PaginatedState and PaginatedNotifier templates. .vscode/tasks.json: Removed the tasks configuration file. Documentation Updates: CONTRIBUTION.md: Removed heart emoji from the introductory text. README.md: Updated the logo image and made minor text adjustments, including removing emojis and updating section titles. [1] [2] [3] [4] [5] Asset Removals: lib/collections/assets.gen.dart: Removed multiple unused asset references, including images related to Spotube logos and banners. [1] [2] [3] Minor Code Cleanups: cli/commands/build/linux.dart, cli/commands/build/windows.dart, cli/commands/translated.dart, cli/commands/untranslated.dart: Adjusted import statements for consistency. [1] [2] [3] [4] integration_test/app_test.dart: Removed an unnecessary blank line. lib/collections/routes.dart: Commented out the TrackRoute configuration.
91 lines
3.2 KiB
Dart
91 lines
3.2 KiB
Dart
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:spotify/spotify.dart';
|
|
import 'package:spotube/provider/audio_player/audio_player.dart';
|
|
import 'package:spotube/provider/authentication/authentication.dart';
|
|
import 'package:spotube/provider/spotify/spotify.dart';
|
|
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
|
|
import 'package:spotube/services/audio_player/audio_player.dart';
|
|
import 'package:spotube/services/logger/logger.dart';
|
|
|
|
void useEndlessPlayback(WidgetRef ref) {
|
|
final auth = ref.watch(authenticationProvider);
|
|
final playback = ref.watch(audioPlayerProvider.notifier);
|
|
final playlist = ref.watch(audioPlayerProvider.select((s) => s.playlist));
|
|
final spotify = ref.watch(spotifyProvider);
|
|
final endlessPlayback =
|
|
ref.watch(userPreferencesProvider.select((s) => s.endlessPlayback));
|
|
|
|
useEffect(
|
|
() {
|
|
if (!endlessPlayback || auth.asData?.value == null) return null;
|
|
|
|
void listener(int index) async {
|
|
try {
|
|
final playlist = ref.read(audioPlayerProvider);
|
|
if (index != playlist.tracks.length - 1) return;
|
|
|
|
final track = playlist.tracks.last;
|
|
|
|
final query = "${track.name} Radio";
|
|
final pages = await spotify.invoke((api) =>
|
|
api.search.get(query, types: [SearchType.playlist]).first());
|
|
|
|
final radios = pages
|
|
.expand((e) => e.items?.toList() ?? <PlaylistSimple>[])
|
|
.toList()
|
|
.cast<PlaylistSimple>();
|
|
|
|
final artists = track.artists!.map((e) => e.name);
|
|
|
|
final radio = radios.firstWhere(
|
|
(e) {
|
|
final validPlaylists =
|
|
artists.where((a) => e.description!.contains(a!));
|
|
return e.name == "${track.name} Radio" &&
|
|
(validPlaylists.length >= 2 ||
|
|
validPlaylists.length == artists.length) &&
|
|
e.owner?.displayName != "Spotify";
|
|
},
|
|
orElse: () => radios.first,
|
|
);
|
|
|
|
final tracks = await spotify.invoke(
|
|
(api) => api.playlists.getTracksByPlaylistId(radio.id!).all());
|
|
|
|
await playback.addTracks(
|
|
tracks.toList()
|
|
..removeWhere((e) {
|
|
final playlist = ref.read(audioPlayerProvider);
|
|
final isDuplicate = playlist.tracks.any((t) => t.id == e.id);
|
|
return e.id == track.id || isDuplicate;
|
|
}),
|
|
);
|
|
} catch (e, stack) {
|
|
AppLogger.reportError(e, stack);
|
|
}
|
|
}
|
|
|
|
// Sometimes user can change settings for which the currentIndexChanged
|
|
// might not be called. So we need to check if the current track is the
|
|
// last track and if it is then we need to call the listener manually.
|
|
if (playlist.index == playlist.medias.length - 1 &&
|
|
audioPlayer.isPlaying) {
|
|
listener(playlist.index);
|
|
}
|
|
|
|
final subscription =
|
|
audioPlayer.currentIndexChangedStream.listen(listener);
|
|
|
|
return subscription.cancel;
|
|
},
|
|
[
|
|
spotify,
|
|
playback,
|
|
playlist.medias,
|
|
endlessPlayback,
|
|
auth,
|
|
],
|
|
);
|
|
}
|