WIP: update trip cards and Spotify handling
This commit is contained in:
@@ -45,6 +45,7 @@ export default function NewTripScreen({ navigation }) {
|
||||
setDuration(finalDuration);
|
||||
|
||||
let generatedPlaylistUrl = null;
|
||||
let spotifyPremiumRequired = false;
|
||||
|
||||
try {
|
||||
console.log("GENERATING_PLAYLIST_FOR_TRIP:", tripName);
|
||||
@@ -95,14 +96,21 @@ export default function NewTripScreen({ navigation }) {
|
||||
}
|
||||
} else if (!testRes.ok) {
|
||||
const testErr = await testRes.text();
|
||||
console.error("Spotify validation request failed:", testRes.status, testErr);
|
||||
if (testRes.status === 403 || testErr.toLowerCase().includes('active premium subscription required')) {
|
||||
spotifyPremiumRequired = true;
|
||||
console.warn("Spotify validation skipped due to expected Premium limitation:", testRes.status, testErr);
|
||||
} else {
|
||||
console.warn("Spotify validation request failed:", testRes.status, testErr);
|
||||
}
|
||||
providerToken = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!providerToken) {
|
||||
console.log("Spotify token missing or expired, skipping playlist generation.");
|
||||
Alert.alert('Spotify Desligado', 'O token do Spotify expirou ou está em falta. Por favor reconecte o Spotify no Perfil.');
|
||||
if (!spotifyPremiumRequired) {
|
||||
Alert.alert('Spotify Desligado', 'O token do Spotify expirou ou está em falta. Por favor reconecte o Spotify no Perfil.');
|
||||
}
|
||||
} else {
|
||||
// B. Fetch Spotify User ID
|
||||
const spotifyUserRes = await fetch('https://api.spotify.com/v1/me', {
|
||||
@@ -182,8 +190,13 @@ export default function NewTripScreen({ navigation }) {
|
||||
|
||||
if (!searchRes.ok) {
|
||||
const errText = await searchRes.text();
|
||||
console.error("Search failed:", errText);
|
||||
Alert.alert('Erro Spotify', `Aviso ao adicionar músicas: ${errText.substring(0, 100)}`);
|
||||
if (searchRes.status === 403 || errText.toLowerCase().includes('active premium subscription required')) {
|
||||
spotifyPremiumRequired = true;
|
||||
console.warn("Spotify search skipped due to expected Premium limitation:", searchRes.status, errText);
|
||||
} else {
|
||||
console.warn("Search failed:", errText);
|
||||
Alert.alert('Erro Spotify', `Aviso ao adicionar músicas: ${errText.substring(0, 100)}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -212,7 +225,7 @@ export default function NewTripScreen({ navigation }) {
|
||||
const chunkSize = 100;
|
||||
for (let i = 0; i < trackUris.length; i += chunkSize) {
|
||||
const chunk = trackUris.slice(i, i + chunkSize);
|
||||
await fetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks`, {
|
||||
const addTracksRes = await fetch(`https://api.spotify.com/v1/playlists/${playlistId}/tracks`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${providerToken}`,
|
||||
@@ -220,15 +233,30 @@ export default function NewTripScreen({ navigation }) {
|
||||
},
|
||||
body: JSON.stringify({ uris: chunk })
|
||||
});
|
||||
if (!addTracksRes.ok) {
|
||||
const addTracksErr = await addTracksRes.text();
|
||||
if (addTracksRes.status === 403 || addTracksErr.toLowerCase().includes('active premium subscription required')) {
|
||||
spotifyPremiumRequired = true;
|
||||
console.warn("Spotify add tracks skipped due to expected Premium limitation:", addTracksRes.status, addTracksErr);
|
||||
break;
|
||||
}
|
||||
throw new Error(`Spotify API returned status ${addTracksRes.status} while adding tracks: ${addTracksErr.substring(0, 150)}`);
|
||||
}
|
||||
}
|
||||
console.log("PLAYLIST_CREATE_SUCCESS:", generatedPlaylistUrl);
|
||||
} else {
|
||||
console.error("No tracks found for genres:", seed_genres);
|
||||
console.warn("No tracks found for genres:", seed_genres);
|
||||
}
|
||||
}
|
||||
} catch (playlistError: any) {
|
||||
console.warn("Expected failure generating playlist:", playlistError.message || playlistError);
|
||||
Alert.alert('Erro Playlist', `A viagem foi calculada, mas a playlist falhou: ${playlistError.message?.substring(0, 50) || 'Erro Desconhecido'}`);
|
||||
const playlistErrorMessage = String(playlistError?.message || playlistError || '');
|
||||
if (playlistErrorMessage.includes('403') || playlistErrorMessage.toLowerCase().includes('active premium subscription required')) {
|
||||
spotifyPremiumRequired = true;
|
||||
console.warn("Spotify playlist skipped due to expected Premium limitation:", playlistErrorMessage);
|
||||
} else {
|
||||
console.warn("Expected failure generating playlist:", playlistErrorMessage);
|
||||
Alert.alert('Erro Playlist', `A viagem foi calculada, mas a playlist falhou: ${playlistErrorMessage.substring(0, 50) || 'Erro Desconhecido'}`);
|
||||
}
|
||||
}
|
||||
|
||||
// G. Save to Supabase unconditionally if route is valid
|
||||
@@ -250,7 +278,13 @@ export default function NewTripScreen({ navigation }) {
|
||||
console.error("DB Insert error:", dbError);
|
||||
Alert.alert('Erro ao Guardar', 'Não foi possível guardar a viagem na base de dados: ' + dbError.message);
|
||||
} else {
|
||||
Alert.alert('Sucesso!', 'Viagem calculada e guardada!');
|
||||
Alert.alert(
|
||||
'Sucesso!',
|
||||
spotifyPremiumRequired
|
||||
? 'Viagem criada, mas a playlist do Spotify não pôde ser criada porque a app Spotify precisa de Premium.'
|
||||
: 'Viagem calculada e guardada!'
|
||||
);
|
||||
navigation.goBack();
|
||||
}
|
||||
} catch (dbEx) {
|
||||
console.error("Exception during DB save:", dbEx);
|
||||
|
||||
Reference in New Issue
Block a user