You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dafl_music/Sources/dafl_project_flutter/lib/model/user.dart

113 lines
2.8 KiB

import 'dart:async';
import '../../../api/track.dart';
import '../../../position/location.dart';
import '../exceptions/api_exception.dart';
import '../main.dart';
import 'conversation.dart';
import 'music.dart';
import 'spot.dart';
Timer? timer;
int test=0;
class User {
//attributes from DAFL
late int idDafl;
late String usernameDafl;
late String passwDafl;
//attributes with Spotify API
String? _idSpotify; //use _getIdUser() as kind of a private getter
late Music _currentMusic;
Set<User> likedUsers = {};
List<Music> discovery = [];
List<Conversation> waitingConv = [];
List<Conversation> confirmConv = [];
List<Spot> spots = [];
Map<User, Conversation> conversations = {};
//constructors
User(this.usernameDafl, this.passwDafl) {
_actualiseCurrentMusic();
}
Music get currentMusic => _currentMusic; //lists
Future<String> getIdSpotify() async {
_idSpotify ??= await MyApp.api.getIdUser();
return _idSpotify!;
}
void addDiscovery(Music newmusic) {
MyApp.controller.currentUser.discovery.add(newmusic);
}
void like(User liked) {
likedUsers.add(liked);
Conversation? conv = liked.conversations[this];
if (conv == null) {
conversations[liked] = Conversation(this, liked);
} else {
conversations[liked] = conv;
}
}
void chat(User recipient, String content) {
Conversation? conv = conversations[recipient];
if (conv != null) conv.addMessage(this, content);
}
void displayConversations() {
conversations.forEach((k, v) => v.displayMessages());
}
_actualiseCurrentMusic() async {
try {
_currentMusic = Music(await MyApp.api.getCurrentlyPlayingTrack());
} on ApiException {
// TODO : add notification to show that an error occurred
}
}
void listspots (){
Future<String>? rep;
int i;
rep = Location.sendCurrentLocation();
List<Future<Music>> futureMusicList = [];
List<List<String>> musicId = [];
rep.then((String result) {
List<String> tab = result.split(",");
if (tab.isEmpty!=true) {
for (i = 0; i < tab.length; i++) {
musicId.add(tab[i].split("-"));
}
/*
for (i = 0; i < musicId.length; i++) {
// futuretracklist.add(MyApp.api.getTrackInfo(trackid[i][1]));
}
futureMusicList[i].then((Music m) {
for (i = 0; i < futureMusicList.length; i++) {
discovery.add(m);
}
});
*/ // EN COMMENTAIRE PARCE QUE ERREUR SINON VU QUE J'AI PAS MUSIC POUR L'INSTANT
}
});
}
void getListSpots(){
if (test==0){
test=1;
listspots();
}else{
timer = Timer.periodic(const Duration(seconds: 72), (Timer t) => listspots());
}
}
@override
String toString() => "$usernameDafl ($passwDafl)";
}