From a46f6325ba3fdd03c4459dad8f8be1947aff8287 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 31 Jul 2023 20:34:54 +0200 Subject: [PATCH] Increment done and improvement on the model done :white_check_mark: --- .../lib/components/post_component.dart | 218 +++++++++++++----- Sources/justMUSIC/lib/model/Post.dart | 23 +- .../lib/model/mapper/PostMapper.dart | 24 +- .../justMUSIC/lib/services/AuthService.dart | 3 + .../justMUSIC/lib/services/PostService.dart | 11 + .../lib/view_model/PostViewModel.dart | 18 +- 6 files changed, 214 insertions(+), 83 deletions(-) diff --git a/Sources/justMUSIC/lib/components/post_component.dart b/Sources/justMUSIC/lib/components/post_component.dart index da0ff03..3d63947 100644 --- a/Sources/justMUSIC/lib/components/post_component.dart +++ b/Sources/justMUSIC/lib/components/post_component.dart @@ -16,7 +16,13 @@ class PostComponent extends StatefulWidget { final Function(int)? callback; final Post post; final int index; - PostComponent({Key? key, required this.callback, required this.post, required this.index}) : super(key: key); + + PostComponent( + {Key? key, + required this.callback, + required this.post, + required this.index}) + : super(key: key); @override State createState() => _PostComponentState(); @@ -27,11 +33,11 @@ class _PostComponentState extends State { DateTime today = DateTime.now(); Future fetchUserData() async { - return await MyApp.userViewModel.getUser(widget.post.idUser); + //return await MyApp.userViewModel.getUser(widget.post.idUser); } - Future fetchMusicData() async { - return await MyApp.musicViewModel.getMusic(widget.post.idMusic); + Future fetchMusicData() async { + //return await MyApp.musicViewModel.getMusic(widget.post.idMusic); } void switchChoice() { @@ -81,37 +87,58 @@ class _PostComponentState extends State { child: Padding( padding: const EdgeInsets.only(left: 10), child: Column( - crossAxisAlignment: CrossAxisAlignment.start, + crossAxisAlignment: + CrossAxisAlignment.start, children: [ Text( user.pseudo, style: GoogleFonts.plusJakartaSans( - color: Colors.white, fontWeight: FontWeight.w600), + color: Colors.white, + fontWeight: FontWeight.w600), ), - Text( - "${widget.post.location.item1}, ${widget.post.location.item2}", - style: GoogleFonts.plusJakartaSans( - color: Colors.white.withOpacity(0.4), - fontWeight: FontWeight.w300, + widget.post.location.item2 != null + ? Text( + "${widget.post.location?.item1}, ${widget.post.location?.item2}", + style: + GoogleFonts.plusJakartaSans( + color: Colors.white + .withOpacity(0.4), + fontWeight: + FontWeight.w300, + fontSize: 13), + ) + : Text( + "", + style: + GoogleFonts.plusJakartaSans( + color: Colors.white + .withOpacity(0.4), + fontWeight: + FontWeight.w300, fontSize: 13), ) ], ), ), ), - DateTime(today.year, today.month, today.day).isAtSameMomentAs( - DateTime(widget.post.date.year, widget.post.date.month, widget.post.date.day)) + DateTime(today.year, today.month, today.day) + .isAtSameMomentAs(DateTime( + widget.post.date.year, + widget.post.date.month, + widget.post.date.day)) ? Text( "Aujourd'hui, ${widget.post.date.hour}:${widget.post.date.minute}", style: GoogleFonts.plusJakartaSans( - color: Colors.white.withOpacity(0.4), + color: + Colors.white.withOpacity(0.4), fontWeight: FontWeight.w300, fontSize: 13), ) : Text( "${widget.post.date.day}/${widget.post.date.month}/${widget.post.date.year}-${widget.post.date.hour}:${widget.post.date.minute}", style: GoogleFonts.plusJakartaSans( - color: Colors.white.withOpacity(0.4), + color: + Colors.white.withOpacity(0.4), fontWeight: FontWeight.w300, fontSize: 13), ), @@ -125,7 +152,8 @@ class _PostComponentState extends State { } }, enableLongTapRepeatEvent: false, - longTapRepeatDuration: const Duration(milliseconds: 100), + longTapRepeatDuration: + const Duration(milliseconds: 100), begin: 1.0, end: 0.99, beginDuration: const Duration(milliseconds: 70), @@ -138,10 +166,13 @@ class _PostComponentState extends State { decoration: BoxDecoration( // add border border: const GradientBoxBorder( - gradient: LinearGradient(colors: [ - Colors.transparent, - Color(0xFF323232), - ], begin: Alignment.topCenter, end: Alignment.bottomCenter), + gradient: LinearGradient( + colors: [ + Colors.transparent, + Color(0xFF323232), + ], + begin: Alignment.topCenter, + end: Alignment.bottomCenter), width: 2.5, ), // set border radius @@ -154,7 +185,9 @@ class _PostComponentState extends State { alignment: Alignment.bottomCenter, children: [ Image( - image: NetworkImage(choice ? widget.post.selfie! : music.cover!), + image: NetworkImage(choice + ? widget.post.selfie! + : music.cover!), fit: BoxFit.cover, width: double.infinity, ), @@ -165,22 +198,35 @@ class _PostComponentState extends State { child: Padding( padding: EdgeInsets.all(12), child: Container( - constraints: BoxConstraints(maxWidth: 140, maxHeight: 140), + constraints: + BoxConstraints( + maxWidth: 140, + maxHeight: 140), width: 90.sp, height: 90.sp, decoration: BoxDecoration( color: Colors.white, // add border - border: Border.all(width: 3, color: Colors.white), + border: Border.all( + width: 3, + color: + Colors.white), // set border radius - borderRadius: BorderRadius.circular(15), + borderRadius: + BorderRadius + .circular(15), ), child: ClipRRect( - borderRadius: BorderRadius.circular(13), + borderRadius: + BorderRadius + .circular(13), // implement image child: Image( - image: - NetworkImage(choice ? music.cover! : widget.post.selfie!), + image: NetworkImage( + choice + ? music.cover! + : widget.post + .selfie!), fit: BoxFit.cover, ), ), @@ -202,13 +248,18 @@ class _PostComponentState extends State { child: TextScroll( music.artists.first.name!, style: GoogleFonts.plusJakartaSans( - height: 1, color: Colors.white, fontWeight: FontWeight.w600, fontSize: 26.h), + height: 1, + color: Colors.white, + fontWeight: FontWeight.w600, + fontSize: 26.h), mode: TextScrollMode.endless, pauseBetween: Duration(milliseconds: 500), - velocity: Velocity(pixelsPerSecond: Offset(20, 0)), + velocity: Velocity( + pixelsPerSecond: Offset(20, 0)), )), Padding( - padding: EdgeInsets.only(bottom: 10.h, right: 5.w, left: 5.w), + padding: EdgeInsets.only( + bottom: 10.h, right: 5.w, left: 5.w), child: ClipOval( child: Container( width: 5.h, @@ -229,8 +280,10 @@ class _PostComponentState extends State { fontWeight: FontWeight.w300, fontSize: 16.h), mode: TextScrollMode.endless, - velocity: Velocity(pixelsPerSecond: Offset(50, 20)), - pauseBetween: Duration(milliseconds: 500), + velocity: Velocity( + pixelsPerSecond: Offset(50, 20)), + pauseBetween: + Duration(milliseconds: 500), ), )), Container(width: 10), @@ -280,26 +333,45 @@ class _PostComponentState extends State { child: Padding( padding: const EdgeInsets.only(left: 10), child: Column( - crossAxisAlignment: CrossAxisAlignment.start, + crossAxisAlignment: + CrossAxisAlignment.start, children: [ Text( user.pseudo, style: GoogleFonts.plusJakartaSans( - color: Colors.white, fontWeight: FontWeight.w600), + color: Colors.white, + fontWeight: FontWeight.w600), ), - Text( - "${widget.post.location.item1}, ${widget.post.location.item2}", - style: GoogleFonts.plusJakartaSans( - color: Colors.white.withOpacity(0.4), - fontWeight: FontWeight.w300, + widget.post.location.item2 != null + ? Text( + "${widget.post.location?.item1}, ${widget.post.location?.item2}", + style: + GoogleFonts.plusJakartaSans( + color: Colors.white + .withOpacity(0.4), + fontWeight: + FontWeight.w300, + fontSize: 13), + ) + : Text( + "", + style: + GoogleFonts.plusJakartaSans( + color: Colors.white + .withOpacity(0.4), + fontWeight: + FontWeight.w300, fontSize: 13), ) ], ), ), ), - DateTime(today.year, today.month, today.day).isAtSameMomentAs( - DateTime(widget.post.date.year, widget.post.date.month, widget.post.date.day)) + DateTime(today.year, today.month, today.day) + .isAtSameMomentAs(DateTime( + widget.post.date.year, + widget.post.date.month, + widget.post.date.day)) ? Text( "Aujourd'hui, ${widget.post.date.hour}:${widget.post.date.minute}", style: GoogleFonts.plusJakartaSans( @@ -322,7 +394,8 @@ class _PostComponentState extends State { widget.callback!(widget.index); }, enableLongTapRepeatEvent: false, - longTapRepeatDuration: const Duration(milliseconds: 100), + longTapRepeatDuration: + const Duration(milliseconds: 100), begin: 1.0, end: 0.99, beginDuration: const Duration(milliseconds: 70), @@ -335,10 +408,13 @@ class _PostComponentState extends State { decoration: BoxDecoration( // add border border: const GradientBoxBorder( - gradient: LinearGradient(colors: [ - Colors.transparent, - Color(0xFF323232), - ], begin: Alignment.topCenter, end: Alignment.bottomCenter), + gradient: LinearGradient( + colors: [ + Colors.transparent, + Color(0xFF323232), + ], + begin: Alignment.topCenter, + end: Alignment.bottomCenter), width: 2.5, ), // set border radius @@ -356,7 +432,8 @@ class _PostComponentState extends State { width: double.infinity, ), Image( - image: AssetImage("assets/images/shadow_post.png"), + image: AssetImage( + "assets/images/shadow_post.png"), fit: BoxFit.fitHeight, width: double.infinity, ), @@ -365,7 +442,9 @@ class _PostComponentState extends State { child: AutoSizeText( '“${widget.post.description}”', style: GoogleFonts.plusJakartaSans( - color: Colors.white, fontWeight: FontWeight.w400, fontSize: 15.sp), + color: Colors.white, + fontWeight: FontWeight.w400, + fontSize: 15.sp), maxFontSize: 20, maxLines: 1, ), @@ -377,21 +456,31 @@ class _PostComponentState extends State { child: Padding( padding: EdgeInsets.all(12), child: Container( - constraints: BoxConstraints(maxWidth: 140, maxHeight: 140), + constraints: BoxConstraints( + maxWidth: 140, + maxHeight: 140), width: 90.sp, height: 90.sp, decoration: BoxDecoration( color: Colors.white, // add border - border: Border.all(width: 3, color: Colors.white), + border: Border.all( + width: 3, + color: Colors.white), // set border radius - borderRadius: BorderRadius.circular(15), + borderRadius: + BorderRadius.circular( + 15), ), child: ClipRRect( - borderRadius: BorderRadius.circular(13), + borderRadius: + BorderRadius.circular( + 13), // implement image child: Image( - image: NetworkImage(widget.post.selfie!), + image: NetworkImage( + widget + .post.selfie!), fit: BoxFit.cover, ), ), @@ -413,13 +502,18 @@ class _PostComponentState extends State { child: TextScroll( music.artists.first.name!, style: GoogleFonts.plusJakartaSans( - height: 1, color: Colors.white, fontWeight: FontWeight.w600, fontSize: 26.h), + height: 1, + color: Colors.white, + fontWeight: FontWeight.w600, + fontSize: 26.h), mode: TextScrollMode.endless, pauseBetween: Duration(milliseconds: 500), - velocity: Velocity(pixelsPerSecond: Offset(20, 0)), + velocity: Velocity( + pixelsPerSecond: Offset(20, 0)), )), Padding( - padding: EdgeInsets.only(bottom: 10.h, right: 5.w, left: 5.w), + padding: EdgeInsets.only( + bottom: 10.h, right: 5.w, left: 5.w), child: ClipOval( child: Container( width: 5.h, @@ -435,9 +529,13 @@ class _PostComponentState extends State { child: TextScroll( music.title!, style: GoogleFonts.plusJakartaSans( - height: 1, color: Colors.white, fontWeight: FontWeight.w300, fontSize: 16.h), + height: 1, + color: Colors.white, + fontWeight: FontWeight.w300, + fontSize: 16.h), mode: TextScrollMode.endless, - velocity: Velocity(pixelsPerSecond: Offset(50, 20)), + velocity: Velocity( + pixelsPerSecond: Offset(50, 20)), pauseBetween: Duration(milliseconds: 500), ), )), @@ -445,7 +543,9 @@ class _PostComponentState extends State { AutoSizeText( music.date.toString(), style: GoogleFonts.plusJakartaSans( - color: Colors.white.withOpacity(0.5), fontWeight: FontWeight.w300, fontSize: 16.h), + color: Colors.white.withOpacity(0.5), + fontWeight: FontWeight.w300, + fontSize: 16.h), textAlign: TextAlign.end, maxFontSize: 20, ), diff --git a/Sources/justMUSIC/lib/model/Post.dart b/Sources/justMUSIC/lib/model/Post.dart index a023950..224d956 100644 --- a/Sources/justMUSIC/lib/model/Post.dart +++ b/Sources/justMUSIC/lib/model/Post.dart @@ -1,23 +1,26 @@ import 'package:tuple/tuple.dart'; +import 'Music.dart'; +import 'User.dart'; + class Post { final String _id; - final String _idUser; + final User _user; String? _description; - String _idMusic; - Tuple2 _location; + late Music _music; + Tuple2 _location; int _nblikes; String? _selfie; DateTime _date; // Constructor - Post(this._id, this._idUser, this._description, this._idMusic, this._location, + Post(this._id, this._user, this._description, this._location, this._nblikes, this._selfie, this._date); //Getters and setters String get id => _id; - String get idUser => _idUser; + User get user => _user; String? get description => _description; @@ -25,15 +28,15 @@ class Post { _description = value; } - String get idMusic => _idMusic; + Music get music => _music; - set idMusic(String value) { - _idMusic = value; + set music(Music value) { + _music = value; } - Tuple2 get location => _location; + Tuple2 get location => _location; - set location(Tuple2 value) { + set location(Tuple2 value) { _location = value; } diff --git a/Sources/justMUSIC/lib/model/mapper/PostMapper.dart b/Sources/justMUSIC/lib/model/mapper/PostMapper.dart index 11e5faf..2d9b6e4 100644 --- a/Sources/justMUSIC/lib/model/mapper/PostMapper.dart +++ b/Sources/justMUSIC/lib/model/mapper/PostMapper.dart @@ -1,19 +1,23 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:tuple/tuple.dart'; +import '../../main.dart'; +import '../Music.dart'; import '../Post.dart'; +import '../User.dart'; class PostMapper { - static Post toModel(DocumentSnapshot> snapshot) { + static Future toModel ( + DocumentSnapshot> snapshot) async { final data = snapshot.data(); - return Post( - snapshot.id, - data?["user_id"], - data?["description"], - data?["song_id"], - Tuple2(data?["place"][0], data?["place"][1]), - data?["likes"], - data?["selfie"], - data?["date"].toDate()); + User? user = await MyApp.userViewModel.getUser(data?['user_id']); + return Post( + snapshot.id, + user!, + data?["description"], + Tuple2(data?["place"][0], data?["place"][1]), + data?["likes"], + data?["selfie"], + data?["date"].toDate()); } } diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 2236b3f..dd6679b 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -17,6 +17,9 @@ class AuthService { "pseudo": pseudo, "phone_number": "", "unique_id": uniqueId, + "followed": 0, + "nbCapsules": 0, + "followers": 0, "picture": "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/defaultImage.png?alt=media&token=cff5ae0a-e29e-4845-91f7-817597962f6b", "friends": [], diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 89f9efc..55921f5 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -21,6 +21,17 @@ class PostService { }; var postAdd = await MyApp.db.collection("posts").add(post); + + var userRef = MyApp.db.collection("users").doc(id); + + await MyApp.db.runTransaction((transaction) async { + var userSnapshot = await transaction.get(userRef); + if (userSnapshot.exists) { + int currentNbCapsules = userSnapshot.data()?['nbCapsules'] ?? 0; + transaction.update(userRef, {'nbCapsules': currentNbCapsules + 1}); + } + }); + if (image != null) { var imageRef = FirebaseStorage.instance.ref('$id${postAdd.id}.jpg'); await imageRef.putFile(image); diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index 2947a59..a674b81 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -4,6 +4,8 @@ import 'package:justmusic/model/Post.dart'; import 'package:justmusic/services/PostService.dart'; import 'package:tuple/tuple.dart'; +import '../main.dart'; +import '../model/Music.dart'; import '../model/mapper/PostMapper.dart'; class PostViewModel { @@ -20,7 +22,8 @@ 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); } @@ -35,9 +38,16 @@ class PostViewModel { getBestPosts() async { try { var responseData = await _postService.getPopularPosts(); - - _bestPosts = responseData.map((value) => PostMapper.toModel(value)).toList(); - print(_bestPosts.length); + List ids = []; + var postsFutures = responseData.map((value) { + ids.add(value.data()["song_id"]); + return PostMapper.toModel(value); + }).toList(); + _bestPosts = await Future.wait(postsFutures); + List musics = await MyApp.musicViewModel.getMusicsWithIds(ids); + for (int i = 0; i < _bestPosts.length; i++) { + _bestPosts[i].music = musics[i]; + } } catch (e) { print(e); }