Skip to content

Commit

Permalink
fix issue with loading more replies
Browse files Browse the repository at this point in the history
  • Loading branch information
hjiangsu committed May 31, 2024
1 parent 3d0689a commit 93b71eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
9 changes: 8 additions & 1 deletion lib/comment/models/comment_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ class CommentNode {

/// Gets the depth/level of the comment in the tree. A depth of 0 indicates a root comment.
/// The [commentView.comment.path] is a dot-separated string of comment ids starting from 0 (post). For example: `0.103315`
get depth => commentView == null ? 0 : commentView!.comment.path.split('.').length - 2;
int get depth {
if (commentView == null) return 0;

List<String> pathSegments = commentView!.comment.path.split('.');
int depth = pathSegments.length > 2 ? pathSegments.length - 2 : 0;

return depth;
}

/// Gets the total number of replies
get totalReplies => replies.length;
Expand Down
16 changes: 8 additions & 8 deletions lib/comment/utils/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:thunder/utils/global_context.dart';

// Optimistically updates a comment
CommentView optimisticallyVoteComment(CommentViewTree commentViewTree, int voteType) {
int newScore = commentViewTree.commentView!.counts.score;
int newUpvotes = commentViewTree.commentView!.counts.upvotes;
int newDownvotes = commentViewTree.commentView!.counts.downvotes;
int? existingVoteType = commentViewTree.commentView!.myVote;
CommentView optimisticallyVoteComment(CommentView commentView, int voteType) {
int newScore = commentView.counts.score;
int newUpvotes = commentView.counts.upvotes;
int newDownvotes = commentView.counts.downvotes;
int? existingVoteType = commentView.myVote;

switch (voteType) {
case -1:
Expand All @@ -39,9 +39,9 @@ CommentView optimisticallyVoteComment(CommentViewTree commentViewTree, int voteT
break;
}

return commentViewTree.commentView!.copyWith(
return commentView.copyWith(
myVote: voteType,
counts: commentViewTree.commentView!.counts.copyWith(
counts: commentView.counts.copyWith(
score: newScore,
upvotes: newUpvotes,
downvotes: newDownvotes,
Expand Down Expand Up @@ -118,7 +118,7 @@ CommentNode buildCommentTree(List<CommentView> comments, {bool flatten = false})

for (CommentView commentView in comments) {
List<String> commentPath = commentView.comment.path.split('.');
String parentId = commentPath[commentPath.length - 2];
String parentId = commentPath.length > 2 ? commentPath[commentPath.length - 2] : commentPath.first;

CommentNode commentNode = CommentNode(commentView: commentView, replies: []);
CommentNode.insertCommentNode(root, parentId, commentNode);
Expand Down
9 changes: 0 additions & 9 deletions lib/post/bloc/post_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,6 @@ class PostBloc extends Bloc<PostEvent, PostState> {
// Combine all of the previous comments list
List<CommentView> fullCommentResponseList = List.from(state.commentResponseMap.values)..addAll(getCommentsResponse.comments);

// Filter out any comments that are potentially duplicates (have the same [id])
List<CommentView> duplicates = groupBy(fullCommentResponseList, (commentView) => commentView.comment.id).values.where((list) => list.length > 1).map((list) => list.first).toList();

if (duplicates.isNotEmpty) {
for (CommentView duplicate in duplicates) {
fullCommentResponseList.remove(duplicate);
}
}

for (CommentView comment in getCommentsResponse.comments) {
state.commentResponseMap[comment.comment.id] = comment;
}
Expand Down

0 comments on commit 93b71eb

Please sign in to comment.