diff --git a/.idea/Daflv4.iml b/.idea/Daflv4.iml index 29145de..690e968 100644 --- a/.idea/Daflv4.iml +++ b/.idea/Daflv4.iml @@ -48,6 +48,7 @@ + diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index 6b14afa..d5f3182 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -5,662 +5,662 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index b6e6985..a82e8b9 100644 --- a/.idea/libraries/Dart_SDK.xml +++ b/.idea/libraries/Dart_SDK.xml @@ -1,25 +1,25 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/Sources/dafl_project_flutter/lib/api/api.dart b/Sources/dafl_project_flutter/lib/api/api.dart index 6b0fba4..6a3871a 100644 --- a/Sources/dafl_project_flutter/lib/api/api.dart +++ b/Sources/dafl_project_flutter/lib/api/api.dart @@ -1,10 +1,10 @@ import 'dart:convert'; import 'dart:math'; +import 'package:dafl_project_flutter/main.dart'; import 'package:http/http.dart' as http; import 'package:crypto/crypto.dart'; import 'dart:developer' as dev; import '../exceptions/api_exception.dart'; -import 'track.dart'; class Api { //from dashboard @@ -23,14 +23,11 @@ class Api { late http.Response _response; //use _setResponse() as kind of a private setter final _playlistName = "Dafl's discovery"; - //final _playlistName = 'daflmusic'; - //from web api String? _code; int? _expiresIn; String? _refreshToken; String? _accessToken; //use _getAccessToken() as kind of a private getter - String? _idUser; //use _getIdUser() as kind of a private getter //other final _client = http.Client(); @@ -105,16 +102,11 @@ class Api { _refreshToken = decodedResponse['refresh_token']; } - //handle 401 status code (bad or expired token) Future _getAccessToken() async { - await _tokenValidity(); - return _accessToken; - } - - _tokenValidity() async { if (DateTime.now().isAfter(_tokenEnd!)) { await _getRefreshedAccessToken(); } + return _accessToken; } _setResponse(value) { @@ -168,7 +160,7 @@ class Api { return decodedResponse['items'][0]['track']['id']; } - Future getTrackInfo(String id) async { + Future getTrackInfo(String id) async { var url = Uri.https('api.spotify.com', 'v1/tracks/$id'); var token = await _getAccessToken(); _setResponse(await _client.get(url, headers: { @@ -176,16 +168,19 @@ class Api { 'Content-Type': 'application/json' })); var decodedResponse = jsonDecode(utf8.decode(_response.bodyBytes)) as Map; - return Track(decodedResponse['artists'][0]['name'], decodedResponse['name'], - decodedResponse['album']['images'][0]['url']); + Map info = { + 'artist': decodedResponse['artists'][0]['name'], + 'name': decodedResponse['name'], + 'cover': decodedResponse['album']['images'][0]['url'] + }; + return info; } addToPLaylist(String id) async { - var res = await _getPlaylist(); + dynamic res = await _getPlaylist(); if (!res) { - await _createPlaylist(); + res = await _createPlaylist(); } - //TODO : add to playlist } dynamic _getPlaylist() async { @@ -207,9 +202,9 @@ class Api { } _createPlaylist() async { + var idUser = await MyApp.controller.currentUser.getIdSpotify(); var token = await _getAccessToken(); - var id = await _getIdUser(); - var url = Uri.https('api.spotify.com', 'v1/users/$id/playlists'); + var url = Uri.https('api.spotify.com', 'v1/users/$idUser/playlists'); _setResponse(await _client.post(url, headers: { 'Accept': 'application/json', @@ -223,17 +218,10 @@ class Api { 'public': 'true' }))); var decodedResponse = jsonDecode(utf8.decode(_response.bodyBytes)) as Map; - _idUser = decodedResponse['id']; - } - - Future _getIdUser() async { - if (_idUser == null) { - await _getIdUserApi(); - } - return _idUser!; + return decodedResponse['id']; } - _getIdUserApi() async { + Future getIdUser() async { var url = Uri.https('api.spotify.com', 'v1/me'); var token = await _getAccessToken(); _setResponse(await _client.get(url, headers: { @@ -241,6 +229,6 @@ class Api { 'Content-Type': 'application/json' })); var decodedResponse = jsonDecode(utf8.decode(_response.bodyBytes)) as Map; - _idUser = decodedResponse['id']; + return decodedResponse['id']; } } diff --git a/Sources/dafl_project_flutter/lib/api/track.dart b/Sources/dafl_project_flutter/lib/api/track.dart deleted file mode 100644 index f4658d6..0000000 --- a/Sources/dafl_project_flutter/lib/api/track.dart +++ /dev/null @@ -1,11 +0,0 @@ -class Track { - final String _artist; - final String _name; - final String _albumImage; - - Track(this._artist, this._name, this._albumImage); - - String get artist => _artist; - String get name => _name; - String get albumImage => _albumImage; -} diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index 198299f..1f78ea5 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -4,48 +4,34 @@ import '../persistence/database_loader.dart'; import '../persistence/database_saver.dart'; import '../persistence/database_searcher.dart'; import '../persistence/loader.dart'; - import '../persistence/saver.dart'; import '../model/user.dart'; import '../persistence/searcher.dart'; class Controller { - static Controller? _this; - static Saver saver = DatabaseSaver(); static Loader loader = DatabaseLoader(); static final Searcher _searcher = DatabaseSearcher(); - User currentUser = User("", ""); - - factory Controller() { - _this ??= Controller._(); - return _this!; - } - - Controller._(); + late User currentUser; void save(User userToSave) { saver.save(userToSave); } Future load(String username, String password) async { - changeCurrentUser(await loader.load(username, password)); - } - - User createUser(String username, String password) { - return User(username, password); + _changeCurrentUser(await loader.load(username, password)); } - void changeCurrentUser(User user) { + void _changeCurrentUser(User user) { currentUser = user; } - void changeUsernameCourant(String newName) { + void changeCurrentUsername(String newName) { currentUser.usernameDafl = newName; } - void changePasswordCourant(String newPass) { + void changeCurrentPassword(String newPass) { currentUser.passwDafl = newPass; } @@ -53,7 +39,7 @@ class Controller { return await _searcher.searchByUsername(username); } - void chargeExample() { + /*void chargeExample() { currentUser.spots = [ Spot( User('Félix', '1234'), @@ -84,5 +70,5 @@ class Controller { Music('Paradis', 'Sopico', 'https://cdns-images.dzcdn.net/images/cover/17a9747927ac3e5ea56f92f635d9180c/500x500.jpg')), ].reversed.toList(); - } + }*/ } diff --git a/Sources/dafl_project_flutter/lib/main.dart b/Sources/dafl_project_flutter/lib/main.dart index 1581d10..9184150 100644 --- a/Sources/dafl_project_flutter/lib/main.dart +++ b/Sources/dafl_project_flutter/lib/main.dart @@ -33,7 +33,7 @@ class MyApp extends StatelessWidget { create: (context) => CardProvider(), child: const MaterialApp( debugShowCheckedModeBanner: false, - title: 'Flutter Demo', + title: 'Dafl Music', home: HomePage(), )); } diff --git a/Sources/dafl_project_flutter/lib/model/music.dart b/Sources/dafl_project_flutter/lib/model/music.dart index 74bb1ab..aa49dc1 100644 --- a/Sources/dafl_project_flutter/lib/model/music.dart +++ b/Sources/dafl_project_flutter/lib/model/music.dart @@ -1,19 +1,29 @@ -class Music{ - String name; - String artist; - String linkCover; +import '../exceptions/api_exception.dart'; +import '../main.dart'; - Music(this.name, this.artist, this.linkCover); +class Music { + late String _name; + late String _artist; + late String _linkCover; + final String _id; + Music(this._id) { + _completeInfo(); + } - @override - bool operator ==(Object other) => - identical(this, other) || - other is Music && - runtimeType == other.runtimeType && - name == other.name && - artist == other.artist; + String get name => _name; + String get artist => _artist; + String get linkCover => _linkCover; + String get id => _id; - @override - int get hashCode => name.hashCode ^ artist.hashCode; -} \ No newline at end of file + _completeInfo() async { + try { + var info = await MyApp.api.getTrackInfo(_id); + _name = info['name']; + _artist = info['artist']; + _linkCover = info['cover']; + } on ApiException { + // TODO : add notification to show that an error occured + } + } +} diff --git a/Sources/dafl_project_flutter/lib/model/user.dart b/Sources/dafl_project_flutter/lib/model/user.dart index d1f0697..5e4d95b 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -1,4 +1,3 @@ -import '../api/track.dart'; import '../exceptions/api_exception.dart'; import '../main.dart'; import 'conversation.dart'; @@ -12,30 +11,29 @@ class User { late String passwDafl; //attributes with Spotify API - late String _id; - late Track track; + String? _idSpotify; //use _getIdUser() as kind of a private getter + late Music _currentMusic; - //constructors - User(this.usernameDafl, this.passwDafl) { - _actualiseTrack(); - } - - User.name(this.usernameDafl); - - User.fromDatabase(this.idDafl, this.usernameDafl); - - //lists Set likedUsers = {}; List discovery = []; List waitingConv = []; List confirmConv = []; - Music currentSong = Music('Couleurs', 'Khali', - 'https://khaligidilit.com/assets/images/cover-LAI%CC%88LA-Khali.jpeg'); - List spots = []; Map conversations = {}; + //constructors + User(this.usernameDafl, this.passwDafl) { + _actualiseCurrentMusic(); + } + + Music get currentMusic => _currentMusic; //lists + + Future getIdSpotify() async { + _idSpotify ??= await MyApp.api.getIdUser(); + return _idSpotify!; + } + void addDiscovery(Music newmusic) { MyApp.controller.currentUser.discovery.add(newmusic); } @@ -59,10 +57,9 @@ class User { conversations.forEach((k, v) => v.displayMessages()); } - _actualiseTrack() async { + _actualiseCurrentMusic() async { try { - _id = await MyApp.api.getCurrentlyPlayingTrack(); - track = await MyApp.api.getTrackInfo(_id); + _currentMusic = Music(await MyApp.api.getCurrentlyPlayingTrack()); } on ApiException { // TODO : add notification to show that an error occured } diff --git a/Sources/dafl_project_flutter/lib/views/pages/main/w_profile.dart b/Sources/dafl_project_flutter/lib/views/pages/main/w_profile.dart index 455e916..7e82474 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/main/w_profile.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/main/w_profile.dart @@ -232,8 +232,8 @@ class _MainPageProfilState extends State { width: 90, placeholder: "assets/images/loadingPlaceholder.gif", - image: MyApp.controller.currentUser.track - .albumImage))), + image: MyApp.controller.currentUser + .currentMusic.linkCover))), Container( margin: const EdgeInsets.fromLTRB(12, 20, 0, 0), child: Column( @@ -241,14 +241,15 @@ class _MainPageProfilState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - MyApp.controller.currentUser.track.name, + MyApp.controller.currentUser.currentMusic.name, style: const TextStyle( fontSize: 18, fontWeight: FontWeight.w500, color: Colors.white), ), Text( - MyApp.controller.currentUser.track.artist, + MyApp + .controller.currentUser.currentMusic.artist, style: const TextStyle( fontSize: 16, fontWeight: FontWeight.w400, diff --git a/Sources/dafl_project_flutter/lib/views/pages/main/w_settings.dart b/Sources/dafl_project_flutter/lib/views/pages/main/w_settings.dart index 7c3cbc8..9e67830 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/main/w_settings.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/main/w_settings.dart @@ -84,7 +84,7 @@ class _SettingsWidgetState extends State { GestureDetector( onTap: () { MyApp.controller - .changeUsernameCourant(userNameTextField.text); + .changeCurrentUsername(userNameTextField.text); notify(0, context, isError: false); }, child: const Padding( @@ -144,7 +144,7 @@ class _SettingsWidgetState extends State { GestureDetector( onTap: () { MyApp.controller - .changePasswordCourant(passwordTextField.text); + .changeCurrentPassword(passwordTextField.text); notify(1, context, isError: false); }, child: const Padding( diff --git a/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart b/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart index 0ebe26d..83537fb 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/sign_in/p_sign_in.dart @@ -260,7 +260,7 @@ class _SignInPageState extends State { .load(userNameTextField.text, passwordTextField.text); if (MyApp.controller.currentUser.usernameDafl != "") { - MyApp.controller.chargeExample(); + //MyApp.controller.chargeExample(); Navigator.of(context).push( PageTransition( type: PageTransitionType.fade, diff --git a/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart b/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart index 9cff4f5..c81d824 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/sign_up/p_sign_up.dart @@ -329,7 +329,7 @@ class _SignUpPageState extends State { ])); } - Future checkInformations( + checkInformations( String username, String password, String confirmPassword) async { if (username == "") { notify(2, context);