Changements et réorganisation des appels des méthodes en fonction de la temporalité des actions de l'utilisateur

remotes/origin/database-api-implementation
Félix MIELCAREK 3 years ago committed by felixmielcarek
parent a526982796
commit 277edb2031

@ -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,16 +102,16 @@ class Controller {
}
// DATABASE
void save(User userToSave) {
_dataBaseService.save(userToSave);
void save(String idDafl, String passw) {
_dataBaseService.save(idDafl, passw);
}
Future<bool> 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;
}

@ -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);
}

@ -167,7 +167,7 @@ class ApiSpotifyRequests extends HttpResponseVerification {
}
Future<LinkedHashMap<String, DateTime>> 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();

@ -9,16 +9,15 @@ class DatabaseLoader implements Loader {
@override
Future<User?> 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;
}

@ -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();
});
}

@ -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<User?> load(String username, String password) async {
@ -35,7 +34,4 @@ class DataBaseService{
changeCurrentPassword(String newPass) {
//TODO : call database method
}
}

@ -1,5 +1,3 @@
import '../../model/user.dart';
abstract class Saver {
void save(User userToSave);
void save(String idDafl, String passw);
}

@ -9,9 +9,7 @@ class Location {
static Future<List<Spot>> 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();

@ -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';

@ -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<MainPage> {
const SettingsWidget(),
];
@override
void initState() {
MyApp.controller.beginRoutine();
super.initState();
}
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;

@ -134,6 +134,8 @@ class _DiscoveryListState extends State<DiscoveryList> {
key: (k) => k, value: (k) => sortedKeys[k]); */
listDiscoveries = LinkedHashMap();
}
//TODO : remove next line
listDiscoveries = MyApp.controller.getDiscoveries();
return RefreshIndicator(
onRefresh: () async {
refreshList();

@ -29,9 +29,9 @@ class _MainPageProfilState extends State<MainPageProfil> {
String username = MyApp.controller.getIdDafl().toString();
@override
initState() async {
initState() {
username = MyApp.controller.getIdDafl();
super.initState();
username = MyApp.controller.getIdDafl().toString();
}
@override

@ -257,8 +257,9 @@ class _SignInPageState extends State<SignInPage> {
notify(2, context);
} else if (password == "") {
notify(4, context);
} else if(await MyApp.controller
} else if (await MyApp.controller
.load(userNameTextField.text, passwordTextField.text)) {
MyApp.controller.initUser();
Navigator.of(context).push(
PageTransition(
type: PageTransitionType.fade,
@ -269,4 +270,4 @@ class _SignInPageState extends State<SignInPage> {
notify(2, context);
}
}
}
}

@ -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<SignUpPage> {
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<SignUpPage> {
} else if (password != confirmPassword) {
notify(1, context);
} else {
MyApp.controller.save(User(username, password));
// create user in database
Navigator.of(context).push(
PageTransition(

Loading…
Cancel
Save