toggleReplyLike method

Future<void> toggleReplyLike(
  1. String parentId,
  2. String replyId
)

Toggles the like status of a reply.

Updates a reply's like status and count within a parent comment's replies list.

parentId is the ID of the parent comment. replyId is the ID of the reply to toggle like for.

If the parent comment or reply is not found, this method does nothing.

Implementation

Future<void> toggleReplyLike(String parentId, String replyId) async {
  final Comment? updatedReply = await controller.toggleLike(replyId);
  if (updatedReply == null) return;

  final parentIndex = _comments.indexWhere((c) => c.id == parentId);
  if (parentIndex == -1) return;

  final replyIndex = _comments[parentIndex].replies.indexWhere((r) => r.id == replyId);
  if (replyIndex == -1) return;

  final updatedReplies = [..._comments[parentIndex].replies];
  updatedReplies[replyIndex] = updatedReply;

  _comments[parentIndex] = _comments[parentIndex].copyWith(replies: updatedReplies);
  notifyListeners();
}