From 10b947cb4edf0e3a479c4be954d7dab39eb5f141 Mon Sep 17 00:00:00 2001 From: Emre Date: Tue, 1 Aug 2023 14:41:49 +0200 Subject: [PATCH] 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");