Skip to content

Commit

Permalink
fixed issue where longer comment threads would not show up
Browse files Browse the repository at this point in the history
  • Loading branch information
hjiangsu committed Jul 5, 2023
1 parent ac99abb commit c46e66b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
2 changes: 2 additions & 0 deletions lib/core/models/comment_view_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import 'package:lemmy_api_client/v3.dart';
class CommentViewTree {
CommentView? comment;
List<CommentViewTree> replies;
int level; // Level starts from 0, which is a direct reply to the post

CommentViewTree({
this.comment,
this.replies = const [],
this.level = 0,
});
}
64 changes: 30 additions & 34 deletions lib/post/bloc/post_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:flutter/cupertino.dart';
import 'package:equatable/equatable.dart';
Expand All @@ -25,7 +24,7 @@ part 'post_state.dart';

const throttleDuration = Duration(seconds: 1);
const timeout = Duration(seconds: 10);
int commentLimit = 20;
int commentLimit = 50;

EventTransformer<E> throttleDroppable<E>(Duration duration) {
return (events, mapper) => droppable<E>().call(events.throttle(duration), mapper);
Expand Down Expand Up @@ -75,10 +74,7 @@ class PostBloc extends Bloc<PostEvent, PostState> {
var exception;

SharedPreferences prefs = await SharedPreferences.getInstance();
CommentSortType defaultSortType = CommentSortType.values.byName(prefs
.getString("setting_post_default_comment_sort_type")
?.toLowerCase() ??
DEFAULT_COMMENT_SORT_TYPE.name);
CommentSortType defaultSortType = CommentSortType.values.byName(prefs.getString("setting_post_default_comment_sort_type")?.toLowerCase() ?? DEFAULT_COMMENT_SORT_TYPE.name);

Account? account = await fetchActiveProfileAccount();

Expand Down Expand Up @@ -123,6 +119,7 @@ class PostBloc extends Bloc<PostEvent, PostState> {
page: 1,
auth: account?.jwt,
communityId: postView?.postView.post.communityId,
maxDepth: 8,
postId: postView?.postView.post.id,
sort: sortType,
limit: commentLimit,
Expand All @@ -136,16 +133,16 @@ class PostBloc extends Bloc<PostEvent, PostState> {

return emit(
state.copyWith(
status: PostStatus.success,
postId: postView?.postView.post.id,
postView: postView,
comments: commentTree,
commentPage: state.commentPage + 1,
commentCount: getCommentsResponse.length,
hasReachedCommentEnd: getCommentsResponse.isEmpty || getCommentsResponse.length < commentLimit,
communityId: postView?.postView.post.communityId,
sortType: sortType
),
status: PostStatus.success,
postId: postView?.postView.post.id,
postView: postView,
comments: commentTree,
commentResponseList: getCommentsResponse,
commentPage: state.commentPage + 1,
commentCount: getCommentsResponse.length,
hasReachedCommentEnd: getCommentsResponse.isEmpty || getCommentsResponse.length < commentLimit,
communityId: postView?.postView.post.communityId,
sortType: sortType),
);
} catch (e, s) {
exception = e;
Expand All @@ -165,10 +162,7 @@ class PostBloc extends Bloc<PostEvent, PostState> {
int attemptCount = 0;

SharedPreferences prefs = await SharedPreferences.getInstance();
CommentSortType defaultSortType = CommentSortType.values.byName(prefs
.getString("setting_post_default_comment_sort_type")
?.toLowerCase() ??
DEFAULT_COMMENT_SORT_TYPE.name);
CommentSortType defaultSortType = CommentSortType.values.byName(prefs.getString("setting_post_default_comment_sort_type")?.toLowerCase() ?? DEFAULT_COMMENT_SORT_TYPE.name);

CommentSortType sortType = event.sortType ?? (state.sortType ?? defaultSortType);

Expand All @@ -191,6 +185,7 @@ class PostBloc extends Bloc<PostEvent, PostState> {
postId: state.postId,
sort: sortType,
limit: commentLimit,
maxDepth: 8,
page: 1,
))
.timeout(timeout, onTimeout: () {
Expand All @@ -202,18 +197,18 @@ class PostBloc extends Bloc<PostEvent, PostState> {

return emit(
state.copyWith(
status: PostStatus.success,
comments: commentTree,
commentPage: 1,
commentCount: getCommentsResponse.length,
hasReachedCommentEnd: getCommentsResponse.isEmpty || getCommentsResponse.length < commentLimit,
sortType: sortType
),
status: PostStatus.success,
comments: commentTree,
commentResponseList: getCommentsResponse,
commentPage: 1,
commentCount: getCommentsResponse.length,
hasReachedCommentEnd: getCommentsResponse.isEmpty || getCommentsResponse.length < commentLimit,
sortType: sortType),
);
}

// Prevent duplicate requests if we're done fetching comments
if (state.commentCount >= state.postView!.postView.counts.comments) return;
if (state.commentCount >= state.postView!.postView.counts.comments || state.hasReachedCommentEnd) return;
emit(state.copyWith(status: PostStatus.refreshing));

List<CommentView> getCommentsResponse = await lemmy
Expand All @@ -223,25 +218,26 @@ class PostBloc extends Bloc<PostEvent, PostState> {
postId: state.postId,
sort: sortType,
limit: commentLimit,
maxDepth: 8,
page: state.commentPage,
))
.timeout(timeout, onTimeout: () {
throw Exception('Error: Timeout when attempting to fetch more comments');
});

// Build the tree view from the flattened comments
List<CommentViewTree> commentTree = buildCommentViewTree(getCommentsResponse);
// Combine all of the previous comments list
List<CommentView> fullCommentResponseList = List.from(state.commentResponseList)..addAll(getCommentsResponse);

// Append the new comments
List<CommentViewTree> commentViewTree = List.from(state.comments);
commentViewTree.addAll(commentTree);
// Build the tree view from the flattened comments
List<CommentViewTree> commentViewTree = buildCommentViewTree(fullCommentResponseList);

// We'll add in a edge case here to stop fetching comments after theres no more comments to be fetched
return emit(state.copyWith(
status: PostStatus.success,
comments: commentViewTree,
commentResponseList: fullCommentResponseList,
commentPage: state.commentPage + 1,
commentCount: state.commentCount + (getCommentsResponse.isEmpty ? commentLimit : getCommentsResponse.length),
commentCount: fullCommentResponseList.length,
hasReachedCommentEnd: getCommentsResponse.isEmpty || getCommentsResponse.length < commentLimit,
));
} catch (e, s) {
Expand Down
29 changes: 16 additions & 13 deletions lib/post/bloc/post_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ part of 'post_bloc.dart';
enum PostStatus { initial, loading, refreshing, success, empty, failure }

class PostState extends Equatable {
const PostState({
this.status = PostStatus.initial,
this.postId,
this.postView,
this.comments = const [],
this.commentPage = 1,
this.commentCount = 0,
this.communityId,
this.hasReachedCommentEnd = false,
this.errorMessage,
this.sortType,
this.sortTypeIcon
});
const PostState(
{this.status = PostStatus.initial,
this.postId,
this.postView,
this.comments = const [],
this.commentResponseList = const [],
this.commentPage = 1,
this.commentCount = 0,
this.communityId,
this.hasReachedCommentEnd = false,
this.errorMessage,
this.sortType,
this.sortTypeIcon});

final PostStatus status;

Expand All @@ -28,6 +28,7 @@ class PostState extends Equatable {

// Comment related data
final List<CommentViewTree> comments;
final List<CommentView> commentResponseList; // This is the raw list of comments
final int commentPage;
final int commentCount;
final bool hasReachedCommentEnd;
Expand All @@ -39,6 +40,7 @@ class PostState extends Equatable {
int? postId,
PostViewMedia? postView,
List<CommentViewTree>? comments,
List<CommentView>? commentResponseList,
int? commentPage,
int? commentCount,
bool? hasReachedCommentEnd,
Expand All @@ -52,6 +54,7 @@ class PostState extends Equatable {
postId: postId ?? this.postId,
postView: postView ?? this.postView,
comments: comments ?? this.comments,
commentResponseList: commentResponseList ?? this.commentResponseList,
commentPage: commentPage ?? this.commentPage,
commentCount: commentCount ?? this.commentCount,
hasReachedCommentEnd: hasReachedCommentEnd ?? this.hasReachedCommentEnd,
Expand Down
1 change: 1 addition & 0 deletions lib/utils/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ List<CommentViewTree> buildCommentViewTree(List<CommentView> comments) {
commentMap[commentView.comment.path] = CommentViewTree(
comment: commentView,
replies: [],
level: commentView.comment.path.split('.').length - 2,
);
}

Expand Down

0 comments on commit c46e66b

Please sign in to comment.