From e738a92cfd0511a0bdf2741686866c94ba1bd672 Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Fri, 4 Aug 2023 00:20:00 +0200 Subject: [PATCH] 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;