From 09dd731b971a38413ee8179a2b6382e9d53474d0 Mon Sep 17 00:00:00 2001 From: Abhishek saini Date: Fri, 26 Jan 2024 23:52:52 +0530 Subject: [PATCH] fix(post): fixed the profile picture issue for users liking and commenting on post --- lib/services/post_service.dart | 14 +++++ .../like_button_view_model.dart | 6 ++ .../feed/individual_post.dart | 57 +++++++++++-------- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/lib/services/post_service.dart b/lib/services/post_service.dart index 43ed5c3fd..ea0cc1a4c 100644 --- a/lib/services/post_service.dart +++ b/lib/services/post_service.dart @@ -1,8 +1,10 @@ import 'dart:async'; +import 'package:graphql_flutter/graphql_flutter.dart'; import 'package:talawa/locator.dart'; import 'package:talawa/models/organization/org_info.dart'; import 'package:talawa/models/post/post_model.dart'; +import 'package:talawa/models/user/user_info.dart'; import 'package:talawa/services/database_mutation_functions.dart'; import 'package:talawa/services/user_config.dart'; import 'package:talawa/utils/post_queries.dart'; @@ -102,6 +104,18 @@ class PostService { return result; } + Future fetchUserInfoById(String userId) async { + final QueryResult result1 = await databaseFunctions.gqlAuthQuery( + queries.fetchUserInfo, + variables: {'id': userId}, + ) as QueryResult; + final User userInfo = User.fromJson( + ((result1.data!['users'] as List)[0]) as Map, + fromOrg: true, + ); + return userInfo; + } + void _localAddLike(String postID) { _posts.forEach((post) { if (post.sId == postID) { diff --git a/lib/view_model/widgets_view_models/like_button_view_model.dart b/lib/view_model/widgets_view_models/like_button_view_model.dart index ee63f1e10..aa324f1ff 100644 --- a/lib/view_model/widgets_view_models/like_button_view_model.dart +++ b/lib/view_model/widgets_view_models/like_button_view_model.dart @@ -22,6 +22,7 @@ class LikeButtonViewModel extends BaseModel { // Local Variables for session caching bool _isLiked = false; late User _user; + late User _likedByUser; List _likedBy = []; late String _postID; @@ -32,6 +33,7 @@ class LikeButtonViewModel extends BaseModel { bool get isLiked => _isLiked; List get likedBy => _likedBy; int get likesCount => _likedBy.length; + User get likedByUser => _likedByUser; /// First function to initialize the ViewModel. /// @@ -64,6 +66,10 @@ class LikeButtonViewModel extends BaseModel { } } + Future fetchLikedByUser(String userId) async { + _likedByUser = await _postService.fetchUserInfoById(userId); + } + /// function to set isLiked boolean. /// /// **params**: diff --git a/lib/views/after_auth_screens/feed/individual_post.dart b/lib/views/after_auth_screens/feed/individual_post.dart index d2be8c346..e3bff40c9 100644 --- a/lib/views/after_auth_screens/feed/individual_post.dart +++ b/lib/views/after_auth_screens/feed/individual_post.dart @@ -3,6 +3,7 @@ import 'package:talawa/models/comment/comment_model.dart'; import 'package:talawa/models/post/post_model.dart'; import 'package:talawa/utils/app_localization.dart'; import 'package:talawa/view_model/widgets_view_models/comments_view_model.dart'; +import 'package:talawa/view_model/widgets_view_models/like_button_view_model.dart'; import 'package:talawa/views/base_view.dart'; import 'package:talawa/widgets/post_widget.dart'; @@ -192,7 +193,8 @@ class CommentTemplate extends StatelessWidget { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ - const CircleAvatar(), + CircleAvatar( + backgroundImage: NetworkImage(comment.creator?.image ?? "")), Expanded( child: Container( decoration: BoxDecoration( @@ -229,28 +231,33 @@ class CommentTemplate extends StatelessWidget { /// likedUserCircleAvatar returns a widget of the individual user liked the post. Widget likedUserCircleAvatar(LikedBy user) { - return const Padding( - padding: EdgeInsets.only(right: 10.0, bottom: 16.0), - child: Stack( - clipBehavior: Clip.none, - alignment: Alignment.center, - children: [ - CircleAvatar( - backgroundColor: Color(0xfff2f2f2), - radius: 20, - ), - Positioned( - top: 30, - right: 0, - bottom: 20, - left: 20, - child: Icon( - Icons.thumb_up, - color: Colors.blue, - size: 20, - ), - ), - ], - ), - ); + return BaseView( + onModelReady: (model) async { + await model.fetchLikedByUser(user.sId ?? ""); + }, + builder: (context, model, child) => Padding( + padding: const EdgeInsets.only(right: 10.0, bottom: 16.0), + child: Stack( + clipBehavior: Clip.none, + alignment: Alignment.center, + children: [ + CircleAvatar( + backgroundImage: NetworkImage(model.likedByUser.image ?? ""), + backgroundColor: const Color(0xfff2f2f2), + radius: 20, + ), + const Positioned( + top: 30, + right: 0, + bottom: 20, + left: 20, + child: Icon( + Icons.thumb_up, + color: Colors.blue, + size: 20, + ), + ), + ], + ), + )); }