From 277edb2031fddf896e82be94da5b59fd7d7c552b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Mielcarek?= Date: Wed, 30 Nov 2022 21:25:41 +0100 Subject: [PATCH] =?UTF-8?q?Changements=20et=20r=C3=A9organisation=20des=20?= =?UTF-8?q?appels=20des=20m=C3=A9thodes=20en=20fonction=20de=20la=20tempor?= =?UTF-8?q?alit=C3=A9=20des=20actions=20de=20l'utilisateur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/controller/controller.dart | 35 +++++++++++-------- .../dafl_project_flutter/lib/model/user.dart | 16 ++------- .../services/api/api_spotify_requests.dart | 2 +- .../services/database/database_loader.dart | 5 ++- .../lib/services/database/database_saver.dart | 9 ++--- .../services/database/database_service.dart | 12 +++---- .../lib/services/database/saver.dart | 4 +-- .../lib/services/position/location.dart | 6 ++-- .../lib/views/pages/home/p_home.dart | 1 + .../lib/views/pages/main/p_main.dart | 7 ++++ .../lib/views/pages/main/w_discovery.dart | 2 ++ .../lib/views/pages/main/w_profile.dart | 4 +-- .../lib/views/pages/sign_in/p_sign_in.dart | 21 +++++------ .../lib/views/pages/sign_up/p_sign_up.dart | 6 ++-- 14 files changed, 60 insertions(+), 70 deletions(-) diff --git a/Sources/dafl_project_flutter/lib/controller/controller.dart b/Sources/dafl_project_flutter/lib/controller/controller.dart index 2a8843a..a5711e2 100644 --- a/Sources/dafl_project_flutter/lib/controller/controller.dart +++ b/Sources/dafl_project_flutter/lib/controller/controller.dart @@ -1,15 +1,15 @@ import 'dart:async'; import 'dart:collection'; 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/music.dart'; +import '../model/spot.dart'; import '../model/user.dart'; +import '../services/api/api_spotify.dart'; +import '../services/database/database_service.dart'; +import '../services/position/location.dart'; +import 'live_datas.dart'; class Controller { final ApiSpotify _api = ApiSpotify(); @@ -19,8 +19,13 @@ class Controller { late BuildContext navigatorKey; - Controller() { - setSpots(); + initUser() async { + await setCurrentMusic(); + await setDiscoveries(); + } + + beginRoutine() async { + await setSpots(); Timer.periodic(const Duration(seconds: 10), (Timer t) => setSpots()); } @@ -69,7 +74,7 @@ class Controller { String getIdSpotify() => _currentUser.idSpotify; - int getIdDafl() => _currentUser.idDafl; + String getIdDafl() => _currentUser.idDafl; // //Other methods @@ -97,18 +102,18 @@ class Controller { } // DATABASE - void save(User userToSave) { - _dataBaseService.save(userToSave); + void save(String idDafl, String passw) { + _dataBaseService.save(idDafl, passw); } Future load(String username, String password) async { User? newUser = await _dataBaseService.load(username, password); - if(newUser == null) + if (newUser == null) { return false; - else - _currentUser = newUser; - return true; + } + _currentUser = newUser; + return true; } changeUsername(String newName) { diff --git a/Sources/dafl_project_flutter/lib/model/user.dart b/Sources/dafl_project_flutter/lib/model/user.dart index b8d76e0..5f99498 100644 --- a/Sources/dafl_project_flutter/lib/model/user.dart +++ b/Sources/dafl_project_flutter/lib/model/user.dart @@ -1,18 +1,8 @@ -import 'dart:async'; - class User { - Timer? timer; - int test = 0; - //attributes from DAFL - late int idDafl; - late String usernameDafl; - late String passwDafl; - - final String _idSpotify; + String idDafl; + late String idSpotify; //constructors - User(this.usernameDafl, this._idSpotify); - - String get idSpotify => _idSpotify; + User(this.idDafl); } 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 3305497..1409b54 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 @@ -167,7 +167,7 @@ class ApiSpotifyRequests extends HttpResponseVerification { } Future> getPlaylistTracks() async { - var idPlaylist = _getPlaylistId(); + var idPlaylist = await _getPlaylistId(); var url = Uri.https('api.spotify.com', 'v1/playlists/$idPlaylist/tracks', {'fields': 'items(track(id),added_at)'}); var token = await _token.getAccessToken(); diff --git a/Sources/dafl_project_flutter/lib/services/database/database_loader.dart b/Sources/dafl_project_flutter/lib/services/database/database_loader.dart index 6539662..e7b4371 100644 --- a/Sources/dafl_project_flutter/lib/services/database/database_loader.dart +++ b/Sources/dafl_project_flutter/lib/services/database/database_loader.dart @@ -9,16 +9,15 @@ class DatabaseLoader implements Loader { @override Future load(String username, String password) async { final connection = await DatabaseConnexion.initConnexion(); - var queryResult = await connection .query( - 'select * from utilisateur where username = @username AND password = @password', + 'select username from utilisateur where username = @username AND password = @password', {'username': username, 'password': password}) .toList() .then((result) { dev.log(result.toString()); if (result.isNotEmpty) { - return User(username, password); + return User(username); } else { return null; } diff --git a/Sources/dafl_project_flutter/lib/services/database/database_saver.dart b/Sources/dafl_project_flutter/lib/services/database/database_saver.dart index 2c9f2d3..6768a50 100644 --- a/Sources/dafl_project_flutter/lib/services/database/database_saver.dart +++ b/Sources/dafl_project_flutter/lib/services/database/database_saver.dart @@ -1,20 +1,15 @@ import 'database_connexion.dart'; import 'saver.dart'; -import '../../model/user.dart'; class DatabaseSaver implements Saver { // Save user in the database @override - void save(User userToSave) async { + void save(String idDafl, String passw) async { final connection = await DatabaseConnexion.initConnexion(); connection.execute( 'insert into utilisateur (username, password) values (@username, @password)', - { - 'id': '', - 'username': userToSave.usernameDafl, - 'password': userToSave.passwDafl - }).whenComplete(() { + {'id': '', 'username': idDafl, 'password': passw}).whenComplete(() { connection.close(); }); } diff --git a/Sources/dafl_project_flutter/lib/services/database/database_service.dart b/Sources/dafl_project_flutter/lib/services/database/database_service.dart index ed8d743..8fb13a6 100644 --- a/Sources/dafl_project_flutter/lib/services/database/database_service.dart +++ b/Sources/dafl_project_flutter/lib/services/database/database_service.dart @@ -9,15 +9,14 @@ import 'package:dafl_project_flutter/services/database/searcher.dart'; import '../../model/user.dart'; -class DataBaseService{ +class DataBaseService { static final Loader _loader = DatabaseLoader(); static final Searcher _searcher = DatabaseSearcher(); static final Saver _saver = DatabaseSaver(); static final UserModifier _userModifier = DatabaseUserModifier(); - - void save(User userToSave) { - _saver.save(userToSave); + void save(String idDafl, String passw) { + _saver.save(idDafl, passw); } Future load(String username, String password) async { @@ -35,7 +34,4 @@ class DataBaseService{ changeCurrentPassword(String newPass) { //TODO : call database method } - - - -} \ No newline at end of file +} diff --git a/Sources/dafl_project_flutter/lib/services/database/saver.dart b/Sources/dafl_project_flutter/lib/services/database/saver.dart index 5c8462b..c75539a 100644 --- a/Sources/dafl_project_flutter/lib/services/database/saver.dart +++ b/Sources/dafl_project_flutter/lib/services/database/saver.dart @@ -1,5 +1,3 @@ -import '../../model/user.dart'; - abstract class Saver { - void save(User userToSave); + void save(String idDafl, String passw); } diff --git a/Sources/dafl_project_flutter/lib/services/position/location.dart b/Sources/dafl_project_flutter/lib/services/position/location.dart index 62fd532..ca443f1 100644 --- a/Sources/dafl_project_flutter/lib/services/position/location.dart +++ b/Sources/dafl_project_flutter/lib/services/position/location.dart @@ -9,9 +9,7 @@ class Location { static Future> sendCurrentLocation() async { Uri uri = Uri.parse( "https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/insertAndMakeListUser.php"); - LocationPermission permission; - - permission = await Geolocator.checkPermission(); + LocationPermission permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); @@ -21,7 +19,7 @@ class Location { } } - String actualUser = MyApp.controller.getIdDafl().toString(); + String actualUser = MyApp.controller.getIdDafl(); String actualSong = MyApp.controller.getCurrentMusic().id; Position current = await Geolocator.getCurrentPosition(); diff --git a/Sources/dafl_project_flutter/lib/views/pages/home/p_home.dart b/Sources/dafl_project_flutter/lib/views/pages/home/p_home.dart index daceb48..5dd4af8 100644 --- a/Sources/dafl_project_flutter/lib/views/pages/home/p_home.dart +++ b/Sources/dafl_project_flutter/lib/views/pages/home/p_home.dart @@ -1,3 +1,4 @@ +import 'package:dafl_project_flutter/main.dart'; import 'package:page_transition/page_transition.dart'; import 'package:flutter/material.dart'; import '../sign_in/p_sign_in.dart'; 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 4b3d579..acc4f92 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,4 +1,5 @@ import 'package:flutter/material.dart'; +import '../../../main.dart'; import '../../presentation/custom_icons_icons.dart'; import './w_settings.dart'; import './w_spot.dart'; @@ -27,6 +28,12 @@ class _MainPageState extends State { const SettingsWidget(), ]; + @override + void initState() { + MyApp.controller.beginRoutine(); + super.initState(); + } + @override Widget build(BuildContext context) { double height = MediaQuery.of(context).size.height; 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 c24307e..6df0077 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 @@ -134,6 +134,8 @@ class _DiscoveryListState extends State { key: (k) => k, value: (k) => sortedKeys[k]); */ listDiscoveries = LinkedHashMap(); } + //TODO : remove next line + listDiscoveries = MyApp.controller.getDiscoveries(); return RefreshIndicator( onRefresh: () async { refreshList(); 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 6942424..d783e36 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 @@ -29,9 +29,9 @@ class _MainPageProfilState extends State { String username = MyApp.controller.getIdDafl().toString(); @override - initState() async { + initState() { + username = MyApp.controller.getIdDafl(); super.initState(); - username = MyApp.controller.getIdDafl().toString(); } @override 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 bb14212..112fba3 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 @@ -257,16 +257,17 @@ class _SignInPageState extends State { notify(2, context); } else if (password == "") { notify(4, context); - } else if(await MyApp.controller + } else if (await MyApp.controller .load(userNameTextField.text, passwordTextField.text)) { - Navigator.of(context).push( - PageTransition( - type: PageTransitionType.fade, - childCurrent: widget, - child: const Splash()), - ); - } else { - notify(2, context); - } + MyApp.controller.initUser(); + Navigator.of(context).push( + PageTransition( + type: PageTransitionType.fade, + childCurrent: widget, + child: const Splash()), + ); + } else { + notify(2, context); } } +} 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 04e979b..a6bb740 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,7 +1,6 @@ import 'package:dafl_project_flutter/main.dart'; import 'package:flutter/material.dart'; import 'package:page_transition/page_transition.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,8 +333,7 @@ class _SignUpPageState extends State { String username, String password, String confirmPassword) async { if (username == "") { notify(2, context); - } - else if (! await MyApp.controller.searchUser(username)) { + } else if (!await MyApp.controller.searchUser(username)) { notify(0, context); } if (password == "" || confirmPassword == "") { @@ -345,7 +343,7 @@ class _SignUpPageState extends State { } else if (password != confirmPassword) { notify(1, context); } else { - MyApp.controller.save(User(username, password)); + // create user in database Navigator.of(context).push( PageTransition(