|
1 | | -let SPOTIFY_API; |
2 | | -let RECENT_SPOTIFY_PLAYBACK_DEVICE_ID; |
3 | | -let SHUFFLE_MAX_BATCH_SAMPLE_SIZE = 50; |
4 | | -let RECENT_SPOTIFY_SHUFFLED_TRACKS = []; |
| 1 | +let SPOTIFY_API; // Globally stores the Spotify API instance |
| 2 | +let SPOTIFY_USER_IS_PREMIUM; // Caches whether the Spotify user is a Premium user or not |
| 3 | +let SHUFFLE_MAX_BATCH_SAMPLE_SIZE = 50; // The sample size for batch shuffling of tracks |
| 4 | +let RECENT_SPOTIFY_PLAYBACK_DEVICE_ID; // Caches the device id of the Spotify player that most recently played shuffled tracks |
| 5 | +let RECENT_SPOTIFY_SHUFFLED_TRACKS = []; // Caches the most recently shuffled tracks from Spotify |
5 | 6 |
|
6 | | -let TEMPORARY_SHUFFLED_PLAYLIST; |
7 | | -const TEMPORARY_SHUFFLED_PLAYLIST_NAME = 'True Shuffle Playlist'; |
8 | | -const TEMPORARY_SHUFFLED_PLAYLIST_DESCRIPTION = `An Automatically Generated Playlist By True Shuffle from ${location.origin}`; |
| 7 | +let TEMPORARY_SHUFFLED_PLAYLIST; // Caches the temporary shuffled playlist object to store free user shuffled tracks |
| 8 | +const TEMPORARY_SHUFFLED_PLAYLIST_NAME = 'True Shuffle Playlist'; // Default name for the temporary shuffled playlist |
| 9 | +const TEMPORARY_SHUFFLED_PLAYLIST_DESCRIPTION = `An Automatically Generated Playlist By True Shuffle from ${location.origin}`; // Default description for the temporary shuffled playlist |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * Begins the process of shuffling and playing music based on UI selected playlist/device. |
@@ -45,16 +46,21 @@ async function shuffle_and_play() { |
45 | 46 | RECENT_SPOTIFY_SHUFFLED_TRACKS = results; |
46 | 47 |
|
47 | 48 | // Safely determine if the user is a Premium user by disabling shuffle |
48 | | - let is_premium; |
49 | | - try { |
50 | | - ui_render_play_button('Preparing Player...', false); |
51 | | - is_premium = await SPOTIFY_API.set_playback_shuffle(device_id, false); |
52 | | - } catch (error) { |
53 | | - log('ERROR', 'Failed to set playback shuffle to disabled.'); |
54 | | - alert('Failed to disable playback shuffle in Spotify player.'); |
55 | | - return console.log(error); |
| 49 | + let is_premium = SPOTIFY_USER_IS_PREMIUM; |
| 50 | + if (is_premium === undefined) { |
| 51 | + try { |
| 52 | + ui_render_play_button('Preparing Player...', false); |
| 53 | + is_premium = await SPOTIFY_API.set_playback_shuffle(device_id, false); |
| 54 | + } catch (error) { |
| 55 | + log('ERROR', 'Failed to set playback shuffle to disabled.'); |
| 56 | + alert('Failed to disable playback shuffle in Spotify player.'); |
| 57 | + return console.log(error); |
| 58 | + } |
56 | 59 | } |
57 | 60 |
|
| 61 | + // Cache the user's premium status for later use |
| 62 | + SPOTIFY_USER_IS_PREMIUM = is_premium; |
| 63 | + |
58 | 64 | // Spotify Premium User: Start playback with our shuffled results song uris |
59 | 65 | if (is_premium) { |
60 | 66 | try { |
@@ -164,6 +170,9 @@ function bind_playables_listeners() { |
164 | 170 | alert('Failed to play tracks from the selected track in selected device Spotify player.'); |
165 | 171 | return console.log(error); |
166 | 172 | } |
| 173 | + |
| 174 | + // Enable the UI button to allow the user to reshuffle and play music |
| 175 | + ui_render_play_button('Reshuffle & Play', true); |
167 | 176 | }; |
168 | 177 | playable.addEventListener('click', listener); |
169 | 178 |
|
|
0 commit comments