Improvement of the model (addition of the class artist and modification of the class music) 🎨 and realization of the class MusicViewModel with test

WORK-DDA
Emre KARTAL 2 years ago
parent e40560aa7d
commit a6e617e59c

@ -0,0 +1,10 @@
class Artist {
String _id;
String _name;
Artist(this._id, this._name);
String get id => _id;
String get name => _name;
}

@ -1,15 +1,19 @@
import 'Artist.dart';
class Music { class Music {
final int _id; final String _id;
String _title; String _title;
String _cover; String _cover;
String _singer; String _previewUrl;
DateTime _date; DateTime _date;
List<Artist> _artists;
// Constructor // Constructor
Music(this._id, this._title, this._cover, this._singer, this._date); Music(this._id, this._title, this._cover, this._previewUrl, this._date,
this._artists);
//Getters and setters //Getters and setters
int get id => _id; String get id => _id;
String get title => _title; String get title => _title;
@ -19,19 +23,25 @@ class Music {
String get cover => _cover; String get cover => _cover;
set cover(String value) {
_cover = value;
}
String get previewUrl => _previewUrl;
set previewUrl(String value) {
_previewUrl = value;
}
DateTime get date => _date; DateTime get date => _date;
set date(DateTime value) { set date(DateTime value) {
_date = value; _date = value;
} }
String get singer => _singer; List<Artist> get artists => _artists;
set singer(String value) {
_singer = value;
}
set cover(String value) { set artists(List<Artist> value) {
_cover = value; _artists = value;
} }
} }

@ -2,7 +2,7 @@ class Post {
final int _id; final int _id;
final int _idUser; final int _idUser;
String? _description; String? _description;
int _idMusic; String _idMusic;
String _location; String _location;
int _nblikes; int _nblikes;
String? _selfie; String? _selfie;
@ -23,9 +23,9 @@ class Post {
_description = value; _description = value;
} }
int get idMusic => _idMusic; String get idMusic => _idMusic;
set idMusic(int value) { set idMusic(String value) {
_idMusic = value; _idMusic = value;
} }

@ -1,10 +1,8 @@
import '../model/Comment.dart'; import '../model/Comment.dart';
class CommentViewModel { class CommentViewModel {
// Methods // Methods
List<Comment> getCommentsPost(int idPost) { List<Comment> getCommentsPost(int idPost) {
throw new Error(); throw new Error();
} }
} }

@ -1,19 +1,96 @@
import 'dart:convert';
import 'package:justmusic/view_model/TokenSpotify.dart';
import 'package:http/http.dart' as http;
import '../model/Artist.dart';
import '../model/Music.dart'; import '../model/Music.dart';
class MusicViewModel { class MusicViewModel {
final String API_URL = "https://api.spotify.com/v1";
late TokenSpotify _token;
final String API_URL = ""; MusicViewModel() {
_token = new TokenSpotify();
}
// Methods // Methods
Music getMusic(int id) { Future<Music> getMusic(String id) async {
throw new Error(); var accessToken = await _token.getAccessToken();
var response = await http.get(Uri.parse('$API_URL/tracks/$id'), headers: {
'Authorization': 'Bearer $accessToken',
});
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
List<Artist> artists =
List<Artist>.from(responseData['artists'].map((artist) {
return Artist(artist['id'], artist['name']);
}));
return Music(
responseData['id'],
responseData['name'],
responseData['album']['images'][0]['url'],
responseData['preview_url'],
DateTime.parse(responseData['album']['release_date']),
artists);
} else {
throw Exception(
'Error retrieving music information : ${response.statusCode} ${response.reasonPhrase}');
}
} }
Music getMusicWithName(String name) { List<Music> _getMusicsFromResponse(Map<String, dynamic> responseData) {
throw new Error(); List<Music> musics = [];
List<dynamic> tracks = responseData['tracks']['items'];
for (var track in tracks) {
List<Artist> artists = List<Artist>.from(track['artists'].map((artist) {
return Artist(artist['id'], artist['name']);
}));
musics.add(Music(
track['id'],
track['name'],
track['album']['images'][0]['url'],
track['preview_url'],
DateTime.now(),
artists));
}
return musics;
} }
List<Music> getMusics(String name) { Future<List<Music>> getMusicsWithName(String name, {int limit = 20, int offset = 1}) async {
throw new Error(); var accessToken = await _token.getAccessToken();
var response = await http
.get(Uri.parse('$API_URL/search?q=track%3A$name&type=track&limit=$limit&offset=$offset'), headers: {
'Authorization': 'Bearer $accessToken',
});
if (response.statusCode == 200) {
Map<String, dynamic> responseData = jsonDecode(response.body);
return _getMusicsFromResponse(responseData);
} else {
throw Exception(
'Error while retrieving music : ${response.statusCode} ${response.reasonPhrase}');
}
}
Future<List<Music>> getMusicsWithArtistName(String name, {int limit = 20, int offset = 1}) async {
var accessToken = await _token.getAccessToken();
var response = await http.get(
Uri.parse('$API_URL/search?q=artist%3A$name&type=track&limit=$limit&offset=$offset'),
headers: {
'Authorization': 'Bearer $accessToken',
});
if (response.statusCode == 200) {
Map<String, dynamic> responseData = jsonDecode(response.body);
return _getMusicsFromResponse(responseData);
} else {
throw Exception(
'Error while retrieving music : ${response.statusCode} ${response.reasonPhrase}');
}
} }
} }

@ -32,9 +32,10 @@ class TokenSpotify {
if (response.statusCode == 200) { if (response.statusCode == 200) {
final responseData = jsonDecode(response.body); final responseData = jsonDecode(response.body);
_accessToken = responseData['access_token']; _accessToken = responseData['access_token'];
_tokenEnd = DateTime.now().add(Duration(seconds: responseData['expires_in'])); _tokenEnd =
DateTime.now().add(Duration(seconds: responseData['expires_in']));
} else { } else {
print('Erreur lors de l\'actualisation du token : ${response.statusCode}'); print('Error refreshing token : ${response.statusCode}');
} }
} }

@ -0,0 +1,35 @@
import 'package:justmusic/model/Artist.dart';
import 'package:justmusic/model/Music.dart';
import 'package:justmusic/view_model/MusicViewModel.dart';
Future<void> main() async {
MusicViewModel musicVM = new MusicViewModel();
Music m = await musicVM.getMusic('295SxdR1DqunCNwd0U767w');
print("id :" + m.id.toString() + " cover :" + m.cover + " title :" + m.title);
print(m.date.toString() + " " + m.previewUrl);
for (Artist a in m.artists) {
print(a.id + ":" + a.name);
}
print('\nMusics :');
List<Music> musics = await musicVM.getMusicsWithName('Onizuka');
for (Music m in musics) {
print("id :" + m.id.toString() + " cover :" + m.cover + " title :" + m.title);
print(m.date.toString() + " " + m.previewUrl);
for (Artist a in m.artists) {
print(a.id + ":" + a.name);
}
}
print('\nMusics With Artist:');
List<Music> musics2 = await musicVM.getMusicsWithArtistName('PNL');
for (Music m in musics2) {
print("id :" + m.id.toString() + " cover :" + m.cover + " title :" + m.title);
print(m.date.toString() + " " + m.previewUrl);
for (Artist a in m.artists) {
print(a.id + ":" + a.name);
}
}
}
Loading…
Cancel
Save