1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00

Display the list of comments

This commit is contained in:
Pierre HUBERT 2019-05-16 14:52:22 +02:00
parent be53a73d9f
commit 28de22f427
8 changed files with 198 additions and 10 deletions

View File

@ -0,0 +1,21 @@
import 'package:comunic/models/comment.dart';
/// Comments helper
///
/// @author Pierre HUBERT
class CommentsHelper {
/// Turn an API entry into a [Comment] object
static Comment apiToComment(Map<String, dynamic> entry) {
return Comment(
id: entry["ID"],
userID: entry["userID"],
postID: entry["postID"],
timeSent: entry["time_sent"],
content: entry["content"],
imageURL: entry["img_url"],
likes: entry["likes"],
userLike: entry["userlike"],
);
}
}

View File

@ -1,6 +1,8 @@
import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/enums/post_kind.dart';
import 'package:comunic/enums/post_visibility_level.dart'; import 'package:comunic/enums/post_visibility_level.dart';
import 'package:comunic/enums/user_access_levels.dart'; import 'package:comunic/enums/user_access_levels.dart';
import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/lists/comments_list.dart';
import 'package:comunic/lists/posts_list.dart'; import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/post.dart'; import 'package:comunic/models/post.dart';
@ -56,6 +58,14 @@ class PostsHelper {
/// Turn an API entry into a [Post] object /// Turn an API entry into a [Post] object
Post _apiToPost(Map<String, dynamic> map) { Post _apiToPost(Map<String, dynamic> map) {
// Parse comments
CommentsList comments;
if (map["comments"] != null) {
comments = CommentsList();
map["comments"]
.forEach((v) => comments.add(CommentsHelper.apiToComment(v)));
}
return Post( return Post(
id: map["ID"], id: map["ID"],
userID: map["userID"], userID: map["userID"],
@ -77,6 +87,7 @@ class PostsHelper {
likes: map["likes"], likes: map["likes"],
userLikes: map["userlike"], userLikes: map["userlike"],
access: _APIUserAccessMap[map["user_access"]], access: _APIUserAccessMap[map["user_access"]],
comments: comments,
); );
} }
} }

View File

@ -0,0 +1,26 @@
import 'dart:collection';
import 'package:comunic/models/comment.dart';
/// Comments list
///
/// Contains the list of comments for a post
///
/// @author Pierre HUBERT
class CommentsList extends ListBase<Comment> {
List<Comment> _list = List();
int get length => _list.length;
set length(int l) => _list.length = l;
@override
Comment operator [](int index) => _list[index];
@override
void operator []=(int index, Comment value) => _list[index] = value;
/// Get the list of users in this comments, as a set
Set<int> get usersID => map((f) => f.userID).toSet();
}

View File

@ -31,6 +31,9 @@ class PostsList extends ListBase<Post> {
if(p.userPageID != null && p.userPageID > 0) if(p.userPageID != null && p.userPageID > 0)
set.add(p.userPageID); set.add(p.userPageID);
if(p.comments != null)
set.addAll(p.comments.usersID);
}); });
return set; return set;

39
lib/models/comment.dart Normal file
View File

@ -0,0 +1,39 @@
import 'package:meta/meta.dart';
/// Comments
///
/// Contains information about a single comment
///
/// @author Pierre HUBERT
class Comment {
final int id;
final int userID;
final int postID;
final int timeSent;
final String content;
final String imageURL;
final int likes;
final bool userLike;
Comment({
@required this.id,
@required this.userID,
@required this.postID,
@required this.timeSent,
@required this.content,
@required this.imageURL,
@required this.likes,
@required this.userLike,
}) : assert(id != null),
assert(userID != null),
assert(postID != null),
assert(timeSent != null),
assert(content != null),
assert(likes != null),
assert(userLike != null);
bool get hasContent => content != null && content.length > 0;
bool get hasImage => imageURL != null;
}

View File

@ -1,6 +1,7 @@
import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/enums/post_kind.dart';
import 'package:comunic/enums/post_visibility_level.dart'; import 'package:comunic/enums/post_visibility_level.dart';
import 'package:comunic/enums/user_access_levels.dart'; import 'package:comunic/enums/user_access_levels.dart';
import 'package:comunic/lists/comments_list.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
/// Single post information /// Single post information
@ -28,6 +29,7 @@ class Post {
int likes; int likes;
bool userLikes; bool userLikes;
final UserAccessLevels access; final UserAccessLevels access;
final CommentsList comments;
Post({ Post({
@required this.id, @required this.id,
@ -50,6 +52,7 @@ class Post {
@required this.likes, @required this.likes,
@required this.userLikes, @required this.userLikes,
@required this.access, @required this.access,
@required this.comments
}) : assert(id != null), }) : assert(id != null),
assert(userID != null), assert(userID != null),
assert(userPageID != 0 || groupID != 0), assert(userPageID != 0 || groupID != 0),
@ -63,4 +66,6 @@ class Post {
assert(access != null); assert(access != null);
bool get hasContent => content != null; bool get hasContent => content != null;
bool get hasComments => comments != null;
} }

View File

@ -0,0 +1,53 @@
import 'package:comunic/models/comment.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/network_image_widget.dart';
import 'package:flutter/material.dart';
/// Single comment tile
///
/// @author Pierre HUBERT
class CommentTile extends StatelessWidget {
final Comment comment;
final User user;
const CommentTile({Key key, this.comment, this.user})
: assert(comment != null),
assert(user != null),
super(key: key);
@override
Widget build(BuildContext context) {
return ListTile(
leading: AccountImageWidget(
user: user,
),
title: Text(user.displayName,),
subtitle: _buildCommentContent(),
);
}
Widget _buildCommentContent() {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
// Comment image
Container(
child: comment.hasImage
? NetworkImageWidget(
url: comment.imageURL,
allowFullScreen: true,
height: 100.0,
)
: null,
),
// Comment text
Container(
child: comment.hasContent ? Text(comment.content,) : null,
),
],
);
}
}

View File

@ -2,6 +2,7 @@ import 'package:comunic/enums/post_kind.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/post.dart'; import 'package:comunic/models/post.dart';
import 'package:comunic/models/user.dart'; import 'package:comunic/models/user.dart';
import 'package:comunic/ui/tiles/comment_tile.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/network_image_widget.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart';
import 'package:comunic/utils/date_utils.dart'; import 'package:comunic/utils/date_utils.dart';
@ -124,17 +125,23 @@ class PostTile extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Card( return Card(
elevation: 1.0, elevation: 1.0,
child: Padding( child: Column(
padding: const EdgeInsets.all(8.0), children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
_buildHeaderRow(),
_buildContentRow(),
_buildButtonsArea(),
],
),
),
// Main post column Container(
child: Column( child: post.hasComments ? _buildComments() : null,
children: <Widget>[ ),
_buildHeaderRow(), ],
_buildContentRow(),
_buildButtonsArea(),
],
),
), ),
); );
} }
@ -146,4 +153,27 @@ class PostTile extends StatelessWidget {
roundedEdges: false, roundedEdges: false,
); );
} }
/// Build the list of comments
Widget _buildComments() {
assert(post.hasComments);
final comments = List.generate(
post.comments.length,
(num) => CommentTile(
comment: post.comments[num],
user: usersInfo.getUser(post.comments[num].userID),
),
);
return Container(
color: Colors.grey[300],
child: Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
child: Column(
children: comments,
),
),
);
}
} }