From 5c8950a2ad6b4d5c8c3202e9311c89dd913c1d83 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Tue, 1 Aug 2023 17:08:32 +0200 Subject: [PATCH] add friend is live !!! :sparkles: --- .../components/profile_list_component.dart | 69 ++++++++++--------- .../lib/screens/add_friend_screen.dart | 4 ++ .../justMUSIC/lib/services/UserService.dart | 17 +++-- .../lib/view_model/UserViewModel.dart | 6 +- 4 files changed, 55 insertions(+), 41 deletions(-) diff --git a/Sources/justMUSIC/lib/components/profile_list_component.dart b/Sources/justMUSIC/lib/components/profile_list_component.dart index 8b77a1c..66b02a3 100644 --- a/Sources/justMUSIC/lib/components/profile_list_component.dart +++ b/Sources/justMUSIC/lib/components/profile_list_component.dart @@ -15,18 +15,9 @@ class ProfileListComponent extends StatefulWidget { class _ProfileListComponentState extends State { late bool clicked; - @override - void initState() { - if (MyApp.userViewModel.isFriend(widget.user.id)) { - clicked = true; - } else { - clicked = false; - } - super.initState(); - } - @override Widget build(BuildContext context) { + clicked = MyApp.userViewModel.isFriend(widget.user.id); return Container( padding: const EdgeInsets.only(bottom: 5), child: Row( @@ -47,22 +38,38 @@ class _ProfileListComponentState extends State { crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ - ScrollConfiguration( - behavior: ScrollBehavior().copyWith(scrollbars: false), - child: Text( - widget.user.uniquePseudo, - style: GoogleFonts.plusJakartaSans(fontSize: 16, color: Colors.white, fontWeight: FontWeight.w700), - overflow: TextOverflow.ellipsis, - maxLines: 1, - ), + Text( + widget.user.pseudo, + style: GoogleFonts.plusJakartaSans(fontSize: 16, color: Colors.white, fontWeight: FontWeight.w700), + overflow: TextOverflow.ellipsis, + maxLines: 1, ), - ScrollConfiguration( - behavior: ScrollBehavior().copyWith(scrollbars: false), - child: Text( - widget.user.pseudo, + Row( + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Text( + widget.user.uniquePseudo, overflow: TextOverflow.ellipsis, style: GoogleFonts.plusJakartaSans(color: Colors.grey, fontWeight: FontWeight.w400), - )) + ), + widget.user.followed.contains(MyApp.userViewModel.userCurrent.id) + ? Container( + padding: const EdgeInsets.all(2), + margin: const EdgeInsets.only(left: 10), + decoration: const BoxDecoration( + color: grayColor, + borderRadius: BorderRadius.all(Radius.circular(3)), + ), + child: Text( + "Vous suit", + overflow: TextOverflow.ellipsis, + style: GoogleFonts.plusJakartaSans( + color: Colors.grey.withOpacity(0.4), fontWeight: FontWeight.w700, fontSize: 12), + ), + ) + : Container(), + ], + ) ], ), ), @@ -73,11 +80,9 @@ class _ProfileListComponentState extends State { color: selectedButton, child: InkWell( splashColor: Colors.white.withOpacity(0.3), - onTap: () { - MyApp.userViewModel.addOrDeleteFriend(widget.user.id); - setState(() { - clicked = !clicked; - }); + onTap: () async { + await MyApp.userViewModel.addOrDeleteFriend(widget.user.id); + setState(() {}); }, child: Container( padding: EdgeInsets.fromLTRB(28, 7, 28, 7), @@ -93,11 +98,9 @@ class _ProfileListComponentState extends State { color: primaryColor, child: InkWell( splashColor: Colors.white.withOpacity(0.3), - onTap: () { - MyApp.userViewModel.addOrDeleteFriend(widget.user.id); - setState(() { - clicked = !clicked; - }); + onTap: () async { + await MyApp.userViewModel.addOrDeleteFriend(widget.user.id); + setState(() {}); }, child: Container( padding: EdgeInsets.fromLTRB(25, 7, 25, 7), diff --git a/Sources/justMUSIC/lib/screens/add_friend_screen.dart b/Sources/justMUSIC/lib/screens/add_friend_screen.dart index 2ac9e3f..246e514 100644 --- a/Sources/justMUSIC/lib/screens/add_friend_screen.dart +++ b/Sources/justMUSIC/lib/screens/add_friend_screen.dart @@ -91,6 +91,10 @@ class _AddFriendScreenState extends State { onSubmitted: (value) async { if (_textEditingController.text.isNotEmpty) { updateList(value); + } else { + setState(() { + _listUsers = []; + }); } }, cursorColor: Colors.white, diff --git a/Sources/justMUSIC/lib/services/UserService.dart b/Sources/justMUSIC/lib/services/UserService.dart index 2e3c3e0..0a66793 100644 --- a/Sources/justMUSIC/lib/services/UserService.dart +++ b/Sources/justMUSIC/lib/services/UserService.dart @@ -19,20 +19,29 @@ class UserService { } addOrDeleteFriend(String id) async { - var userRef = MyApp.db.collection("users").doc(MyApp.userViewModel.userCurrent.id); + var actionUserRef = MyApp.db.collection("users").doc(id); if (MyApp.userViewModel.isFriend(id)) { await MyApp.db.runTransaction((transaction) async { - transaction.update(userRef, {'followed': FieldValue.arrayRemove([id])}); + transaction.update(userRef, { + 'followed': FieldValue.arrayRemove([id]) + }); + transaction.update(actionUserRef, { + 'followers': FieldValue.arrayRemove([id]) + }); }); MyApp.userViewModel.userCurrent.followed.remove(id); } else { await MyApp.db.runTransaction((transaction) async { - transaction.update(userRef, {'followed': FieldValue.arrayUnion([id])}); + transaction.update(userRef, { + 'followed': FieldValue.arrayUnion([id]) + }); + transaction.update(actionUserRef, { + 'followers': FieldValue.arrayUnion([id]) + }); }); MyApp.userViewModel.userCurrent.followed.add(id); } } - } diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index f2a5344..1636212 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -78,10 +78,10 @@ class UserViewModel { } } - AddOrDeleteFriend(String id) async { + addOrDeleteFriend(String id) async { try { await _userService.addOrDeleteFriend(id); - } catch(e) { + } catch (e) { print(e); rethrow; } @@ -94,6 +94,4 @@ class UserViewModel { bool isFriend(String id) { return _userCurrent.followed.contains(id); } - - void addOrDeleteFriend(String id) {} }