Ajout classe data pour éviter au maximum les méthodes asynchrone et les requêtes.

remotes/origin/database-api-implementation
Félix MIELCAREK 2 years ago committed by felixmielcarek
parent e049a265cd
commit 2b1ad41178

@ -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<Music> 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<Spot> getSpots() => _datas.spots;
int getIdDafl() {
return _currentUser.idDafl;
setSpots() async {
_datas.spots = await Location.sendCurrentLocation();
}
List<Spot> getSpots() {
return _location.spots;
}
Map<Music, DateTime> getDiscoveries() => _datas.discoveries;
getLocation() async {
await _location.sendCurrentLocation();
setDiscoveries() async {
Map<String, DateTime> tmpData = await _api.requests.getPlaylistTracks();
Map<Music, DateTime> 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<Map<String, DateTime>> getDiscoveries() async {
_currentUser.discoveries = await _api.requests.getPlaylistTracks();
return _currentUser.discoveries;
Future<Music> 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);

@ -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<Music, DateTime> discoveries;
List<Spot> spots;
Music userCurrentMusic;
LiveDatas() {}
}

@ -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),

@ -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<String, DateTime> discoveries = {};
final String _idSpotify;
late String currentMusic;
bool sortChoise = true;
//constructors
User(this.usernameDafl, this._idSpotify);

@ -6,17 +6,7 @@ import 'dart:async';
import '../../main.dart';
class Location {
final Map _spots = {};
List<Spot> get spots {
List<Spot> spots = [];
_spots.forEach((key, value) {
spots.add(Spot(key, value));
});
return spots;
}
sendCurrentLocation() async {
static Future<List<Spot>> 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<String, String> spotsData = {};
List<Spot> 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;
}
}

@ -39,8 +39,8 @@ class _DiscoveryWidgetState extends State<DiscoveryWidget> {
),
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<DiscoveryWidget> {
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<DiscoveryList> {
@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);
});

@ -30,12 +30,6 @@ class MainPageProfil extends StatefulWidget {
class _MainPageProfilState extends State<MainPageProfil> {
String username = MyApp.controller.getIdDafl().toString();
late Future<Music> data;
Future<Music> getData() async {
return await MyApp.controller
.getCompleteMusic(MyApp.controller.getCurrentMusic());
}
@override
initState() async {

Loading…
Cancel
Save