From ee88c97446ba743c18c2660c32534cd1dba8153d Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Wed, 16 Aug 2023 23:28:39 +0200 Subject: [PATCH 1/2] Add likes functions :white_check_mark: --- .../justMUSIC/lib/services/PostService.dart | 30 +++++++++++++++++++ .../lib/view_model/PostViewModel.dart | 18 +++++++++++ 2 files changed, 48 insertions(+) diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index a7d9742..f8a74db 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -128,4 +128,34 @@ class PostService { return recapList; } + + Future> getLikesByPostId(String id) async { + var response = await FirebaseFirestore.instance.collection("posts").doc(id).get(); + if (response.exists) { + var musicFavorite = response.get("likes"); + return List.from(musicFavorite); + } else { + return []; + } + } + + Future addOrDeleteFavoritePost(String id) async { + final idUser = MyApp.userViewModel.userCurrent.id; + var postRef = await FirebaseFirestore.instance + .collection("posts") + .doc(id); + var response = await postRef.get(); + + List likes = List.from(response.get("likes")); + + if (!likes.contains(idUser)) { + likes.add(idUser); + await postRef.update({"likes": likes}); + return false; + } else { + likes.remove(idUser); + await postRef.update({"likes": likes}); + return true; + } + } } diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index c70fbbb..b5d8545 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -98,4 +98,22 @@ class PostViewModel { rethrow; } } + + Future> getLikesByPostId(String id) async { + try { + return await _postService.getLikesByPostId(id); + } catch (e) { + print(e); + rethrow; + } + } + + Future addOrDeleteFavoritePost(String id) async { + try { + return await _postService.addOrDeleteFavoritePost(id); + } catch (e) { + print(e); + rethrow; + } + } } From b6d77d582b97f19422b95f3827445cf65797a422 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Thu, 17 Aug 2023 00:00:43 +0200 Subject: [PATCH 2/2] saved song --- .idea/libraries/Dart_Packages.xml | 56 +++++++++++++++ .../lib/screens/detail_post_screen.dart | 70 ++++++++++++++++++- .../justMUSIC/lib/services/PostService.dart | 42 ++++------- .../lib/view_model/PostViewModel.dart | 6 +- 4 files changed, 140 insertions(+), 34 deletions(-) diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index 8cb1ec7..70bceed 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -541,6 +541,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -800,6 +842,13 @@ + + + + + + @@ -1066,6 +1115,12 @@ + + + + + + @@ -1103,6 +1158,7 @@ + diff --git a/Sources/justMUSIC/lib/screens/detail_post_screen.dart b/Sources/justMUSIC/lib/screens/detail_post_screen.dart index 373ce89..397983e 100644 --- a/Sources/justMUSIC/lib/screens/detail_post_screen.dart +++ b/Sources/justMUSIC/lib/screens/detail_post_screen.dart @@ -49,6 +49,10 @@ class _DetailPostScreenState extends State { return MyApp.userViewModel.userCurrent.musics_likes.contains(widget.post.music.id); } + bool isLiked() { + return widget.post.likes.contains(MyApp.userViewModel.userCurrent.id); + } + @override void dispose() { MyApp.audioPlayer.release(); @@ -294,7 +298,71 @@ class _DetailPostScreenState extends State { mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [ - SvgPicture.asset("assets/images/heart.svg", semanticsLabel: 'Like Logo'), + Column( + children: [ + GestureDetector( + onTap: () async { + var bool = await MyApp.postViewModel + .addOrDeleteFavoritePost(widget.post.id); + if (!bool) { + widget.post.likes.add(MyApp.userViewModel.userCurrent.id); + } else { + widget.post.likes.remove(MyApp.userViewModel.userCurrent.id); + } + !bool + ? ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text("Vous avez liké cette capsule", + style: TextStyle(fontWeight: FontWeight.bold)), + backgroundColor: primaryColor, + closeIconColor: Colors.white, + ), + ) + : ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: SnackBar( + content: Text("Vous avez supprimé votre like", + style: TextStyle(fontWeight: FontWeight.bold)), + backgroundColor: primaryColor, + closeIconColor: Colors.white, + ), + backgroundColor: Colors.red, + closeIconColor: Colors.white, + ), + ); + setState(() {}); + }, + child: SvgPicture.asset( + "assets/images/heart.svg", + semanticsLabel: 'Like Logo', + color: isLiked() ? primaryColor : Colors.white, + ), + ), + Container( + padding: EdgeInsets.only(top: 8), + height: 30, + child: FutureBuilder>( + future: MyApp.postViewModel.getLikesByPostId(widget.post.id), + builder: + (BuildContext context, AsyncSnapshot> snapshot) { + if (snapshot.hasData) { + return Text(snapshot.data!.length.toString(), + style: GoogleFonts.plusJakartaSans( + color: Colors.white, + fontWeight: FontWeight.w800, + )); + } else { + return Container( + child: Center( + child: CupertinoActivityIndicator(), + ), + ); + } + }, + ), + ) + ], + ), Column( children: [ GestureDetector( diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index f8a74db..82bd2d0 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -7,8 +7,7 @@ import 'package:firebase_storage/firebase_storage.dart'; import '../main.dart'; class PostService { - createPost(String? description, String idMusic, File? image, - Tuple2? location) async { + createPost(String? description, String idMusic, File? image, Tuple2? location) async { var id = MyApp.userViewModel.userCurrent.id; final post = { "user_id": id, @@ -42,14 +41,11 @@ class PostService { deletePost() {} - Future>>> getPopularPosts( - {int limit = 10, int offset = 0}) async { + Future>>> getPopularPosts({int limit = 10, int offset = 0}) async { DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); - Timestamp twentyFourHoursAgoTimestamp = - Timestamp.fromDate(twentyFourHoursAgo); + Timestamp twentyFourHoursAgoTimestamp = Timestamp.fromDate(twentyFourHoursAgo); - QuerySnapshot> response = await FirebaseFirestore - .instance + QuerySnapshot> response = await FirebaseFirestore.instance .collection("posts") .where("date", isGreaterThan: twentyFourHoursAgoTimestamp) .limit(limit) @@ -67,8 +63,7 @@ class PostService { return Timestamp.fromDate(twentyFourHoursAgo); } - Future>>> getPostsFriends( - {int limit = 10, int offset = 0}) async { + Future>>> getPostsFriends({int limit = 10, int offset = 0}) async { var timestamp = _getTwentyFourHoursAgoTimestamp(); var response = await FirebaseFirestore.instance .collection("posts") @@ -84,17 +79,12 @@ class PostService { Future getAvailable(String idUser) async { DateTime today = DateTime.now(); - QuerySnapshot> response = await FirebaseFirestore - .instance - .collection("posts") - .where("user_id", isEqualTo: idUser) - .get(); + QuerySnapshot> response = + await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: idUser).get(); bool isTodayAvailable = response.docs.any((doc) { DateTime date = doc["date"].toDate(); // Assuming the field name is "date" - return date.day == today.day && - date.month == today.month && - date.year == today.year; + return date.day == today.day && date.month == today.month && date.year == today.year; }); return !isTodayAvailable; @@ -105,15 +95,11 @@ class PostService { DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 6)); - QuerySnapshot> response = await FirebaseFirestore - .instance - .collection("posts") - .where("user_id", isEqualTo: id) - .get(); + QuerySnapshot> response = + await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: id).get(); - List?> postList = response.docs - .map((DocumentSnapshot> doc) => doc.data()) - .toList(); + List?> postList = + response.docs.map((DocumentSnapshot> doc) => doc.data()).toList(); for (int i = 0; i < 7; i++) { DateTime date = sevenDaysAgo.add(Duration(days: i)); @@ -141,9 +127,7 @@ class PostService { Future addOrDeleteFavoritePost(String id) async { final idUser = MyApp.userViewModel.userCurrent.id; - var postRef = await FirebaseFirestore.instance - .collection("posts") - .doc(id); + var postRef = await FirebaseFirestore.instance.collection("posts").doc(id); var response = await postRef.get(); List likes = List.from(response.get("likes")); diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index b5d8545..f7498ce 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -22,8 +22,7 @@ class PostViewModel { List get bestPosts => _bestPosts; // Methods - addPost(String? description, String idMusic, File? image, - Tuple2? location) async { + addPost(String? description, String idMusic, File? image, Tuple2? location) async { await _postService.createPost(description, idMusic, image, location); } @@ -91,8 +90,7 @@ class PostViewModel { Future getAvailable() async { try { - return await _postService - .getAvailable(MyApp.userViewModel.userCurrent.id); + return await _postService.getAvailable(MyApp.userViewModel.userCurrent.id); } catch (e) { print(e); rethrow;