From 10b947cb4edf0e3a479c4be954d7dab39eb5f141 Mon Sep 17 00:00:00 2001 From: Emre Date: Tue, 1 Aug 2023 14:41:49 +0200 Subject: [PATCH 1/8] Refresh User Current --- .../components/editable_post_component.dart | 2 +- .../lib/components/statistics_component.dart | 4 +- .../lib/components/top_nav_bar_component.dart | 1 + Sources/justMUSIC/lib/model/User.dart | 17 ++-- .../lib/model/mapper/UserMapper.dart | 5 +- .../lib/screens/add_friend_screen.dart | 85 +++++++++++-------- .../justMUSIC/lib/services/AuthService.dart | 12 ++- .../justMUSIC/lib/services/PostService.dart | 5 -- .../lib/view_model/UserViewModel.dart | 13 +++ 9 files changed, 81 insertions(+), 63 deletions(-) diff --git a/Sources/justMUSIC/lib/components/editable_post_component.dart b/Sources/justMUSIC/lib/components/editable_post_component.dart index e028916..d45a26d 100644 --- a/Sources/justMUSIC/lib/components/editable_post_component.dart +++ b/Sources/justMUSIC/lib/components/editable_post_component.dart @@ -65,7 +65,7 @@ class _EditablePostComponentState extends State with Tick try { final image = await ImagePicker().pickImage( source: source, - imageQuality: 10 + imageQuality: 20 ); if (image == null) return; final imageTemp = File(image.path); diff --git a/Sources/justMUSIC/lib/components/statistics_component.dart b/Sources/justMUSIC/lib/components/statistics_component.dart index 6324318..a3707cd 100644 --- a/Sources/justMUSIC/lib/components/statistics_component.dart +++ b/Sources/justMUSIC/lib/components/statistics_component.dart @@ -20,7 +20,7 @@ class StatisticsComponent extends StatelessWidget { child: Column( children: [ AutoSizeText( - user.followed.toString(), + user.followed.length.toString(), style: GoogleFonts.plusJakartaSans( color: Colors.white, fontWeight: FontWeight.bold, @@ -40,7 +40,7 @@ class StatisticsComponent extends StatelessWidget { mainAxisSize: MainAxisSize.min, children: [ AutoSizeText( - user.followers.toString(), + user.followers.length.toString(), style: GoogleFonts.plusJakartaSans( color: Colors.white, fontWeight: FontWeight.bold, diff --git a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart index fbc25d9..688a762 100644 --- a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart +++ b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart @@ -237,6 +237,7 @@ class _TopNavBarComponentState extends State with TickerProv flex: 1, child: GestureDetector( onTap: () async { + await MyApp.userViewModel.updateUserCurrent(); bool returnFromOtherPage = await Navigator.of(context).push(routeProfile()); if (returnFromOtherPage == true) { checkAvailable(); diff --git a/Sources/justMUSIC/lib/model/User.dart b/Sources/justMUSIC/lib/model/User.dart index 6ddd1da..def349a 100644 --- a/Sources/justMUSIC/lib/model/User.dart +++ b/Sources/justMUSIC/lib/model/User.dart @@ -5,14 +5,13 @@ class User { String _country; String _mail; String _pp; - int _followers; + List _followers; int _capsules; - int _followed; - List friends = []; + List _followed; // Constructor - User(this._id, this._pseudo, this._uniquePseudo, this._country, this._mail, this._pp, - this._followers, this._capsules, this._followed, this.friends); + User(this._id, this._pseudo, this._uniquePseudo, this._country, this._mail, + this._pp, this._followers, this._capsules, this._followed); //Getters and setters String get id => _id; @@ -53,15 +52,15 @@ class User { _capsules = value; } - int get followed => _followed; + List get followed => _followed; - set followed(int value) { + set followed(List value) { _followed = value; } - int get followers => _followers; + List get followers => _followers; - set followers(int value) { + set followers(List value) { _followers = value; } } diff --git a/Sources/justMUSIC/lib/model/mapper/UserMapper.dart b/Sources/justMUSIC/lib/model/mapper/UserMapper.dart index eba3d57..3e6f1c0 100644 --- a/Sources/justMUSIC/lib/model/mapper/UserMapper.dart +++ b/Sources/justMUSIC/lib/model/mapper/UserMapper.dart @@ -12,10 +12,9 @@ class UserMapper { data?["country"] ?? "", data?["mail"], data?["picture"], - data?["followers"] ?? 0, + List.from(data?["followers"] as List), data?["nbCapsules"] ?? 0, - data?["followed"] ?? 0, - data?['friends'] is List ? List.from(data?['friends']) : []); + List.from(data?["followed"] as List)); } /* static Map toFirebase(User user) { diff --git a/Sources/justMUSIC/lib/screens/add_friend_screen.dart b/Sources/justMUSIC/lib/screens/add_friend_screen.dart index 298caf6..7c5c9ae 100644 --- a/Sources/justMUSIC/lib/screens/add_friend_screen.dart +++ b/Sources/justMUSIC/lib/screens/add_friend_screen.dart @@ -25,9 +25,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2UYSIpWSnX4gJhZJzaN4q.jpg?alt=media&token=39baf86a-4d19-4534-b777-1a4feca67359", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -35,9 +35,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -45,9 +45,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -55,9 +55,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -65,9 +65,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -75,9 +75,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -85,9 +85,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -95,9 +95,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -105,9 +105,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -115,9 +115,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -125,9 +125,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), User( "&", "_pseudo", @@ -135,9 +135,9 @@ class _AddFriendScreenState extends State { "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - 1, + [], 12, - 23, []), + []), ]; Future resetFullScreen() async { @@ -162,8 +162,10 @@ class _AddFriendScreenState extends State { Align( child: Text( "Ajouter des amis", - style: - GoogleFonts.plusJakartaSans(color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold), + style: GoogleFonts.plusJakartaSans( + color: Colors.white, + fontSize: 14, + fontWeight: FontWeight.bold), ), ), GestureDetector( @@ -179,7 +181,8 @@ class _AddFriendScreenState extends State { height: 30, width: 30, child: const Image( - image: AssetImage("assets/images/return_icon.png"), + image: + AssetImage("assets/images/return_icon.png"), height: 8, ), ))), @@ -193,7 +196,8 @@ class _AddFriendScreenState extends State { child: Column( children: [ Padding( - padding: const EdgeInsets.only(bottom: 20, left: 20, right: 20, top: 20), + padding: const EdgeInsets.only( + bottom: 20, left: 20, right: 20, top: 20), child: SizedBox( height: 40, child: TextField( @@ -216,31 +220,40 @@ class _AddFriendScreenState extends State { ), focusedBorder: const OutlineInputBorder( borderSide: BorderSide(width: 1, color: grayColor), - borderRadius: BorderRadius.all(Radius.circular(10))), - contentPadding: - const EdgeInsets.only(top: 0, bottom: 0, left: defaultPadding, right: defaultPadding), + borderRadius: + BorderRadius.all(Radius.circular(10))), + contentPadding: const EdgeInsets.only( + top: 0, + bottom: 0, + left: defaultPadding, + right: defaultPadding), fillColor: searchBarColor, filled: true, focusColor: grayText, enabledBorder: const OutlineInputBorder( borderSide: BorderSide(width: 1, color: grayColor), - borderRadius: BorderRadius.all(Radius.circular(10))), + borderRadius: + BorderRadius.all(Radius.circular(10))), hintText: 'Chercher un ami', - hintStyle: GoogleFonts.plusJakartaSans(color: grayHint)), + hintStyle: + GoogleFonts.plusJakartaSans(color: grayHint)), ), ), ), Flexible( child: ScrollConfiguration( - behavior: const ScrollBehavior().copyWith(scrollbars: true), + behavior: + const ScrollBehavior().copyWith(scrollbars: true), child: ListView.builder( - physics: const BouncingScrollPhysics(decelerationRate: ScrollDecelerationRate.fast), + physics: const BouncingScrollPhysics( + decelerationRate: ScrollDecelerationRate.fast), itemCount: fakeList.length, itemBuilder: (BuildContext context, int index) { return InkWell( child: Padding( padding: EdgeInsets.fromLTRB(20, 0, 20, 20), - child: ProfileListComponent(user: fakeList[index])), + child: ProfileListComponent( + user: fakeList[index])), ); }, ))) diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index ed3ad4a..9ca8cb0 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -17,13 +17,11 @@ class AuthService { "pseudo": pseudo, "phone_number": "", "unique_id": uniqueId, - "followed": 0, + "followed": [], "nbCapsules": 0, - "followers": 0, + "followers": [], "picture": - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc", - "friends": [], - "comments": [] + "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc" }; MyApp.db @@ -52,10 +50,10 @@ class AuthService { final QuerySnapshot querySnapshot = await usersCollection.where('pseudo', isEqualTo: pseudo).get(); - for (final doc in querySnapshot.docs) { + querySnapshot.docs.forEach((snapshot) { suffix++; uniqueId = '$pseudo#${suffix.toString().padLeft(4, '0')}'; - } + }); return uniqueId; } diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 7b3e76a..4d6ad74 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -1,8 +1,6 @@ -import 'dart:convert'; import 'dart:io'; import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:intl/intl.dart'; import 'package:tuple/tuple.dart'; import 'package:firebase_storage/firebase_storage.dart'; @@ -43,8 +41,6 @@ class PostService { deletePost() {} - getPostsById(String id) {} - Future>>> getPopularPosts({int limit = 10, int offset = 0}) async { DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); Timestamp twentyFourHoursAgoTimestamp = Timestamp.fromDate(twentyFourHoursAgo); @@ -68,7 +64,6 @@ class PostService { QuerySnapshot> response = await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: idUser).get(); - // Utiliser any() pour vérifier s'il y a au moins un document avec la date d'aujourd'hui 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; diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index f1afb2b..c98537c 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -42,6 +42,19 @@ class UserViewModel { return alphaNumericRegExp.hasMatch(input); } + updateUserCurrent() async { + try { + final user = await MyApp.db + .collection("users") + .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) + .get(); + User data = UserMapper.toModel(user, null); + _userCurrent = data; + } catch (e) { + print(e); + } + } + register(String pseudo, String password, String email) async { if (!_isAlphaNumeric(pseudo)) { throw ("Le pseudo doit contenir seulement des lettres et des chiffres"); -- 2.36.3 From 8e5e386d79f9b836648a1e04405f40fcfacdd492 Mon Sep 17 00:00:00 2001 From: Emre Date: Tue, 1 Aug 2023 15:49:10 +0200 Subject: [PATCH 2/8] Add getPostsFriends and getUsersByUniqueId --- Sources/justMUSIC/lib/model/User.dart | 9 +---- .../lib/model/mapper/UserMapper.dart | 4 +-- .../lib/screens/add_friend_screen.dart | 12 ------- .../justMUSIC/lib/services/AuthService.dart | 1 - .../justMUSIC/lib/services/PostService.dart | 36 +++++++++++++++---- .../justMUSIC/lib/services/UserService.dart | 22 +++++++----- .../lib/view_model/PostViewModel.dart | 20 +++++++++-- .../lib/view_model/UserViewModel.dart | 23 +++++++++--- 8 files changed, 82 insertions(+), 45 deletions(-) diff --git a/Sources/justMUSIC/lib/model/User.dart b/Sources/justMUSIC/lib/model/User.dart index def349a..2507da7 100644 --- a/Sources/justMUSIC/lib/model/User.dart +++ b/Sources/justMUSIC/lib/model/User.dart @@ -2,7 +2,6 @@ class User { final String _id; String _pseudo; String _uniquePseudo; - String _country; String _mail; String _pp; List _followers; @@ -10,7 +9,7 @@ class User { List _followed; // Constructor - User(this._id, this._pseudo, this._uniquePseudo, this._country, this._mail, + User(this._id, this._pseudo, this._uniquePseudo, this._mail, this._pp, this._followers, this._capsules, this._followed); //Getters and setters @@ -28,12 +27,6 @@ class User { _uniquePseudo = value; } - String get country => _country; - - set country(String value) { - _country = value; - } - String get mail => _mail; set mail(String value) { diff --git a/Sources/justMUSIC/lib/model/mapper/UserMapper.dart b/Sources/justMUSIC/lib/model/mapper/UserMapper.dart index 3e6f1c0..7d4c6c1 100644 --- a/Sources/justMUSIC/lib/model/mapper/UserMapper.dart +++ b/Sources/justMUSIC/lib/model/mapper/UserMapper.dart @@ -2,14 +2,12 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:justmusic/model/User.dart'; class UserMapper { - static User toModel(DocumentSnapshot> snapshot, - SnapshotOptions? options) { + static User toModel(DocumentSnapshot> snapshot) { final data = snapshot.data(); return User( snapshot.id, data?["pseudo"], data?["unique_id"], - data?["country"] ?? "", data?["mail"], data?["picture"], List.from(data?["followers"] as List), diff --git a/Sources/justMUSIC/lib/screens/add_friend_screen.dart b/Sources/justMUSIC/lib/screens/add_friend_screen.dart index 7c5c9ae..124f1fd 100644 --- a/Sources/justMUSIC/lib/screens/add_friend_screen.dart +++ b/Sources/justMUSIC/lib/screens/add_friend_screen.dart @@ -22,7 +22,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2UYSIpWSnX4gJhZJzaN4q.jpg?alt=media&token=39baf86a-4d19-4534-b777-1a4feca67359", [], @@ -32,7 +31,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -42,7 +40,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -52,7 +49,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -62,7 +58,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -72,7 +67,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -82,7 +76,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -92,7 +85,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -102,7 +94,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -112,7 +103,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -122,7 +112,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], @@ -132,7 +121,6 @@ class _AddFriendScreenState extends State { "&", "_pseudo", "_pseudo", - "_country", "_mail", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", [], diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 9ca8cb0..818fddf 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -15,7 +15,6 @@ class AuthService { final user = { "mail": email, "pseudo": pseudo, - "phone_number": "", "unique_id": uniqueId, "followed": [], "nbCapsules": 0, diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 4d6ad74..03f8aa4 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -7,7 +7,8 @@ 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, @@ -41,11 +42,14 @@ 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) @@ -58,15 +62,33 @@ class PostService { return filteredPosts; } + Future>>> getPostsFriends( + {int limit = 10, int offset = 0}) async { + QuerySnapshot> response = await FirebaseFirestore + .instance + .collection("posts") + .where("user_id", whereIn: MyApp.userViewModel.userCurrent.followed) + .orderBy("date", descending: true) + .limit(limit) + .get(); + + return response.docs; + } + 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; diff --git a/Sources/justMUSIC/lib/services/UserService.dart b/Sources/justMUSIC/lib/services/UserService.dart index d91e886..8cfb929 100644 --- a/Sources/justMUSIC/lib/services/UserService.dart +++ b/Sources/justMUSIC/lib/services/UserService.dart @@ -1,15 +1,21 @@ import 'package:cloud_firestore/cloud_firestore.dart'; -import '../model/User.dart'; import '../main.dart'; class UserService { - acceptFriend(User user, String idFriend) { - MyApp.db.collection("users").doc(user.id).update({ - "friends": FieldValue.arrayUnion([idFriend]) - }); - MyApp.db.collection("users").doc(idFriend).update({ - "friends": FieldValue.arrayUnion([user.id]) - }); + Future>>> getUsersByIdUnique( + String uniqueId) async { + QuerySnapshot> response = await FirebaseFirestore + .instance + .collection("users") + .where("unique_id", arrayContains: uniqueId) + .get(); + + var users = response.docs.where((doc) { + String id = doc["id"]; + return id != MyApp.userViewModel.userCurrent.id; + }).toList(); + + return users; } } diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index 2980018..c6d71c4 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -27,8 +27,24 @@ class PostViewModel { await _postService.createPost(description, idMusic, image, location); } - List getPostsFriends() { - throw new Error(); + Future> getPostsFriends() async { + try { + var responseData = await _postService.getPostsFriends(); + List ids = []; + var postsFutures = responseData.map((value) { + ids.add(value.data()["song_id"]); + return PostMapper.toModel(value); + }).toList(); + List musics = await MyApp.musicViewModel.getMusicsWithIds(ids); + for (int i = 0; i < _postsFriends.length; i++) { + _postsFriends[i].music = musics[i]; + } + return _postsFriends; + } catch (e) { + print(e); + _postsFriends = []; + return []; + } } List getMorePostsFriends() { diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index c98537c..e01def1 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -1,5 +1,6 @@ import 'package:firebase_auth/firebase_auth.dart' as firebase_auth; import 'package:justmusic/services/AuthService.dart'; +import 'package:justmusic/services/UserService.dart'; import '../model/User.dart'; import '../model/mapper/UserMapper.dart'; @@ -8,6 +9,7 @@ import '../main.dart'; class UserViewModel { late User _userCurrent; final AuthService _authService = AuthService(); + final UserService _userService = UserService(); User get userCurrent => _userCurrent; @@ -20,7 +22,7 @@ class UserViewModel { // Methods Future getUser(String id) async { final user = await MyApp.db.collection("users").doc(id).get(); - return UserMapper.toModel(user, null); + return UserMapper.toModel(user); } login(String pseudo, String password) async { @@ -30,7 +32,7 @@ class UserViewModel { .collection("users") .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) .get(); - User data = UserMapper.toModel(user, null); + User data = UserMapper.toModel(user); _userCurrent = data; } catch (e) { rethrow; @@ -48,7 +50,7 @@ class UserViewModel { .collection("users") .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) .get(); - User data = UserMapper.toModel(user, null); + User data = UserMapper.toModel(user); _userCurrent = data; } catch (e) { print(e); @@ -66,13 +68,26 @@ class UserViewModel { .collection("users") .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) .get(); - User data = UserMapper.toModel(user, null); + User data = UserMapper.toModel(user); _userCurrent = data; } catch (e) { rethrow; } } + Future> getUsersByUniqueId(String uniqueId) async { + try { + var response = await _userService.getUsersByIdUnique(uniqueId.toLowerCase()); + var users = response.map((value) { + return UserMapper.toModel(value); + }).toList(); + return users; + } catch(e) { + print(e); + return []; + } + } + logout() { _authService.signOut(); } -- 2.36.3 From 00219afacb075fd3ae1f7ed344b5d9c9e0109b31 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Tue, 1 Aug 2023 16:20:55 +0200 Subject: [PATCH 3/8] manage search friend --- .../components/profile_list_component.dart | 13 +- .../lib/screens/add_friend_screen.dart | 164 +++--------------- .../justMUSIC/lib/services/UserService.dart | 16 +- .../lib/view_model/UserViewModel.dart | 24 ++- 4 files changed, 55 insertions(+), 162 deletions(-) diff --git a/Sources/justMUSIC/lib/components/profile_list_component.dart b/Sources/justMUSIC/lib/components/profile_list_component.dart index 981dd9b..78bbf48 100644 --- a/Sources/justMUSIC/lib/components/profile_list_component.dart +++ b/Sources/justMUSIC/lib/components/profile_list_component.dart @@ -1,5 +1,6 @@ import 'package:flutter/Material.dart'; import 'package:google_fonts/google_fonts.dart'; +import '../main.dart'; import '../model/User.dart'; import '../values/constants.dart'; @@ -12,7 +13,17 @@ class ProfileListComponent extends StatefulWidget { } class _ProfileListComponentState extends State { - bool clicked = false; + 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) { diff --git a/Sources/justMUSIC/lib/screens/add_friend_screen.dart b/Sources/justMUSIC/lib/screens/add_friend_screen.dart index 124f1fd..2ac9e3f 100644 --- a/Sources/justMUSIC/lib/screens/add_friend_screen.dart +++ b/Sources/justMUSIC/lib/screens/add_friend_screen.dart @@ -4,6 +4,7 @@ import 'package:google_fonts/google_fonts.dart'; import 'package:justmusic/values/constants.dart'; import '../components/profile_list_component.dart'; +import '../main.dart'; import '../model/User.dart'; class AddFriendScreen extends StatefulWidget { @@ -17,116 +18,14 @@ class _AddFriendScreenState extends State { final ScrollController _scrollController = ScrollController(); final TextEditingController _textEditingController = TextEditingController(); - List fakeList = [ - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2UYSIpWSnX4gJhZJzaN4q.jpg?alt=media&token=39baf86a-4d19-4534-b777-1a4feca67359", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - User( - "&", - "_pseudo", - "_pseudo", - "_mail", - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/RUiGpZ8AzCQPqiVJKwuQeIqiC4B2bafPGRGLh2La72LkmQst.jpg?alt=media&token=ac1916f0-e08d-43bd-977a-2c2d94182609", - [], - 12, - []), - ]; + List _listUsers = []; + + Future updateList(String value) async { + var newlistUsers = await MyApp.userViewModel.getUsersByUniqueId(value); + setState(() { + _listUsers = newlistUsers; + }); + } Future resetFullScreen() async { await SystemChannels.platform.invokeMethod( @@ -150,10 +49,8 @@ class _AddFriendScreenState extends State { Align( child: Text( "Ajouter des amis", - style: GoogleFonts.plusJakartaSans( - color: Colors.white, - fontSize: 14, - fontWeight: FontWeight.bold), + style: + GoogleFonts.plusJakartaSans(color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold), ), ), GestureDetector( @@ -169,8 +66,7 @@ class _AddFriendScreenState extends State { height: 30, width: 30, child: const Image( - image: - AssetImage("assets/images/return_icon.png"), + image: AssetImage("assets/images/return_icon.png"), height: 8, ), ))), @@ -184,8 +80,7 @@ class _AddFriendScreenState extends State { child: Column( children: [ Padding( - padding: const EdgeInsets.only( - bottom: 20, left: 20, right: 20, top: 20), + padding: const EdgeInsets.only(bottom: 20, left: 20, right: 20, top: 20), child: SizedBox( height: 40, child: TextField( @@ -194,8 +89,8 @@ class _AddFriendScreenState extends State { keyboardAppearance: Brightness.dark, onEditingComplete: resetFullScreen, onSubmitted: (value) async { - if (_textEditingController.text.isEmpty) { - print("search"); + if (_textEditingController.text.isNotEmpty) { + updateList(value); } }, cursorColor: Colors.white, @@ -208,40 +103,31 @@ class _AddFriendScreenState extends State { ), focusedBorder: const OutlineInputBorder( borderSide: BorderSide(width: 1, color: grayColor), - borderRadius: - BorderRadius.all(Radius.circular(10))), - contentPadding: const EdgeInsets.only( - top: 0, - bottom: 0, - left: defaultPadding, - right: defaultPadding), + borderRadius: BorderRadius.all(Radius.circular(10))), + contentPadding: + const EdgeInsets.only(top: 0, bottom: 0, left: defaultPadding, right: defaultPadding), fillColor: searchBarColor, filled: true, focusColor: grayText, enabledBorder: const OutlineInputBorder( borderSide: BorderSide(width: 1, color: grayColor), - borderRadius: - BorderRadius.all(Radius.circular(10))), + borderRadius: BorderRadius.all(Radius.circular(10))), hintText: 'Chercher un ami', - hintStyle: - GoogleFonts.plusJakartaSans(color: grayHint)), + hintStyle: GoogleFonts.plusJakartaSans(color: grayHint)), ), ), ), Flexible( child: ScrollConfiguration( - behavior: - const ScrollBehavior().copyWith(scrollbars: true), + behavior: const ScrollBehavior().copyWith(scrollbars: true), child: ListView.builder( - physics: const BouncingScrollPhysics( - decelerationRate: ScrollDecelerationRate.fast), - itemCount: fakeList.length, + physics: const BouncingScrollPhysics(decelerationRate: ScrollDecelerationRate.fast), + itemCount: _listUsers.length, itemBuilder: (BuildContext context, int index) { return InkWell( child: Padding( - padding: EdgeInsets.fromLTRB(20, 0, 20, 20), - child: ProfileListComponent( - user: fakeList[index])), + padding: const EdgeInsets.fromLTRB(20, 0, 20, 20), + child: ProfileListComponent(user: _listUsers[index])), ); }, ))) diff --git a/Sources/justMUSIC/lib/services/UserService.dart b/Sources/justMUSIC/lib/services/UserService.dart index 8cfb929..35de6eb 100644 --- a/Sources/justMUSIC/lib/services/UserService.dart +++ b/Sources/justMUSIC/lib/services/UserService.dart @@ -3,18 +3,18 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import '../main.dart'; class UserService { - Future>>> getUsersByIdUnique( - String uniqueId) async { - QuerySnapshot> response = await FirebaseFirestore - .instance + Future>>> getUsersByIdUnique(String uniqueId) async { + QuerySnapshot> response = await FirebaseFirestore.instance .collection("users") - .where("unique_id", arrayContains: uniqueId) + .where("unique_id", isGreaterThanOrEqualTo: uniqueId) + .where("unique_id", isLessThanOrEqualTo: uniqueId + "zzzzzzzzzzzzzzzzzzzzzzzzzzzz") + .limit(20) .get(); - var users = response.docs.where((doc) { - String id = doc["id"]; - return id != MyApp.userViewModel.userCurrent.id; + String id = doc["unique_id"]; + return id != MyApp.userViewModel.userCurrent.uniquePseudo; }).toList(); + print("cc34" + users.toString()); return users; } diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index e01def1..5af8a14 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -28,10 +28,7 @@ class UserViewModel { login(String pseudo, String password) async { try { await _authService.login(pseudo, password); - final user = await MyApp.db - .collection("users") - .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) - .get(); + final user = await MyApp.db.collection("users").doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid).get(); User data = UserMapper.toModel(user); _userCurrent = data; } catch (e) { @@ -46,10 +43,7 @@ class UserViewModel { updateUserCurrent() async { try { - final user = await MyApp.db - .collection("users") - .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) - .get(); + final user = await MyApp.db.collection("users").doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid).get(); User data = UserMapper.toModel(user); _userCurrent = data; } catch (e) { @@ -64,10 +58,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(); + final user = await MyApp.db.collection("users").doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid).get(); User data = UserMapper.toModel(user); _userCurrent = data; } catch (e) { @@ -82,8 +73,7 @@ class UserViewModel { return UserMapper.toModel(value); }).toList(); return users; - } catch(e) { - print(e); + } catch (e) { return []; } } @@ -91,4 +81,10 @@ class UserViewModel { logout() { _authService.signOut(); } + + bool isFriend(String id) { + return _userCurrent.followed.contains(id); + } + + void addOrDeleteFriend(String id) {} } -- 2.36.3 From 1fe178c662036f953f0cf1c3a75386a2cef9e6b0 Mon Sep 17 00:00:00 2001 From: Emre Date: Tue, 1 Aug 2023 16:46:35 +0200 Subject: [PATCH 4/8] add function AddOrDeleteFriend --- .../components/profile_list_component.dart | 2 ++ .../lib/model/mapper/PostMapper.dart | 1 - .../justMUSIC/lib/services/UserService.dart | 19 ++++++++++++++++++- .../lib/view_model/UserViewModel.dart | 9 +++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Sources/justMUSIC/lib/components/profile_list_component.dart b/Sources/justMUSIC/lib/components/profile_list_component.dart index 78bbf48..8b77a1c 100644 --- a/Sources/justMUSIC/lib/components/profile_list_component.dart +++ b/Sources/justMUSIC/lib/components/profile_list_component.dart @@ -74,6 +74,7 @@ class _ProfileListComponentState extends State { child: InkWell( splashColor: Colors.white.withOpacity(0.3), onTap: () { + MyApp.userViewModel.addOrDeleteFriend(widget.user.id); setState(() { clicked = !clicked; }); @@ -93,6 +94,7 @@ class _ProfileListComponentState extends State { child: InkWell( splashColor: Colors.white.withOpacity(0.3), onTap: () { + MyApp.userViewModel.addOrDeleteFriend(widget.user.id); setState(() { clicked = !clicked; }); diff --git a/Sources/justMUSIC/lib/model/mapper/PostMapper.dart b/Sources/justMUSIC/lib/model/mapper/PostMapper.dart index 2d9b6e4..61ed7c4 100644 --- a/Sources/justMUSIC/lib/model/mapper/PostMapper.dart +++ b/Sources/justMUSIC/lib/model/mapper/PostMapper.dart @@ -2,7 +2,6 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:tuple/tuple.dart'; import '../../main.dart'; -import '../Music.dart'; import '../Post.dart'; import '../User.dart'; diff --git a/Sources/justMUSIC/lib/services/UserService.dart b/Sources/justMUSIC/lib/services/UserService.dart index 35de6eb..2e3c3e0 100644 --- a/Sources/justMUSIC/lib/services/UserService.dart +++ b/Sources/justMUSIC/lib/services/UserService.dart @@ -14,8 +14,25 @@ class UserService { String id = doc["unique_id"]; return id != MyApp.userViewModel.userCurrent.uniquePseudo; }).toList(); - print("cc34" + users.toString()); return users; } + + addOrDeleteFriend(String id) async { + + var userRef = MyApp.db.collection("users").doc(MyApp.userViewModel.userCurrent.id); + + if (MyApp.userViewModel.isFriend(id)) { + await MyApp.db.runTransaction((transaction) async { + transaction.update(userRef, {'followed': FieldValue.arrayRemove([id])}); + }); + MyApp.userViewModel.userCurrent.followed.remove(id); + } else { + await MyApp.db.runTransaction((transaction) async { + transaction.update(userRef, {'followed': 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 5af8a14..f2a5344 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -78,6 +78,15 @@ class UserViewModel { } } + AddOrDeleteFriend(String id) async { + try { + await _userService.addOrDeleteFriend(id); + } catch(e) { + print(e); + rethrow; + } + } + logout() { _authService.signOut(); } -- 2.36.3 From 5c8950a2ad6b4d5c8c3202e9311c89dd913c1d83 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Tue, 1 Aug 2023 17:08:32 +0200 Subject: [PATCH 5/8] 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) {} } -- 2.36.3 From 6e614e2100711cdc584503a4dc9750a04264f2f0 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Tue, 1 Aug 2023 18:06:50 +0200 Subject: [PATCH 6/8] add friend is live !!! :sparkles: --- .../justMUSIC/assets/animations/rocket.json | 1 + .../components/profile_list_component.dart | 22 ++++++++++ .../lib/components/top_nav_bar_component.dart | 26 +++++------ Sources/justMUSIC/lib/main.dart | 2 + .../lib/screens/launching_rocker_screen.dart | 43 +++++++++++++++++++ 5 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 Sources/justMUSIC/assets/animations/rocket.json create mode 100644 Sources/justMUSIC/lib/screens/launching_rocker_screen.dart diff --git a/Sources/justMUSIC/assets/animations/rocket.json b/Sources/justMUSIC/assets/animations/rocket.json new file mode 100644 index 0000000..e960151 --- /dev/null +++ b/Sources/justMUSIC/assets/animations/rocket.json @@ -0,0 +1 @@ +{"nm":"newScene","ddd":0,"h":506,"w":246,"meta":{"g":"LottieFiles Figma v38"},"layers":[{"ty":4,"nm":"","sr":1,"st":0,"op":55,"ip":0,"hd":false,"ddd":0,"bm":0,"hasMask":false,"ao":0,"ks":{"a":{"a":0,"k":[0,0]},"s":{"a":0,"k":[100,100]},"sk":{"a":0,"k":0},"p":{"a":0,"k":[0,0]},"r":{"a":0,"k":0},"sa":{"a":0,"k":0},"o":{"a":0,"k":100}},"ef":[],"shapes":[{"ty":"gr","bm":0,"hd":false,"nm":"","it":[{"ty":"sh","bm":0,"hd":false,"nm":"","d":1,"ks":{"a":0,"k":{"c":true,"i":[[0,0],[18.31699999999998,-14.117000000000019],[0,0],[4.924000000000007,-5.552999999999997],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[-2.176000000000016,8.12299999999999],[0,0],[-4.0330000000000155,15.050000000000011],[0,0],[0,0]],"o":[[-15.049000000000007,-4.031999999999982],[0,0],[-8.12299999999999,-2.1759999999999877],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[7.271000000000015,-1.488999999999976],[0,0],[21.383999999999986,-8.803999999999974],[0,0],[0,0],[0,0]],"v":[[229.562,323.522],[173.235,338.521],[161.993,335.509],[142.666,343.701],[136.075,351.133],[146.207,353.848],[156.351,356.566],[164.059,369.916],[171.766,383.266],[169.048,393.41],[166.333,403.542],[176.065,401.551],[192.823,388.908],[195.836,377.666],[236.989,336.385],[239.707,326.24],[229.562,323.522]]}}},{"ty":"sh","bm":0,"hd":false,"nm":"","d":1,"ks":{"a":0,"k":{"c":true,"i":[[0,0],[1.0389999999999873,-3.8759999999999764],[3.8770000000000095,1.038000000000011],[-1.0389999999999873,3.8770000000000095],[-3.8770000000000095,-1.0389999999999873]],"o":[[3.8760000000000048,1.038000000000011],[-1.0390000000000157,3.8770000000000095],[-3.8770000000000095,-1.0389999999999873],[1.038000000000011,-3.8770000000000095],[0,0]],"v":[[205.989,339.775],[211.127,348.674],[202.227,353.813],[197.089,344.913],[205.989,339.775]]}}},{"ty":"sh","bm":0,"hd":false,"nm":"","d":1,"ks":{"a":0,"k":{"c":true,"i":[[0,0],[0,0],[0,0],[-7.138000000000005,4.120999999999981],[0,0],[0,0]],"o":[[0,0],[-6.4269999999999925,3.7110000000000127],[0,0],[0,0],[0,0],[0,0]],"v":[[149.08,369.664],[144.63,372.233],[132.909,387.9],[152.338,385.583],[156.788,383.014],[149.08,369.664]]}}},{"ty":"fl","bm":0,"hd":false,"nm":"","c":{"a":0,"k":[1,1,1]},"r":1,"o":{"a":0,"k":100}},{"ty":"tr","a":{"a":0,"k":[0,0]},"s":{"a":0,"k":[100,100]},"sk":{"a":0,"k":0},"p":{"a":0,"k":[0,0]},"r":{"a":1,"k":[{"o":{"x":0.167,"y":0.167},"i":{"x":0.833,"y":0.833},"s":[90],"t":0},{"o":{"x":0.167,"y":0.167},"i":{"x":0.833,"y":0.833},"s":[90],"t":5},{"s":[0],"t":25}]},"sa":{"a":0,"k":0},"o":{"a":1,"k":[{"o":{"x":0.167,"y":0.167},"i":{"x":0.833,"y":0.833},"s":[0],"t":0},{"o":{"x":0.167,"y":0.167},"i":{"x":0.833,"y":0.833},"s":[0],"t":5},{"s":[100],"t":25}]}}]}],"ind":1}],"v":"5.7.0","fr":60,"op":55,"ip":0,"assets":[]} \ No newline at end of file diff --git a/Sources/justMUSIC/lib/components/profile_list_component.dart b/Sources/justMUSIC/lib/components/profile_list_component.dart index 66b02a3..ebb5363 100644 --- a/Sources/justMUSIC/lib/components/profile_list_component.dart +++ b/Sources/justMUSIC/lib/components/profile_list_component.dart @@ -1,4 +1,5 @@ import 'package:flutter/Material.dart'; +import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:google_fonts/google_fonts.dart'; import '../main.dart'; import '../model/User.dart'; @@ -82,6 +83,17 @@ class _ProfileListComponentState extends State { splashColor: Colors.white.withOpacity(0.3), onTap: () async { await MyApp.userViewModel.addOrDeleteFriend(widget.user.id); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + "Vous ne suivez plus ${widget.user.pseudo}", + style: GoogleFonts.plusJakartaSans( + color: Colors.white, fontWeight: FontWeight.w400, fontSize: 20.h), + ), + backgroundColor: Colors.red, + closeIconColor: Colors.white, + ), + ); setState(() {}); }, child: Container( @@ -100,6 +112,16 @@ class _ProfileListComponentState extends State { splashColor: Colors.white.withOpacity(0.3), onTap: () async { await MyApp.userViewModel.addOrDeleteFriend(widget.user.id); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + backgroundColor: primaryColor, + content: Text( + "Vous suivez à present ${widget.user.pseudo}", + style: GoogleFonts.plusJakartaSans( + color: Colors.white, fontWeight: FontWeight.w400, fontSize: 20.h), + ), + ), + ); setState(() {}); }, child: Container( diff --git a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart index 688a762..b990baa 100644 --- a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart +++ b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart @@ -138,10 +138,7 @@ class _TopNavBarComponentState extends State with TickerProv child: GestureDetector( behavior: HitTestBehavior.translucent, onTap: () async { - bool returnFromOtherPage = await Navigator.of(context).push(routeAddFriend()); - if (returnFromOtherPage == true) { - checkAvailable(); - } + Navigator.of(context).push(routeAddFriend()); }, child: const Icon( Icons.person_add_alt_1_rounded, @@ -159,7 +156,9 @@ class _TopNavBarComponentState extends State with TickerProv enableLongTapRepeatEvent: false, longTapRepeatDuration: const Duration(milliseconds: 100), begin: 1.0, - onTap: () {}, + onTap: () { + Navigator.of(context).pushNamed('/launchingRocket'); + }, end: 0.97, beginDuration: const Duration(milliseconds: 70), endDuration: const Duration(milliseconds: 100), @@ -238,19 +237,16 @@ class _TopNavBarComponentState extends State with TickerProv child: GestureDetector( onTap: () async { await MyApp.userViewModel.updateUserCurrent(); - bool returnFromOtherPage = await Navigator.of(context).push(routeProfile()); - if (returnFromOtherPage == true) { - checkAvailable(); - } + Navigator.of(context).push(routeProfile()); }, child: ClipOval( child: SizedBox.fromSize( - // Image radius - child: Image( - image: NetworkImage(MyApp.userViewModel.userCurrent.pp), - width: 30, - ), - ), + // Image radius + child: FadeInImage.assetNetwork( + placeholder: 'assets/images/loadingPlaceholder.gif', + image: MyApp.userViewModel.userCurrent.pp, + width: 30, + )), ), ), ) diff --git a/Sources/justMUSIC/lib/main.dart b/Sources/justMUSIC/lib/main.dart index 38fa806..b2416fd 100644 --- a/Sources/justMUSIC/lib/main.dart +++ b/Sources/justMUSIC/lib/main.dart @@ -8,6 +8,7 @@ import 'package:justmusic/screens/add_friend_screen.dart'; import 'package:justmusic/screens/explanations_screen.dart'; import 'package:justmusic/screens/feed_screen.dart'; import 'package:justmusic/screens/login_screen.dart'; +import 'package:justmusic/screens/launching_rocker_screen.dart'; import 'package:justmusic/screens/post_screen.dart'; import 'package:justmusic/screens/profile_screen.dart'; import 'package:justmusic/screens/registration_screen.dart'; @@ -53,6 +54,7 @@ class MyApp extends StatelessWidget { '/profile': (context) => const ProfileScreen(), '/explanation': (context) => const ExplanationsScreen(), '/addFriend': (context) => const AddFriendScreen(), + '/launchingRocket': (context) => const LaunchingRocketScreen(), }, debugShowCheckedModeBanner: false, theme: ThemeData( diff --git a/Sources/justMUSIC/lib/screens/launching_rocker_screen.dart b/Sources/justMUSIC/lib/screens/launching_rocker_screen.dart new file mode 100644 index 0000000..d68dbef --- /dev/null +++ b/Sources/justMUSIC/lib/screens/launching_rocker_screen.dart @@ -0,0 +1,43 @@ +import 'package:flutter/Material.dart'; +import 'package:lottie/lottie.dart'; + +class LaunchingRocketScreen extends StatefulWidget { + const LaunchingRocketScreen({super.key}); + + @override + State createState() => _LaunchingRocketScreenState(); +} + +class _LaunchingRocketScreenState extends State with TickerProviderStateMixin { + late final AnimationController _controller; + + @override + initState() { + _controller = AnimationController(vsync: this, duration: Duration(seconds: 3)); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Stack( + fit: StackFit.expand, + children: [ + Align( + alignment: Alignment.topCenter, + child: Lottie.asset( + 'assets/animations/rocket.json', + width: double.infinity, + frameRate: FrameRate(60), + fit: BoxFit.contain, + controller: _controller, + onLoaded: (composition) { + // Configure the AnimationController with the duration of the + // Lottie file and start the animation. + _controller.forward(); + }, + ), + ), + ], + ); + } +} -- 2.36.3 From 3771b40a49122ce7cec548809ff007f67041f3b7 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Tue, 1 Aug 2023 19:06:12 +0200 Subject: [PATCH 7/8] animation when post --- .../lib/components/top_nav_bar_component.dart | 2 +- .../lib/screens/launching_rocker_screen.dart | 107 +++++++++++++++--- .../justMUSIC/lib/screens/post_screen.dart | 22 ++-- 3 files changed, 97 insertions(+), 34 deletions(-) diff --git a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart index b990baa..ab0c499 100644 --- a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart +++ b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart @@ -157,7 +157,7 @@ class _TopNavBarComponentState extends State with TickerProv longTapRepeatDuration: const Duration(milliseconds: 100), begin: 1.0, onTap: () { - Navigator.of(context).pushNamed('/launchingRocket'); + checkAvailable(); }, end: 0.97, beginDuration: const Duration(milliseconds: 70), diff --git a/Sources/justMUSIC/lib/screens/launching_rocker_screen.dart b/Sources/justMUSIC/lib/screens/launching_rocker_screen.dart index d68dbef..b815342 100644 --- a/Sources/justMUSIC/lib/screens/launching_rocker_screen.dart +++ b/Sources/justMUSIC/lib/screens/launching_rocker_screen.dart @@ -1,6 +1,8 @@ import 'package:flutter/Material.dart'; import 'package:lottie/lottie.dart'; +import '../values/constants.dart'; + class LaunchingRocketScreen extends StatefulWidget { const LaunchingRocketScreen({super.key}); @@ -11,33 +13,102 @@ class LaunchingRocketScreen extends StatefulWidget { class _LaunchingRocketScreenState extends State with TickerProviderStateMixin { late final AnimationController _controller; + late AnimationController _controller2; + late Animation _animation; + @override initState() { _controller = AnimationController(vsync: this, duration: Duration(seconds: 3)); + + _controller2 = AnimationController( + vsync: this, + duration: Duration(milliseconds: 800), + ); + + final CurvedAnimation curve = CurvedAnimation(parent: _controller2, curve: Curves.easeIn); + + _animation = Tween( + begin: 0, + end: 1, + ).animate(curve); + + _controller2.addStatusListener((status) { + print("cccccccc"); + if (status == AnimationStatus.completed) { + Navigator.of(context).popUntil((route) => route.settings.name == '/feed'); + } + }); + super.initState(); } + @override + void dispose() { + _controller.dispose(); + _controller2.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { - return Stack( - fit: StackFit.expand, - children: [ - Align( - alignment: Alignment.topCenter, - child: Lottie.asset( - 'assets/animations/rocket.json', - width: double.infinity, - frameRate: FrameRate(60), - fit: BoxFit.contain, - controller: _controller, - onLoaded: (composition) { - // Configure the AnimationController with the duration of the - // Lottie file and start the animation. - _controller.forward(); - }, - ), + bool animationCompleted = false; + return Scaffold( + backgroundColor: primaryColor, + body: Container( + width: double.infinity, + height: double.infinity, + child: Stack( + children: [ + Lottie.asset( + 'assets/animations/rocket.json', + height: 600, + frameRate: FrameRate(60), + fit: BoxFit.contain, + controller: _controller, + onLoaded: (composition) { + _controller + ..duration = composition.duration + ..forward().whenComplete(() { + setState(() { + animationCompleted = true; + }); + _controller2.forward(); + }); + }, + ), + Center( + child: AnimatedBuilder( + animation: _animation, + builder: (context, child) { + double circlePosition = MediaQuery.of(context).size.height * _animation.value; + return CustomPaint( + painter: CirclePainter(circlePosition), + ); + }, + )), + ], ), - ], + ), ); } } + +class CirclePainter extends CustomPainter { + final double circlePosition; + + CirclePainter(this.circlePosition); + + @override + void paint(Canvas canvas, Size size) { + Paint paint = Paint()..color = Colors.white; + double radius = 50 * circlePosition; // Remplacez par le rayon souhaité de votre cercle + Offset center = Offset(size.width / 2, size.height / 2); + + canvas.drawCircle(center, radius, paint); + } + + @override + bool shouldRepaint(CustomPainter oldDelegate) { + return true; + } +} diff --git a/Sources/justMUSIC/lib/screens/post_screen.dart b/Sources/justMUSIC/lib/screens/post_screen.dart index ac94942..3f76761 100644 --- a/Sources/justMUSIC/lib/screens/post_screen.dart +++ b/Sources/justMUSIC/lib/screens/post_screen.dart @@ -19,8 +19,7 @@ class PostScreen extends StatefulWidget { State createState() => _PostScreenState(); } -class _PostScreenState extends State - with SingleTickerProviderStateMixin { +class _PostScreenState extends State with SingleTickerProviderStateMixin { final scrollController = ScrollController(); late AnimationController _controller; @@ -82,25 +81,22 @@ class _PostScreenState extends State isScrollControlled: true, context: context, shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.only( - topLeft: Radius.circular(20), topRight: Radius.circular(20))), + borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20))), builder: ((context) { return ClipRRect( - borderRadius: BorderRadius.only( - topLeft: Radius.circular(20), topRight: Radius.circular(20)), + borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)), child: SearchSongScreen(callback: _selectMusic)); }), ); } handleSubmit() async { - await MyApp.postViewModel.addPost( - description, (selectedMusic?.id)!, selectedImage, selectedCity); + MyApp.postViewModel.addPost(description, (selectedMusic?.id)!, selectedImage, selectedCity); quit(); } quit() { - Navigator.pop(context); + Navigator.pushNamed(context, '/launchingRocket'); } @override @@ -113,10 +109,7 @@ class _PostScreenState extends State preferredSize: Size(double.infinity, 80), child: SafeArea( child: Padding( - padding: EdgeInsets.only( - left: defaultPadding, - right: defaultPadding, - top: defaultPadding), + padding: EdgeInsets.only(left: defaultPadding, right: defaultPadding, top: defaultPadding), child: Row( children: [BackButtonComponent()], ), @@ -124,8 +117,7 @@ class _PostScreenState extends State ), ), body: Container( - padding: - const EdgeInsets.only(left: defaultPadding, right: defaultPadding), + padding: const EdgeInsets.only(left: defaultPadding, right: defaultPadding), width: double.infinity, height: double.infinity, decoration: const BoxDecoration( -- 2.36.3 From 51434401db2a288dd454e896a06eabc9a9e64df4 Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Tue, 1 Aug 2023 19:52:17 +0200 Subject: [PATCH 8/8] get Posts Friends done --- .../lib/components/top_nav_bar_component.dart | 5 +++-- Sources/justMUSIC/lib/screens/feed_screen.dart | 1 + Sources/justMUSIC/lib/services/PostService.dart | 12 +++++++++--- Sources/justMUSIC/lib/view_model/PostViewModel.dart | 2 ++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart index ab0c499..e981ad7 100644 --- a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart +++ b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart @@ -27,9 +27,10 @@ class _TopNavBarComponentState extends State with TickerProv final DateTime midnight = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day + 1); - void actionSurBouton() { + void actionSurBouton() async { widget.callback(choice); - MyApp.postViewModel.getBestPosts(); + await MyApp.postViewModel.getBestPosts(); + await MyApp.postViewModel.getPostsFriends(); } @override diff --git a/Sources/justMUSIC/lib/screens/feed_screen.dart b/Sources/justMUSIC/lib/screens/feed_screen.dart index f619441..7670d92 100644 --- a/Sources/justMUSIC/lib/screens/feed_screen.dart +++ b/Sources/justMUSIC/lib/screens/feed_screen.dart @@ -28,6 +28,7 @@ class _FeedScreenState extends State @override void initState() { super.initState(); + MyApp.postViewModel.getPostsFriends(); friendFeed = MyApp.postViewModel.postsFriends; MyApp.postViewModel.getBestPosts(); discoveryFeed = MyApp.postViewModel.bestPosts; diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 03f8aa4..9aff8e2 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -46,7 +46,7 @@ class PostService { {int limit = 10, int offset = 0}) async { DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); Timestamp twentyFourHoursAgoTimestamp = - Timestamp.fromDate(twentyFourHoursAgo); + Timestamp.fromDate(twentyFourHoursAgo); QuerySnapshot> response = await FirebaseFirestore .instance @@ -64,14 +64,20 @@ class PostService { Future>>> getPostsFriends( {int limit = 10, int offset = 0}) async { - QuerySnapshot> response = await FirebaseFirestore + var response = await FirebaseFirestore .instance .collection("posts") .where("user_id", whereIn: MyApp.userViewModel.userCurrent.followed) - .orderBy("date", descending: true) .limit(limit) .get(); + response.docs.sort((a, b) { + DateTime aDate = a.data()['date'].toDate(); + DateTime bDate = b.data()['date'].toDate(); + + return bDate.compareTo(aDate); + }); + return response.docs; } diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index c6d71c4..e74ab19 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -29,12 +29,14 @@ class PostViewModel { Future> getPostsFriends() async { try { + _postsFriends = []; var responseData = await _postService.getPostsFriends(); List ids = []; var postsFutures = responseData.map((value) { ids.add(value.data()["song_id"]); return PostMapper.toModel(value); }).toList(); + _postsFriends = await Future.wait(postsFutures); List musics = await MyApp.musicViewModel.getMusicsWithIds(ids); for (int i = 0; i < _postsFriends.length; i++) { _postsFriends[i].music = musics[i]; -- 2.36.3