From c8b93c51cd1244a5bc9a11e89375ad226b58a37d Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Thu, 3 Aug 2023 21:27:11 +0200 Subject: [PATCH 1/2] Get, add and delete Favorite Musics done --- .../justMUSIC/lib/services/AuthService.dart | 1 + .../justMUSIC/lib/services/MusicService.dart | 46 +++++++++++++++++++ Sources/justMUSIC/lib/values/keys.dart | 2 + .../lib/view_model/CommentViewModel.dart | 4 ++ .../lib/view_model/MusicViewModel.dart | 30 ++++++++++++ .../lib/view_model/TokenSpotify.dart | 6 +-- .../lib/view_model/UserViewModel.dart | 25 ++++------ 7 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 Sources/justMUSIC/lib/services/MusicService.dart diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 6e77d10..79bb717 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -29,6 +29,7 @@ class AuthService { "nbCapsules": 0, "followers": [], "token_notify": token, + "musics_likes": [], "picture": "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc" }; diff --git a/Sources/justMUSIC/lib/services/MusicService.dart b/Sources/justMUSIC/lib/services/MusicService.dart new file mode 100644 index 0000000..cbc14a2 --- /dev/null +++ b/Sources/justMUSIC/lib/services/MusicService.dart @@ -0,0 +1,46 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; + +import '../main.dart'; + +class MusicService { + Future getFavoriteMusicsByUserId(String id) async { + var response = + await FirebaseFirestore.instance.collection("users").doc(id).get(); + if (response.exists) { + var musicFavorite = response.get("musics_likes"); + return List.from(musicFavorite); + } else { + return []; + } + } + + deleteFavoriteMusic(String id) async { + var userRef = await FirebaseFirestore.instance + .collection("users") + .doc(MyApp.userViewModel.userCurrent.id); + var response = await userRef.get(); + + List musicFavorite = List.from(response.get("musics_likes")); + if (!musicFavorite.contains(id)) { + musicFavorite.remove(id); + await userRef.update({"musics_likes": musicFavorite}); + } else { + print("Delete error: The music is not in the user's favorite music list"); + } + } + + addFavoriteMusic(String id) async { + var userRef = await FirebaseFirestore.instance + .collection("users") + .doc(MyApp.userViewModel.userCurrent.id); + var response = await userRef.get(); + + List musicFavorite = List.from(response.get("musics_likes")); + if (!musicFavorite.contains(id)) { + musicFavorite.add(id); + await userRef.update({"musics_likes": musicFavorite}); + } else { + print("Add error: The music is not in the user's favorite music list"); + } + } +} diff --git a/Sources/justMUSIC/lib/values/keys.dart b/Sources/justMUSIC/lib/values/keys.dart index 4654117..8ae4fe9 100644 --- a/Sources/justMUSIC/lib/values/keys.dart +++ b/Sources/justMUSIC/lib/values/keys.dart @@ -1 +1,3 @@ const geoKey = "85a2724ad38b3994c2b7ebe1d239bbff"; +const clientId = "d9b82921bbdf43efa15d0c34c28c6f93"; +const clientSecret = "ba01687f59ea4ab7ad00c769e89e44d8"; \ No newline at end of file diff --git a/Sources/justMUSIC/lib/view_model/CommentViewModel.dart b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart index c3aa0a4..c0616d8 100644 --- a/Sources/justMUSIC/lib/view_model/CommentViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart @@ -7,10 +7,14 @@ class CommentViewModel { List _comments = []; final CommentService _commentService = CommentService(); + List get comments => _comments; + // Constructor CommentViewModel(); // Methods + + addComment(String text, String idPost) async { try { await _commentService.createComment(text, idPost); diff --git a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart index c5afd40..eab00f0 100644 --- a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart @@ -4,10 +4,12 @@ import 'package:justmusic/view_model/TokenSpotify.dart'; import 'package:http/http.dart' as http; import '../model/Artist.dart'; import '../model/Music.dart'; +import '../services/MusicService.dart'; class MusicViewModel { final String API_URL = "https://api.spotify.com/v1"; late TokenSpotify _token; + MusicService _musicService = MusicService(); MusicViewModel() { _token = new TokenSpotify(); @@ -232,4 +234,32 @@ class MusicViewModel { limit: limit, offset: offset, market: market); } } + + Future> getFavoriteMusicsByUserId(String id) async { + try { + var idMusics = await _musicService.getFavoriteMusicsByUserId(id); + return await getMusicsWithIds(idMusics); + } catch (e) { + print(e); + rethrow; + } + } + + addFavoriteMusic(String id) async { + try { + await _musicService.addFavoriteMusic(id); + } catch (e) { + print(e); + rethrow; + } + } + + deleteFavoriteMusic(String id) async { + try { + await _musicService.deleteFavoriteMusic(id); + } catch (e) { + print(e); + rethrow; + } + } } diff --git a/Sources/justMUSIC/lib/view_model/TokenSpotify.dart b/Sources/justMUSIC/lib/view_model/TokenSpotify.dart index a221129..00fe7dc 100644 --- a/Sources/justMUSIC/lib/view_model/TokenSpotify.dart +++ b/Sources/justMUSIC/lib/view_model/TokenSpotify.dart @@ -1,9 +1,9 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import '../values/keys.dart'; + class TokenSpotify { - final String clientId = 'd9b82921bbdf43efa15d0c34c28c6f93'; - final String _clientSecret = 'ba01687f59ea4ab7ad00c769e89e44d8'; late String _accessToken; late DateTime _tokenEnd; @@ -19,7 +19,7 @@ class TokenSpotify { } _refreshToken() async { - final basicAuth = base64Encode(utf8.encode('$clientId:$_clientSecret')); + final basicAuth = base64Encode(utf8.encode('$clientId:$clientSecret')); final response = await http.post( Uri.parse('https://accounts.spotify.com/api/token'), headers: { diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index 178b522..964beac 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -17,7 +17,9 @@ class UserViewModel { set userCurrent(User value) { _userCurrent = value; - } // Constructor + } + + // Constructor UserViewModel(); @@ -31,20 +33,14 @@ class UserViewModel { try { var token; await authService.login(pseudo, password); - final user = await MyApp.db - .collection("users") - .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) - .get(); + await updateUserCurrent(); if (!kIsWeb) { token = await FirebaseMessaging.instance.getToken(); - if (MyApp.userViewModel.userCurrent.token != token) { - _userService.updateTokenNotify( - MyApp.userViewModel.userCurrent.id, token); - MyApp.userViewModel.userCurrent.token = token; + if (_userCurrent.token != token) { + _userService.updateTokenNotify(_userCurrent.id, token); + _userCurrent.token = token; } } - User data = UserMapper.toModel(user); - _userCurrent = data; } catch (e) { rethrow; } @@ -75,12 +71,7 @@ class UserViewModel { try { await authService.register(pseudo.toLowerCase(), email, password); - final user = await MyApp.db - .collection("users") - .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) - .get(); - User data = UserMapper.toModel(user); - _userCurrent = data; + await updateUserCurrent(); } catch (e) { rethrow; } -- 2.36.3 From e738a92cfd0511a0bdf2741686866c94ba1bd672 Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Fri, 4 Aug 2023 00:20:00 +0200 Subject: [PATCH 2/2] Merge add and delete Favorite Music Functions --- Sources/justMUSIC/lib/services/MusicService.dart | 7 +++++-- .../justMUSIC/lib/services/NotificationService.dart | 3 ++- Sources/justMUSIC/lib/values/keys.dart | 4 +++- .../justMUSIC/lib/view_model/MusicViewModel.dart | 13 ++----------- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Sources/justMUSIC/lib/services/MusicService.dart b/Sources/justMUSIC/lib/services/MusicService.dart index cbc14a2..a100258 100644 --- a/Sources/justMUSIC/lib/services/MusicService.dart +++ b/Sources/justMUSIC/lib/services/MusicService.dart @@ -29,7 +29,7 @@ class MusicService { } } - addFavoriteMusic(String id) async { + Future addOrDeleteFavoriteMusic(String id) async { var userRef = await FirebaseFirestore.instance .collection("users") .doc(MyApp.userViewModel.userCurrent.id); @@ -39,8 +39,11 @@ class MusicService { if (!musicFavorite.contains(id)) { musicFavorite.add(id); await userRef.update({"musics_likes": musicFavorite}); + return false; } else { - print("Add error: The music is not in the user's favorite music list"); + musicFavorite.remove(id); + await userRef.update({"musics_likes": musicFavorite}); + return true; } } } diff --git a/Sources/justMUSIC/lib/services/NotificationService.dart b/Sources/justMUSIC/lib/services/NotificationService.dart index ac02322..67494b9 100644 --- a/Sources/justMUSIC/lib/services/NotificationService.dart +++ b/Sources/justMUSIC/lib/services/NotificationService.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:http/http.dart' as http; import '../main.dart'; +import '../values/keys.dart'; class NotificationService { sendPushMessage(String token, String title, String body) async { @@ -10,7 +11,7 @@ class NotificationService { headers: { 'Content-Type': 'application/json', 'Authorization': - 'key=AAAA56TmIPg:APA91bFeKMr_i6CbUuuUdFI1XkdaNE2A7OVHzxrPIsOSlDfhR6qzZwof7JNGxthWUKj1dRHQMheWNYaLbf3AtXUp9o4DX_gB2073yR4urqUEh9CjvnxVws_9g1cWMgmFS3EpaQEA3icC' + 'key=$keyApiFirebase' }, body: jsonEncode({ 'priority': 'high', diff --git a/Sources/justMUSIC/lib/values/keys.dart b/Sources/justMUSIC/lib/values/keys.dart index 8ae4fe9..b2f8586 100644 --- a/Sources/justMUSIC/lib/values/keys.dart +++ b/Sources/justMUSIC/lib/values/keys.dart @@ -1,3 +1,5 @@ const geoKey = "85a2724ad38b3994c2b7ebe1d239bbff"; const clientId = "d9b82921bbdf43efa15d0c34c28c6f93"; -const clientSecret = "ba01687f59ea4ab7ad00c769e89e44d8"; \ No newline at end of file +const clientSecret = "ba01687f59ea4ab7ad00c769e89e44d8"; +const keyApiFirebase = + "AAAA56TmIPg:APA91bFeKMr_i6CbUuuUdFI1XkdaNE2A7OVHzxrPIsOSlDfhR6qzZwof7JNGxthWUKj1dRHQMheWNYaLbf3AtXUp9o4DX_gB2073yR4urqUEh9CjvnxVws_9g1cWMgmFS3EpaQEA3icC"; diff --git a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart index eab00f0..c84c691 100644 --- a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart @@ -245,18 +245,9 @@ class MusicViewModel { } } - addFavoriteMusic(String id) async { + Future addOrDeleteFavoriteMusic(String id) async { try { - await _musicService.addFavoriteMusic(id); - } catch (e) { - print(e); - rethrow; - } - } - - deleteFavoriteMusic(String id) async { - try { - await _musicService.deleteFavoriteMusic(id); + return await _musicService.addOrDeleteFavoriteMusic(id); } catch (e) { print(e); rethrow; -- 2.36.3