From 44b6477101a3282a80ac063cbe766e9efa6ed787 Mon Sep 17 00:00:00 2001 From: Emre Date: Mon, 31 Jul 2023 16:38:27 +0200 Subject: [PATCH] Starting get capsules :hammer: --- .../lib/components/post_component.dart | 5 +- .../lib/components/top_nav_bar_component.dart | 2 + Sources/justMUSIC/lib/model/Post.dart | 16 +++-- .../lib/model/mapper/PostMapper.dart | 26 ++++---- .../justMUSIC/lib/screens/feed_screen.dart | 66 +++++++++---------- .../justMUSIC/lib/services/AuthService.dart | 3 +- .../justMUSIC/lib/services/PostService.dart | 12 ++++ .../lib/view_model/PostViewModel.dart | 13 +++- 8 files changed, 86 insertions(+), 57 deletions(-) diff --git a/Sources/justMUSIC/lib/components/post_component.dart b/Sources/justMUSIC/lib/components/post_component.dart index 7d47b8d..d5e0f9e 100644 --- a/Sources/justMUSIC/lib/components/post_component.dart +++ b/Sources/justMUSIC/lib/components/post_component.dart @@ -7,9 +7,12 @@ import 'package:gradient_borders/box_borders/gradient_box_border.dart'; import 'package:text_scroll/text_scroll.dart'; import 'package:zoom_tap_animation/zoom_tap_animation.dart'; +import '../model/Post.dart'; + class PostComponent extends StatefulWidget { final VoidCallback? callback; - PostComponent({Key? key, required this.callback}) : super(key: key); + final Post post; + PostComponent({Key? key, required this.callback, required this.post}) : super(key: key); @override State createState() => _PostComponentState(); diff --git a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart index c7e872d..4d4bc92 100644 --- a/Sources/justMUSIC/lib/components/top_nav_bar_component.dart +++ b/Sources/justMUSIC/lib/components/top_nav_bar_component.dart @@ -7,6 +7,7 @@ import 'package:ionicons/ionicons.dart'; import 'package:zoom_tap_animation/zoom_tap_animation.dart'; import '../config/routes.dart'; +import '../main.dart'; import '../values/constants.dart'; class TopNavBarComponent extends StatefulWidget { @@ -23,6 +24,7 @@ class _TopNavBarComponentState extends State with TickerProv void actionSurBouton() { widget.callback(choice); + MyApp.postViewModel.getBestPosts(); } @override diff --git a/Sources/justMUSIC/lib/model/Post.dart b/Sources/justMUSIC/lib/model/Post.dart index 0c248dd..a023950 100644 --- a/Sources/justMUSIC/lib/model/Post.dart +++ b/Sources/justMUSIC/lib/model/Post.dart @@ -1,9 +1,11 @@ +import 'package:tuple/tuple.dart'; + class Post { - final int _id; - final int _idUser; + final String _id; + final String _idUser; String? _description; String _idMusic; - String _location; + Tuple2 _location; int _nblikes; String? _selfie; DateTime _date; @@ -13,9 +15,9 @@ class Post { this._nblikes, this._selfie, this._date); //Getters and setters - int get id => _id; + String get id => _id; - int get idUser => _idUser; + String get idUser => _idUser; String? get description => _description; @@ -29,9 +31,9 @@ class Post { _idMusic = value; } - String get location => _location; + Tuple2 get location => _location; - set location(String 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 6768403..11e5faf 100644 --- a/Sources/justMUSIC/lib/model/mapper/PostMapper.dart +++ b/Sources/justMUSIC/lib/model/mapper/PostMapper.dart @@ -1,15 +1,19 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:tuple/tuple.dart'; + import '../Post.dart'; class PostMapper { - static Map toFirebase(Post post) { - return { - "user_id": post.idUser, - "description": post.description ?? "", - "date": post.date, - "place": post.location ?? "", - "selfie": post.selfie ?? "", - "song_id": post.idMusic, - "likes": post.nblikes - }; + static Post toModel(DocumentSnapshot> snapshot) { + 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()); } -} \ No newline at end of file +} diff --git a/Sources/justMUSIC/lib/screens/feed_screen.dart b/Sources/justMUSIC/lib/screens/feed_screen.dart index 00b6680..216dae6 100644 --- a/Sources/justMUSIC/lib/screens/feed_screen.dart +++ b/Sources/justMUSIC/lib/screens/feed_screen.dart @@ -8,6 +8,8 @@ import 'package:google_fonts/google_fonts.dart'; import '../components/comment_component.dart'; import '../components/post_component.dart'; import '../components/top_nav_bar_component.dart'; +import '../main.dart'; +import '../model/Post.dart'; import '../values/constants.dart'; class FeedScreen extends StatefulWidget { @@ -20,27 +22,15 @@ class FeedScreen extends StatefulWidget { class _FeedScreenState extends State with TickerProviderStateMixin { late AnimationController animationController; late Animation animation; - late List friendFeed; - late List discoveryFeed; - late List displayFeed; + late List friendFeed; + late List discoveryFeed; + late List displayFeed; @override void initState() { super.initState(); - friendFeed = [ - PostComponent( - callback: openDetailPost, - ), - PostComponent( - callback: openDetailPost, - ), - PostComponent( - callback: openDetailPost, - ), - ]; - discoveryFeed = [ - PostComponent(callback: openDetailPost), - ]; + friendFeed = []; + discoveryFeed = MyApp.postViewModel.bestPosts; displayFeed = friendFeed; animationController = AnimationController( vsync: this, @@ -140,13 +130,13 @@ class _FeedScreenState extends State with TickerProviderStateMixin { color: Colors.white, fontWeight: FontWeight.w600), children: [ - TextSpan( - text: " commentaires", - style: GoogleFonts.plusJakartaSans( - color: Colors.white, - fontWeight: FontWeight.w300), - ) - ])), + TextSpan( + text: " commentaires", + style: GoogleFonts.plusJakartaSans( + color: Colors.white, + fontWeight: FontWeight.w300), + ) + ])), ), SizedBox(height: 20), CommentComponent(), @@ -161,7 +151,10 @@ class _FeedScreenState extends State with TickerProviderStateMixin { ), Padding( padding: EdgeInsets.only( - bottom: MediaQuery.of(context).viewInsets.bottom), + bottom: MediaQuery + .of(context) + .viewInsets + .bottom), child: Container( height: 70, width: double.infinity, @@ -213,7 +206,7 @@ class _FeedScreenState extends State with TickerProviderStateMixin { fillColor: bgModal, filled: true, focusColor: - Color.fromRGBO(255, 255, 255, 0.30), + Color.fromRGBO(255, 255, 255, 0.30), enabledBorder: OutlineInputBorder( borderSide: BorderSide( width: 1, color: grayText), @@ -263,9 +256,12 @@ class _FeedScreenState extends State with TickerProviderStateMixin { child: Padding( padding: EdgeInsets.only(top: 100.h), child: SingleChildScrollView( - child: Wrap( - runSpacing: 60, - children: displayFeed, + child: ListView.builder( + itemBuilder: (BuildContext context, + int index) { + return PostComponent(callback: openDetailPost,); + }, + ), )), ), @@ -279,12 +275,12 @@ class _FeedScreenState extends State with TickerProviderStateMixin { height: 240.h, decoration: BoxDecoration( gradient: LinearGradient(begin: Alignment.topRight, stops: [ - 0.3, - 1 - ], colors: [ - bgColor.withOpacity(0.9), - bgColor.withOpacity(0) - ])), + 0.3, + 1 + ], colors: [ + bgColor.withOpacity(0.9), + bgColor.withOpacity(0) + ])), ), ), Align( diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 7b289cf..2236b3f 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -19,7 +19,8 @@ class AuthService { "unique_id": uniqueId, "picture": "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/defaultImage.png?alt=media&token=cff5ae0a-e29e-4845-91f7-817597962f6b", - "friends": [] + "friends": [], + "comments": [] }; MyApp.db diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index e46d45e..89f9efc 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -1,5 +1,7 @@ +import 'dart:convert'; import 'dart:io'; +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:tuple/tuple.dart'; import 'package:firebase_storage/firebase_storage.dart'; @@ -30,4 +32,14 @@ class PostService { deletePost() {} getPostsById(String id) {} + + Future>>> getPopularPosts( + {int limit = 10, int offset = 0}) async { + QuerySnapshot> response = await FirebaseFirestore.instance + .collection("posts") + .limit(limit) + .orderBy("likes").get(); + return response.docs; + + } } diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index b61d96a..fbacc1c 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 '../model/mapper/PostMapper.dart'; + class PostViewModel { List _postsFriends = []; List _bestPosts = []; @@ -31,8 +33,15 @@ class PostViewModel { throw new Error(); } - List getBestPosts() { - throw new Error(); + getBestPosts() async { + try { + var responseData = await _postService.getPopularPosts(); + + _bestPosts = + responseData.map((value) => PostMapper.toModel(value)).toList(); + } catch (e) { + print(e); + } } List getMoreBestPosts() {