Skip to content

Commit

Permalink
added deprecations and more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hjiangsu committed May 14, 2024
1 parent b32ee0d commit 7e69367
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/comment/models/comment_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ class CommentNode {
/// The replies to this comment
final List<CommentNode> replies;

/// Gets the depth/level of the comment in the tree
get depth => commentView == null ? 0 : commentView!.comment.path.split('.').length - 2;

/// Gets the total number of replies
get totalReplies => replies.length;

CommentNode({this.commentView, this.replies = const []});

/// Adds a reply to this comment node
/// There is a constraint where the comment [id] must be unique. If there exists a comment that has the same [id], we will replace it with the new comment.
void addReply(CommentNode reply) {
// Add the comment only if theres no other comment with the same id
int existingCommentNodeIndex = replies.indexWhere((node) => node.commentView?.comment.id == reply.commentView?.comment.id);
Expand All @@ -32,8 +35,6 @@ class CommentNode {

/// A static helper method to insert a comment node into the tree.
/// If the parent node is not found, the comment node is added to the root node.
///
/// There is a constraint where the comment [id] must be unique. If there exists a comment that has the same [id], we will replace it with the new comment.
static void insertCommentNode(CommentNode root, String parentId, CommentNode commentNode) {
CommentNode? parent = findCommentNode(root, parentId);

Expand Down
6 changes: 5 additions & 1 deletion lib/comment/utils/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Future<CommentView> deleteComment(int commentId, bool deleted) async {
Account? account = await fetchActiveProfileAccount();
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;

if (account?.jwt == null) throw Exception('User not logged in');
if (account?.jwt == null) throw Exception(AppLocalizations.of(GlobalContext.context)!.userNotLoggedIn);

CommentResponse commentResponse = await lemmy.run(DeleteComment(
auth: account!.jwt!,
Expand Down Expand Up @@ -159,6 +159,7 @@ List<CommentViewTree> buildCommentViewTree(List<CommentView> comments, {bool fla
return commentMap.values.where((commentView) => commentView.commentView!.comment.path.isEmpty || commentView.commentView!.comment.path == '0.${commentView.commentView!.comment.id}').toList();
}

@Deprecated('This function is used only for the legacy PostPage. Use CommentNode.insertCommentNode instead.')
List<CommentViewTree> insertNewComment(List<CommentViewTree> comments, CommentView commentView) {
List<String> parentIds = commentView.comment.path.split('.');
String commentTime = commentView.comment.published.toIso8601String();
Expand Down Expand Up @@ -186,6 +187,7 @@ List<CommentViewTree> insertNewComment(List<CommentViewTree> comments, CommentVi
return comments;
}

@Deprecated('This function is used only for the legacy PostPage. Use CommentNode.findCommentNode instead.')
CommentViewTree? findParentComment(int index, List<String> parentIds, String targetId, List<CommentViewTree> comments) {
for (CommentViewTree existing in comments) {
if (existing.commentView?.comment.id.toString() != parentIds[index]) {
Expand All @@ -202,6 +204,7 @@ CommentViewTree? findParentComment(int index, List<String> parentIds, String tar
return null;
}

@Deprecated('This function is used only for the legacy PostPage')
List<int> findCommentIndexesFromCommentViewTree(List<CommentViewTree> commentTrees, int commentId, [List<int>? indexes]) {
indexes ??= [];

Expand All @@ -225,6 +228,7 @@ List<int> findCommentIndexesFromCommentViewTree(List<CommentViewTree> commentTre
}

// Used for modifying the comment current comment tree so we don't have to refresh the whole thing
@Deprecated('This function is used only for the legacy PostPage')
bool updateModifiedComment(List<CommentViewTree> commentTrees, CommentView commentView) {
for (int i = 0; i < commentTrees.length; i++) {
if (commentTrees[i].commentView!.comment.id == commentView.comment.id) {
Expand Down

0 comments on commit 7e69367

Please sign in to comment.