|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:math';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
import 'package:dafl_project_flutter/main.dart';
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
@ -176,14 +178,41 @@ class Api {
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addToPLaylist(String id) async {
|
|
|
|
|
dynamic res = await _getPlaylist();
|
|
|
|
|
if (!res) {
|
|
|
|
|
res = await _createPlaylist();
|
|
|
|
|
Future<bool> _isInPlaylist(String idTrack, String idPlaylist) async {
|
|
|
|
|
var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks',
|
|
|
|
|
{'limit': '50', 'fields': 'items(track(id))'});
|
|
|
|
|
var token = await _getAccessToken();
|
|
|
|
|
_setResponse(await _client.get(url, headers: <String, String>{
|
|
|
|
|
'Authorization': '$_tokenType $token',
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
}));
|
|
|
|
|
var decodedResponse = jsonDecode(utf8.decode(_response.bodyBytes)) as Map;
|
|
|
|
|
var res = decodedResponse['items']
|
|
|
|
|
.where((element) => element['track']['id'] == idTrack)
|
|
|
|
|
.toList();
|
|
|
|
|
if (res.length >= 1) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addToPLaylist(String idTrack) async {
|
|
|
|
|
var idPlaylist = await _getPlaylist();
|
|
|
|
|
if (idPlaylist == null) {
|
|
|
|
|
idPlaylist = await _createPlaylist();
|
|
|
|
|
} else {
|
|
|
|
|
if (await _isInPlaylist(idTrack, idPlaylist)) return;
|
|
|
|
|
}
|
|
|
|
|
var token = await _getAccessToken();
|
|
|
|
|
var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks',
|
|
|
|
|
{'uris': 'spotify:track:$idTrack'});
|
|
|
|
|
_setResponse(await _client.post(url, headers: <String, String>{
|
|
|
|
|
'Authorization': '$_tokenType $token',
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dynamic _getPlaylist() async {
|
|
|
|
|
Future<String?> _getPlaylist() async {
|
|
|
|
|
var url = Uri.https('api.spotify.com', 'v1/me/playlists', {'limit': '50'});
|
|
|
|
|
var token = await _getAccessToken();
|
|
|
|
|
_setResponse(await _client.get(url, headers: <String, String>{
|
|
|
|
@ -198,7 +227,7 @@ class Api {
|
|
|
|
|
return daflplaylist[0]['uri'].substring(
|
|
|
|
|
17); //17 char because format is 'spotify:playlist:MYPLAYLISTID'
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_createPlaylist() async {
|
|
|
|
@ -218,7 +247,44 @@ class Api {
|
|
|
|
|
'public': 'true'
|
|
|
|
|
})));
|
|
|
|
|
var decodedResponse = jsonDecode(utf8.decode(_response.bodyBytes)) as Map;
|
|
|
|
|
return decodedResponse['id'];
|
|
|
|
|
var idPlaylist = decodedResponse['id'];
|
|
|
|
|
url = Uri.https('api.spotify.com', 'v1/playlists/playlist_id/images');
|
|
|
|
|
String imagePath = 'assets/images/icon_App.png';
|
|
|
|
|
File imagefile = File(imagePath);
|
|
|
|
|
Uint8List imagebytes = await imagefile.readAsBytes();
|
|
|
|
|
String base64string = base64.encode(imagebytes);
|
|
|
|
|
_setResponse(await _client.put(url,
|
|
|
|
|
headers: <String, String>{
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'Authorization': '$_tokenType $token',
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: base64string));
|
|
|
|
|
decodedResponse = jsonDecode(utf8.decode(_response.bodyBytes)) as Map;
|
|
|
|
|
return idPlaylist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeFromPlaylist(String idTrack) async {
|
|
|
|
|
var idPlaylist = await _getPlaylist();
|
|
|
|
|
if (idPlaylist == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (await _isInPlaylist(idTrack, idPlaylist)) {
|
|
|
|
|
var token = await _getAccessToken();
|
|
|
|
|
var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks');
|
|
|
|
|
var jsonVar = jsonEncode(<String, List>{
|
|
|
|
|
"tracks": [
|
|
|
|
|
{'uri': 'spotify:track:$idTrack'}
|
|
|
|
|
]
|
|
|
|
|
});
|
|
|
|
|
jsonEncode(<String, String>{'uri': 'spotify:track:$idTrack'});
|
|
|
|
|
_setResponse(await _client.delete(url,
|
|
|
|
|
headers: <String, String>{
|
|
|
|
|
'Authorization': '$_tokenType $token',
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: jsonVar));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> getIdUser() async {
|
|
|
|
|