diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index 8405804..a7ea9dd 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -12,6 +12,8 @@ class Controller { late User _currentUser; Area _area = Area(); + late BuildContext navigatorKey; + Uri getApiUrlAuthorize() { return _api.identification.urlAuthorize; } @@ -49,6 +51,29 @@ class Controller { return _area.spots; } + Future> getArea() async { + await _area.sendCurrentLocation(); + await _area.getData(); + return _area.spots; + } + + playTrack(String id) { + _api.requests.playTrack(id); + } + + Future> getDiscoveries() async { + _currentUser.discoveries = await _api.requests.getPlaylistTracks(); + return _currentUser.discoveries; + } + + removeFromPlaylist(String id) { + _api.requests.removeFromPlaylist(id); + } + + addToPlaylist(String id) { + _api.requests.addToPlaylist(id); + } + /* static Saver saver = DatabaseSaver(); static Loader loader = DatabaseLoader(); diff --git a/Sources/dafl_project_flutter/lib/main.dart b/Sources/dafl_project_flutter/lib/main.dart index 61fc00b..b78e806 100644 --- a/Sources/dafl_project_flutter/lib/main.dart +++ b/Sources/dafl_project_flutter/lib/main.dart @@ -157,7 +157,7 @@ class CardProvider extends ChangeNotifier { notifyListeners(); } - void discovery(BuildContext context) { + void discovery(BuildContext context) async { dev.log("discovery"); _angle = 0; _position -= Offset(0, -_screenSize.height); @@ -178,7 +178,7 @@ class CardProvider extends ChangeNotifier { child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - MyApp.controller.currentUser.discovery.contains( + await MyApp.controller.getDiscoveries().containsKey( MyApp.controller.currentUser.spots.last.music) ? const Icon( Icons.info_rounded, @@ -193,7 +193,7 @@ class CardProvider extends ChangeNotifier { const SizedBox( width: 10, ), - MyApp.controller.currentUser.discovery.contains( + MyApp.controller.getDiscoveries().contains( MyApp.controller.getSpots().last.music) ? const Text( "Déjà dans vos discovery", @@ -217,11 +217,10 @@ class CardProvider extends ChangeNotifier { curve: Curves.linear, reverseCurve: Curves.linear, ); - if (!MyApp.controller.currentUser.discovery + if (!MyApp.controller + .getDiscoveries() .contains(MyApp.controller.getSpots().last.music)) { - MyApp.controller.getSpots().last.music.defineDate(); - MyApp.controller.currentUser - .addDiscovery(MyApp.controller.getSpots().last.music); + MyApp.controller.addToPlaylist(MyApp.controller.getSpots().last.music.id); notifyListeners(); } } diff --git a/Sources/dafl_project_flutter/lib/model/user.dart b/Sources/dafl_project_flutter/lib/model/user.dart index d5a1dbe..967b8dc 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -11,12 +11,9 @@ class User { late int idDafl; late String usernameDafl; late String passwDafl; - List discovery = []; - List spots = []; + Map discoveries = {}; - //attributes with Spotify API final String _idSpotify; - late String currentMusic; bool sortChoise = true; @@ -24,29 +21,4 @@ class User { User(this.usernameDafl, this._idSpotify); String get idSpotify => _idSpotify; - - addDiscovery(Music music) { - discovery.add(music); - } - - listSpots() { - int verif = 0; - Future> rep = Location.sendCurrentLocation(); - //ex : dorian : 2d2s52a15d2a5 , audric : 2x5s2az3d1s5wx5s1 , lucas : s2a5d25a2a25d - rep.then((Map result) { - if (result.isNotEmpty) { - result.forEach((key, value) { - for (var element in spots) { - if (element.userId == key) { - verif = 1; - } - } - if (verif == 0) { - spots.add(Spot(key, Music(value))); - } - verif = 0; - }); - } - }); - } } diff --git a/Sources/dafl_project_flutter/lib/services/api/api_spotify_requests.dart b/Sources/dafl_project_flutter/lib/services/api/api_spotify_requests.dart index d85cbad..ffef63b 100644 --- a/Sources/dafl_project_flutter/lib/services/api/api_spotify_requests.dart +++ b/Sources/dafl_project_flutter/lib/services/api/api_spotify_requests.dart @@ -54,43 +54,31 @@ class ApiSpotifyRequests extends HttpResponseVerification { return infos; } - Future _isInPlaylist(String idTrack, String idPlaylist) async { - var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks', - {'limit': '50', 'fields': 'items(track(id))'}); + Future getIdUser() async { + var url = Uri.https('api.spotify.com', 'v1/me'); var token = await _token.getAccessToken(); setResponse(await http.get(url, headers: { '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; + return decodedResponse['id']; } - addToPLaylist(String idTrack) async { - var idPlaylist = await _getPlaylist(); - if (idPlaylist == null) { - idPlaylist = await _createPlaylist(); - } else { - if (await _isInPlaylist(idTrack, idPlaylist)) { - return; - } - } + playTrack(String idTrack) async { var token = await _token.getAccessToken(); - var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks', - {'uris': 'spotify:track:$idTrack'}); - setResponse(await http.post(url, headers: { - 'Authorization': '$_tokenType $token', - 'Content-Type': 'application/json' - })); + var url = Uri.https('api.spotify.com', 'v1/me/player/play'); + setResponse(await http.put(url, + headers: { + 'Authorization': '$_tokenType $token', + 'Content-Type': 'application/json' + }, + body: jsonEncode({ + 'uris': ['spotify:track:$idTrack'] + }))); } - Future _getPlaylist() async { + Future _getPlaylistId() async { var url = Uri.https('api.spotify.com', 'v1/me/playlists', {'limit': '50'}); var token = await _token.getAccessToken(); setResponse(await http.get(url, headers: { @@ -105,7 +93,7 @@ class ApiSpotifyRequests extends HttpResponseVerification { return daflplaylist[0]['uri'].substring( 17); //17 char because format is 'spotify:playlist:MYPLAYLISTID' } - return null; + return await _createPlaylist(); } Future _createPlaylist() async { @@ -129,21 +117,37 @@ class ApiSpotifyRequests extends HttpResponseVerification { return idPlaylist; } - playTrack(String idTrack) async { + addToPlaylist(String idTrack) async { + var idPlaylist = await _getPlaylistId(); + if (await _isInPlaylist(idTrack, idPlaylist)) { + return; + } var token = await _token.getAccessToken(); - var url = Uri.https('api.spotify.com', 'v1/me/player/play'); - setResponse(await http.put(url, - headers: { - 'Authorization': '$_tokenType $token', - 'Content-Type': 'application/json' - }, - body: jsonEncode({ - 'uris': ['spotify:track:$idTrack'] - }))); + var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks', + {'uris': 'spotify:track:$idTrack'}); + setResponse(await http.post(url, headers: { + 'Authorization': '$_tokenType $token', + 'Content-Type': 'application/json' + })); + } + + Future _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 _token.getAccessToken(); + setResponse(await http.get(url, headers: { + '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(); + return (res.length >= 1) ? true : false; } removeFromPlaylist(String idTrack) async { - var idPlaylist = await _getPlaylist(); + var idPlaylist = await _getPlaylistId(); if (idPlaylist != null) { if (await _isInPlaylist(idTrack, idPlaylist)) { var token = await _token.getAccessToken(); @@ -164,14 +168,19 @@ class ApiSpotifyRequests extends HttpResponseVerification { } } - Future getIdUser() async { - var url = Uri.https('api.spotify.com', 'v1/me'); + Future> getPlaylistTracks() async { + var idPlaylist = _getPlaylistId(); + var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks', + {'fields': 'items(track(id),added_at)'}); var token = await _token.getAccessToken(); setResponse(await http.get(url, headers: { 'Authorization': '$_tokenType $token', 'Content-Type': 'application/json' })); var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes)) as Map; - return decodedResponse['id']; + Map mapRes = {}; + decodedResponse['items'].toList().forEach((elem) => + {mapRes[elem['track']['id']] = DateTime.parse(elem['added_at'])}); + return mapRes; } } diff --git a/Sources/dafl_project_flutter/lib/views/pages/main/p_main.dart b/Sources/dafl_project_flutter/lib/views/pages/main/p_main.dart index 93cc366..0f7d518 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/main/p_main.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/main/p_main.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'package:dafl_project_flutter/main.dart'; import 'package:flutter/material.dart'; -import '../../../presentation/custom_icons_icons.dart'; +import '../../presentation/custom_icons_icons.dart'; import './w_settings.dart'; import './w_spot.dart'; import './w_discovery.dart'; @@ -9,8 +9,6 @@ import './w_profile.dart'; import './w_messages.dart'; import 'w_top.dart'; - - class MainPage extends StatefulWidget { const MainPage({Key? key}) : super(key: key); @@ -110,10 +108,12 @@ class _MainPageState extends State { ), ); } + @override void initState() { super.initState(); - Timer timer = Timer.periodic(const Duration(seconds: 10), (Timer t) => MyApp.controller.currentUser.listSpots()); + Timer timer = Timer.periodic( + const Duration(seconds: 10), (Timer t) => MyApp.controller.getSpots()); } } diff --git a/Sources/dafl_project_flutter/lib/views/pages/main/w_discovery.dart b/Sources/dafl_project_flutter/lib/views/pages/main/w_discovery.dart index 3220532..eb10007 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/main/w_discovery.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/main/w_discovery.dart @@ -30,7 +30,7 @@ class _DiscoveryWidgetState extends State { Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Text( + const Text( 'Playlist découverte', style: TextStyle( color: Colors.white, @@ -118,13 +118,13 @@ class _DiscoveryListState extends State { @override Widget build(BuildContext context) { - List listDiscovery = MyApp.controller.currentUser.discovery; + var listDiscoveries = MyApp.controller.getDiscoveries(); if (MyApp.controller.currentUser.sortChoise) { - listDiscovery.sort((a, b) { + listDiscoveries.sort((a, b) { return a.date.compareTo(b.date); }); } else { - listDiscovery.sort((a, b) { + listDiscoveries.sort((a, b) { return a.name.compareTo(b.name); }); } @@ -135,25 +135,27 @@ class _DiscoveryListState extends State { }, key: refreshKey, child: ListView.builder( - itemCount: listDiscovery.length, + itemCount: listDiscoveries.length, itemBuilder: (context, index) { - int itemCount = listDiscovery.length; + int itemCount = listDiscoveries.length; int reversedIndex = itemCount - 1 - index; return Dismissible( - movementDuration: Duration(milliseconds: 400), - key: Key(listDiscovery[index].name), + movementDuration: const Duration(milliseconds: 400), + key: Key(listDiscoveries[index].name), confirmDismiss: (direction) async { if (direction == DismissDirection.endToStart) { - print(listDiscovery[reversedIndex].id); - print(listDiscovery[reversedIndex].name); - MyApp.controller.currentUser.discovery - .remove(listDiscovery[reversedIndex]); + print(listDiscoveries[reversedIndex].id); + print(listDiscoveries[reversedIndex].name); + MyApp.controller.removeFromPlaylist( + listDiscoveries[reversedIndex].id); + listDiscoveries = MyApp.controller.getDiscoveries(); return true; } if (direction == DismissDirection.startToEnd) { - print(listDiscovery[reversedIndex].name); + print(listDiscoveries[reversedIndex].name); print('play'); - MyApp.api.playTrack(listDiscovery[reversedIndex].id); + MyApp.controller + .playTrack(listDiscoveries[reversedIndex].id); setState(() {}); } return false; @@ -191,8 +193,9 @@ class _DiscoveryListState extends State { child: FadeInImage.assetNetwork( placeholder: "assets/images/loadingPlaceholder.gif", - image: MyApp.controller.currentUser - .discovery[reversedIndex].linkCover), + image: MyApp.controller + .getDiscoveries()[reversedIndex] + .linkCover), ), Container( margin: @@ -204,8 +207,9 @@ class _DiscoveryListState extends State { MainAxisAlignment.center, children: [ Text( - MyApp.controller.currentUser - .discovery[reversedIndex].name, + MyApp.controller + .getDiscoveries()[reversedIndex] + .name, style: TextStyle( fontFamily: 'DMSans', color: @@ -214,10 +218,8 @@ class _DiscoveryListState extends State { fontWeight: FontWeight.w800), ), Text( - MyApp - .controller - .currentUser - .discovery[reversedIndex] + MyApp.controller + .getDiscoveries()[reversedIndex] .artist, style: TextStyle( fontFamily: 'DMSans', 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 71bb08e..5e78ab7 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 @@ -27,15 +27,15 @@ class MainPageProfil extends StatefulWidget { } class _MainPageProfilState extends State { - String? username = MyApp.controller.currentUser.usernameDafl; + late String username; late Music currentmusic; @override - initState() { + initState() async { super.initState(); - username = MyApp.controller.currentUser.usernameDafl; - MyApp.controller.currentUser.actualiseCurrentMusic(); - currentmusic = MyApp.controller.currentUser.currentMusic; + username = MyApp.controller.getIdDafl().toString(); + currentmusic = await MyApp.controller + .getCompleteMusic(MyApp.controller.getCurrentMusic()); } @override @@ -77,14 +77,14 @@ class _MainPageProfilState extends State { ], ), child: Center( - child: Text(username![0], + child: Text(username[0], style: const TextStyle( color: Colors.white, fontSize: 60, fontWeight: FontWeight.w500), textAlign: TextAlign.center))), Text( - username!, + username, style: const TextStyle( color: Colors.white, fontSize: 17, @@ -303,7 +303,7 @@ class _MainPageProfilState extends State { builder: (context) => const SettingsWidget())) .then((value) => setState(() { username = - MyApp.controller.currentUser.usernameDafl; + MyApp.controller.getIdDafl().toString(); })); }, child: Row( 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 9e67830..5078c6b 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 @@ -11,9 +11,9 @@ class SettingsWidget extends StatefulWidget { class _SettingsWidgetState extends State { final userNameTextField = - TextEditingController(text: MyApp.controller.currentUser.usernameDafl); + TextEditingController(text: MyApp.controller.getIdDafl().toString()); final passwordTextField = - TextEditingController(text: MyApp.controller.currentUser.passwDafl); + TextEditingController(text: MyApp.controller.getIdDafl().toString()); @override Widget build(BuildContext context) { @@ -83,8 +83,8 @@ class _SettingsWidgetState extends State { const Spacer(), GestureDetector( onTap: () { - MyApp.controller - .changeCurrentUsername(userNameTextField.text); + /*MyApp.controller + .changeCurrentUsername(userNameTextField.text);*/ notify(0, context, isError: false); }, child: const Padding( @@ -143,8 +143,8 @@ class _SettingsWidgetState extends State { const Spacer(), GestureDetector( onTap: () { - MyApp.controller - .changeCurrentPassword(passwordTextField.text); + /*MyApp.controller + .changeCurrentPassword(passwordTextField.text);*/ notify(1, context, isError: false); }, child: const Padding( diff --git a/Sources/dafl_project_flutter/lib/views/pages/main/w_spot.dart b/Sources/dafl_project_flutter/lib/views/pages/main/w_spot.dart index 3f908e3..5508ada 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/main/w_spot.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/main/w_spot.dart @@ -31,11 +31,9 @@ class _SpotsWidgetState extends State { child: Container( decoration: BoxDecoration( image: DecorationImage( - image: NetworkImage( - MyApp.controller.currentUser.spots.isEmpty - ? "https://i.imgur.com/Uovh293.png" - : MyApp.controller.currentUser.spots.last.music - .linkCover), + image: NetworkImage(MyApp.controller.getSpots().isEmpty + ? "https://i.imgur.com/Uovh293.png" + : MyApp.controller.getSpots().last.music.linkCover), fit: BoxFit.cover, ), ), @@ -49,21 +47,19 @@ class _SpotsWidgetState extends State { ), ), Align( - alignment: FractionalOffset.bottomCenter, - child: MyApp.controller.currentUser.spots.isEmpty - ? Container() - : OpenContainer( - closedColor: Colors.transparent, - closedElevation: 0, - transitionDuration: const Duration(milliseconds: 400), - closedBuilder: (context, openWidget) { - return const PreviewInfoWidget(); - }, - openBuilder: (context, closeWidget) { - return const DisplayInfoWidget(); - }, - ), - ), + alignment: FractionalOffset.bottomCenter, + child: MyApp.controller.getSpots().isEmpty + ? Container() + : OpenContainer( + closedColor: Colors.transparent, + closedElevation: 0, + transitionDuration: const Duration(milliseconds: 400), + closedBuilder: (context, openWidget) { + return const PreviewInfoWidget(); + }, + openBuilder: (context, closeWidget) { + return const DisplayInfoWidget(); + })), const Center( child: SizedBox( width: 300, @@ -75,7 +71,7 @@ class _SpotsWidgetState extends State { Positioned( top: height * 0.68, width: width, - child: MyApp.controller.currentUser.spots.isEmpty + child: MyApp.controller.getSpots().isEmpty ? Container() : Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, @@ -172,9 +168,9 @@ class _SpotsWidgetState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - MyApp.controller.currentUser.spots.isEmpty + MyApp.controller.getSpots().isEmpty ? '' - : MyApp.controller.currentUser.spots.last.music.name, + : MyApp.controller.getSpots().last.music.name, style: TextStyle( fontFamily: 'DMSans', color: Colors.white.withOpacity(1), @@ -182,10 +178,9 @@ class _SpotsWidgetState extends State { fontWeight: FontWeight.w800), ), Text( - MyApp.controller.currentUser.spots.isEmpty + MyApp.controller.getSpots().isEmpty ? '' - : MyApp - .controller.currentUser.spots.last.music.artist, + : MyApp.controller.getSpots().last.music.artist, style: TextStyle( fontFamily: 'DMSans', color: Colors.white.withOpacity(1), @@ -200,12 +195,12 @@ class _SpotsWidgetState extends State { right: 0, child: GestureDetector( onTap: () { - MyApp.api.playTrack( - MyApp.controller.currentUser.spots.last.music.id); + MyApp.controller + .playTrack(MyApp.controller.getSpots().last.music.id); }, child: SizedBox( height: 40, - child: !MyApp.controller.currentUser.spots.isEmpty + child: MyApp.controller.getSpots().isEmpty ? Image.asset("assets/images/play_spotify_button.png") : Container(), ), 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 62fdf45..750f061 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 @@ -258,10 +258,10 @@ class _SignInPageState extends State { } else if (password == "") { notify(4, context); } else { - await MyApp.controller - .load(userNameTextField.text, passwordTextField.text); + /*await MyApp.controller + .load(userNameTextField.text, passwordTextField.text);*/ - if (MyApp.controller.currentUser.usernameDafl != "") { + if (MyApp.controller.getIdDafl().toString() != "") { 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 7672c5d..f50fa7e 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 @@ -1,8 +1,8 @@ import 'package:dafl_project_flutter/main.dart'; import 'package:flutter/material.dart'; import 'package:page_transition/page_transition.dart'; -import '../../../api/in_app_browser.dart'; import '../../../model/user.dart'; +import '../../../services/api/in_app_browser.dart'; import '../home/p_home.dart'; import '../sign_in/p_sign_in.dart'; @@ -334,9 +334,10 @@ class _SignUpPageState extends State { String username, String password, String confirmPassword) async { if (username == "") { notify(2, context); - } else if (!await MyApp.controller.searchByUsername(username)) { - notify(0, context); } + /* else if (!await MyApp.controller.searchByUsername(username)) { + notify(0, context); + }*/ if (password == "" || confirmPassword == "") { notify(4, context); } else if (password.length < 8) { @@ -344,7 +345,7 @@ class _SignUpPageState extends State { } else if (password != confirmPassword) { notify(1, context); } else { - MyApp.controller.save(User(username, password)); + //MyApp.controller.save(User(username, password)); Navigator.of(context).push( PageTransition(