From 160d75a3e7fd852fc45fac38e976d198be5819b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Mielcarek?= Date: Wed, 30 Nov 2022 10:07:49 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20classe=20data=20pour=20=C3=A9viter=20au?= =?UTF-8?q?=20maximum=20les=20m=C3=A9thodes=20asynchrone=20=20et=20les=20r?= =?UTF-8?q?equ=C3=AAtes.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/controller/controller.dart | 81 +++++++++++-------- .../lib/controller/live_datas.dart | 14 ++++ Sources/dafl_project_flutter/lib/main.dart | 4 +- .../dafl_project_flutter/lib/model/user.dart | 6 -- .../lib/services/position/location.dart | 23 +++--- .../lib/views/pages/main/w_discovery.dart | 8 +- .../lib/views/pages/main/w_profile.dart | 6 -- 7 files changed, 77 insertions(+), 65 deletions(-) create mode 100644 Sources/dafl_project_flutter/lib/controller/live_datas.dart diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index 9e73a5d..5ae7f07 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -1,70 +1,79 @@ import 'dart:convert'; +import 'package:dafl_project_flutter/controller/live_datas.dart'; import 'package:dafl_project_flutter/model/music.dart'; +import 'package:dafl_project_flutter/model/spot.dart'; import 'package:dafl_project_flutter/services/api/api_spotify.dart'; import 'package:dafl_project_flutter/services/database/database_service.dart'; import 'package:dafl_project_flutter/services/position/location.dart'; import 'package:flutter/cupertino.dart'; import 'package:http/http.dart' as http; -import '../model/spot.dart'; import '../model/user.dart'; class Controller { ApiSpotify _api = ApiSpotify(); late User _currentUser; - final Location _location = Location(); final DataBaseService _dataBaseService = DataBaseService(); - bool sortChoice = false; //false = sort by name ; true = sort by date + final LiveDatas _datas = LiveDatas(); late BuildContext navigatorKey; - Uri getApiUrlAuthorize() { - return _api.identification.urlAuthorize; - } + // + // Methods to manage datas + // - String getApiRedirectUrl() { - return _api.identification.redirectUri; - } + // Datas that can change - apiAuthorization(url) { - _api.apiAuthorization(url); - } + bool getChoice() => _datas.discoveriesSortChoice; - String getIdSpotify() { - return _currentUser.idSpotify; + setChoice(bool c) { + _datas.discoveriesSortChoice = c; } - Future getCompleteMusic(String id) async { - Map infos = await _api.requests.getTrackInfo(id); - return Music(id, infos['name'], infos['artist'], infos['cover']); - } + Music getCurrentMusic() => _datas.userCurrentMusic; setCurrentMusic() async { - _currentUser.currentMusic = await _api.requests.getCurrentlyPlayingTrack(); + _datas.userCurrentMusic = + await getCompleteMusic(await _api.requests.getCurrentlyPlayingTrack()); } - String getCurrentMusic() { - return _currentUser.currentMusic; - } + List getSpots() => _datas.spots; - int getIdDafl() { - return _currentUser.idDafl; + setSpots() async { + _datas.spots = await Location.sendCurrentLocation(); } - List getSpots() { - return _location.spots; - } + Map getDiscoveries() => _datas.discoveries; - getLocation() async { - await _location.sendCurrentLocation(); + setDiscoveries() async { + Map tmpData = await _api.requests.getPlaylistTracks(); + Map tmpCast = {}; + tmpData.forEach((key, value) async { + tmpCast[(await getCompleteMusic(key))] = value; + }); + _datas.discoveries = tmpCast; } - playTrack(String id) { - _api.requests.playTrack(id); + //Data that can not change + + Uri getApiUrlAuthorize() => _api.identification.urlAuthorize; + + String getApiRedirectUrl() => _api.identification.redirectUri; + + String getIdSpotify() => _currentUser.idSpotify; + + int getIdDafl() => _currentUser.idDafl; + + // + //Other methods + // + + apiAuthorization(url) { + _api.apiAuthorization(url); } - Future> getDiscoveries() async { - _currentUser.discoveries = await _api.requests.getPlaylistTracks(); - return _currentUser.discoveries; + Future getCompleteMusic(String id) async { + Map infos = await _api.requests.getTrackInfo(id); + return Music(id, infos['name'], infos['artist'], infos['cover']); } removeFromPlaylist(String id) { @@ -75,6 +84,10 @@ class Controller { _api.requests.addToPlaylist(id); } + playTrack(String id) { + _api.requests.playTrack(id); + } + // DATABASE void save(User userToSave) { _dataBaseService.save(userToSave); diff --git a/Sources/dafl_project_flutter/lib/controller/live_datas.dart b/Sources/dafl_project_flutter/lib/controller/live_datas.dart new file mode 100644 index 0000000..e7878ec --- /dev/null +++ b/Sources/dafl_project_flutter/lib/controller/live_datas.dart @@ -0,0 +1,14 @@ +import 'package:dafl_project_flutter/main.dart'; + +import '../model/music.dart'; +import '../model/spot.dart'; + +class LiveDatas { + bool discoveriesSortChoice = + false; //false = sort by name ; true = sort by date + Map discoveries; + List spots; + Music userCurrentMusic; + + LiveDatas() {} +} diff --git a/Sources/dafl_project_flutter/lib/main.dart b/Sources/dafl_project_flutter/lib/main.dart index d7b1dad..46d2b15 100644 --- a/Sources/dafl_project_flutter/lib/main.dart +++ b/Sources/dafl_project_flutter/lib/main.dart @@ -175,7 +175,7 @@ class CardProvider extends ChangeNotifier { child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - await MyApp.controller.getDiscoveries().containsKey( + MyApp.controller.getDiscoveries().containsKey( MyApp.controller.currentUser.spots.last.music) ? const Icon( Icons.info_rounded, @@ -327,7 +327,7 @@ class CardProvider extends ChangeNotifier { child: ElevatedButton( onPressed: () { sendMessage(messageTextField.text, - MyApp.controller.getSpots().last.userId); + MyApp.controller.getSpots().last.key); }, style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF3F1DC3), diff --git a/Sources/dafl_project_flutter/lib/model/user.dart b/Sources/dafl_project_flutter/lib/model/user.dart index 967b8dc..b8d76e0 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -1,7 +1,4 @@ import 'dart:async'; -import '../main.dart'; -import 'music.dart'; -import 'spot.dart'; class User { Timer? timer; @@ -11,11 +8,8 @@ class User { late int idDafl; late String usernameDafl; late String passwDafl; - Map discoveries = {}; final String _idSpotify; - late String currentMusic; - bool sortChoise = true; //constructors User(this.usernameDafl, this._idSpotify); diff --git a/Sources/dafl_project_flutter/lib/services/position/location.dart b/Sources/dafl_project_flutter/lib/services/position/location.dart index 563b11e..62fd532 100644 --- a/Sources/dafl_project_flutter/lib/services/position/location.dart +++ b/Sources/dafl_project_flutter/lib/services/position/location.dart @@ -6,17 +6,7 @@ import 'dart:async'; import '../../main.dart'; class Location { - final Map _spots = {}; - - List get spots { - List spots = []; - _spots.forEach((key, value) { - spots.add(Spot(key, value)); - }); - return spots; - } - - sendCurrentLocation() async { + static Future> sendCurrentLocation() async { Uri uri = Uri.parse( "https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/insertAndMakeListUser.php"); LocationPermission permission; @@ -32,7 +22,7 @@ class Location { } String actualUser = MyApp.controller.getIdDafl().toString(); - String actualSong = MyApp.controller.getCurrentMusic(); + String actualSong = MyApp.controller.getCurrentMusic().id; Position current = await Geolocator.getCurrentPosition(); http.Response response = await http.post(uri, body: { @@ -43,13 +33,20 @@ class Location { }); var data = jsonDecode(response.body); + Map spotsData = {}; + List spots = []; if (data == 2) { return Future.error("Failed to connect, connection timeout"); } else if (data == 3) { return Future.error("POST method failed"); } else { - data.forEach((s) => _spots.putIfAbsent(s['user'], () => s['music'])); + data.forEach((s) => spotsData.putIfAbsent(s['user'], () => s['music'])); } + + spotsData.forEach((key, value) async { + spots.add(Spot(key, await MyApp.controller.getCompleteMusic(value))); + }); + return spots; } } 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 81f6c7d..7f65424 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 @@ -39,8 +39,8 @@ class _DiscoveryWidgetState extends State { ), OutlinedButton( onPressed: () { - MyApp.controller.sortChoice = - !MyApp.controller.sortChoice; + MyApp.controller + .setChoice(!MyApp.controller.getChoice()); rebuildAllChildren(context); setState(() {}); }, @@ -49,7 +49,7 @@ class _DiscoveryWidgetState extends State { shadowColor: Colors.black, shape: const CircleBorder(), padding: const EdgeInsets.all(24)), - child: MyApp.controller.sortChoice + child: MyApp.controller.getChoice() ? Image.asset( 'assets/images/date_sort_icon.png', height: 25, @@ -119,7 +119,7 @@ class _DiscoveryListState extends State { @override Widget build(BuildContext context) { var listDiscoveries = MyApp.controller.getDiscoveries(); - if (MyApp.controller.sortChoice) { + if (MyApp.controller.getChoice()) { listDiscoveries.sort((a, b) { return a.date.compareTo(b.date); }); 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 f91b958..acd6c26 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 @@ -30,12 +30,6 @@ class MainPageProfil extends StatefulWidget { class _MainPageProfilState extends State { String username = MyApp.controller.getIdDafl().toString(); - late Future data; - - Future getData() async { - return await MyApp.controller - .getCompleteMusic(MyApp.controller.getCurrentMusic()); - } @override initState() async {