diff --git a/lib/services/post_service.dart b/lib/services/post_service.dart index 6b6fcaa2f..5bb53aa5e 100644 --- a/lib/services/post_service.dart +++ b/lib/services/post_service.dart @@ -5,6 +5,7 @@ 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'; @@ -145,6 +146,20 @@ 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; + } + + /// Locally add like on a Post and updates it using updated Post Stream. /// /// **params**: @@ -152,6 +167,7 @@ class PostService { /// /// **returns**: /// None + 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 67b8013ce..d0072c501 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 @@ -23,6 +23,7 @@ class LikeButtonViewModel extends BaseModel { // Local Variables for session caching bool _isLiked = false; late User _user; + late User _likedByUser; List _likedBy = []; late String _postID; @@ -33,6 +34,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. /// @@ -65,6 +67,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 a8d12549e..7738d8d17 100644 --- a/lib/views/after_auth_screens/feed/individual_post.dart +++ b/lib/views/after_auth_screens/feed/individual_post.dart @@ -4,6 +4,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'; @@ -193,7 +194,8 @@ class CommentTemplate extends StatelessWidget { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ - const CircleAvatar(), + CircleAvatar( + backgroundImage: NetworkImage(comment.creator?.image ?? "")), Expanded( child: Container( decoration: BoxDecoration( @@ -230,28 +232,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, + ), + ), + ], + ), + )); }