diff --git a/lib/core/models/comment_view_tree.dart b/lib/core/models/comment_view_tree.dart index d7df0d81a..c07bb8f7c 100644 --- a/lib/core/models/comment_view_tree.dart +++ b/lib/core/models/comment_view_tree.dart @@ -3,9 +3,11 @@ import 'package:lemmy_api_client/v3.dart'; class CommentViewTree { CommentView? comment; List replies; + int level; // Level starts from 0, which is a direct reply to the post CommentViewTree({ this.comment, this.replies = const [], + this.level = 0, }); } diff --git a/lib/post/bloc/post_bloc.dart b/lib/post/bloc/post_bloc.dart index 71dd0d864..a6b90938b 100644 --- a/lib/post/bloc/post_bloc.dart +++ b/lib/post/bloc/post_bloc.dart @@ -1,4 +1,3 @@ - import 'package:bloc_concurrency/bloc_concurrency.dart'; import 'package:flutter/cupertino.dart'; import 'package:equatable/equatable.dart'; @@ -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 throttleDroppable(Duration duration) { return (events, mapper) => droppable().call(events.throttle(duration), mapper); @@ -75,10 +74,7 @@ class PostBloc extends Bloc { 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(); @@ -123,6 +119,7 @@ class PostBloc extends Bloc { page: 1, auth: account?.jwt, communityId: postView?.postView.post.communityId, + maxDepth: 8, postId: postView?.postView.post.id, sort: sortType, limit: commentLimit, @@ -136,16 +133,16 @@ class PostBloc extends Bloc { 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; @@ -165,10 +162,7 @@ class PostBloc extends Bloc { 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); @@ -191,6 +185,7 @@ class PostBloc extends Bloc { postId: state.postId, sort: sortType, limit: commentLimit, + maxDepth: 8, page: 1, )) .timeout(timeout, onTimeout: () { @@ -202,18 +197,18 @@ class PostBloc extends Bloc { 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 getCommentsResponse = await lemmy @@ -223,25 +218,26 @@ class PostBloc extends Bloc { 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 commentTree = buildCommentViewTree(getCommentsResponse); + // Combine all of the previous comments list + List fullCommentResponseList = List.from(state.commentResponseList)..addAll(getCommentsResponse); - // Append the new comments - List commentViewTree = List.from(state.comments); - commentViewTree.addAll(commentTree); + // Build the tree view from the flattened comments + List 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) { diff --git a/lib/post/bloc/post_state.dart b/lib/post/bloc/post_state.dart index f5acb551a..07eb8a0c9 100644 --- a/lib/post/bloc/post_state.dart +++ b/lib/post/bloc/post_state.dart @@ -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; @@ -28,6 +28,7 @@ class PostState extends Equatable { // Comment related data final List comments; + final List commentResponseList; // This is the raw list of comments final int commentPage; final int commentCount; final bool hasReachedCommentEnd; @@ -39,6 +40,7 @@ class PostState extends Equatable { int? postId, PostViewMedia? postView, List? comments, + List? commentResponseList, int? commentPage, int? commentCount, bool? hasReachedCommentEnd, @@ -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, diff --git a/lib/utils/comment.dart b/lib/utils/comment.dart index 6045045e6..64eb0fc08 100644 --- a/lib/utils/comment.dart +++ b/lib/utils/comment.dart @@ -73,6 +73,7 @@ List buildCommentViewTree(List comments) { commentMap[commentView.comment.path] = CommentViewTree( comment: commentView, replies: [], + level: commentView.comment.path.split('.').length - 2, ); }