From 256bec0c91fc563428bfdfcb8a17768e22c171eb Mon Sep 17 00:00:00 2001 From: Micah Morrison Date: Wed, 29 May 2024 11:12:27 -0400 Subject: [PATCH] Fix optimistic voting --- lib/comment/utils/comment.dart | 17 ++++++++++++++++- lib/post/utils/post.dart | 28 +++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/comment/utils/comment.dart b/lib/comment/utils/comment.dart index c8e97dc0e..eee86ea30 100644 --- a/lib/comment/utils/comment.dart +++ b/lib/comment/utils/comment.dart @@ -11,26 +11,41 @@ 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; switch (voteType) { case -1: newScore--; + newDownvotes++; + if (existingVoteType == 1) newUpvotes--; break; case 1: newScore++; + newUpvotes++; + if (existingVoteType == -1) newDownvotes--; break; case 0: // Determine score from existing if (existingVoteType == -1) { newScore++; + newDownvotes--; } else if (existingVoteType == 1) { newScore--; + newUpvotes--; } break; } - return commentViewTree.commentView!.copyWith(myVote: voteType, counts: commentViewTree.commentView!.counts.copyWith(score: newScore)); + return commentViewTree.commentView!.copyWith( + myVote: voteType, + counts: commentViewTree.commentView!.counts.copyWith( + score: newScore, + upvotes: newUpvotes, + downvotes: newDownvotes, + ), + ); } /// Logic to vote on a comment diff --git a/lib/post/utils/post.dart b/lib/post/utils/post.dart index c91597352..59b66b241 100644 --- a/lib/post/utils/post.dart +++ b/lib/post/utils/post.dart @@ -95,26 +95,40 @@ Future deletePost(int postId, bool delete) async { // Optimistically updates a post. This changes the value of the post locally, without sending the network request PostView optimisticallyVotePost(PostViewMedia postViewMedia, int voteType) { int newScore = postViewMedia.postView.counts.score; - int? existingint = postViewMedia.postView.myVote; + int newUpvotes = postViewMedia.postView.counts.upvotes; + int newDownvotes = postViewMedia.postView.counts.downvotes; + int? existingVoteType = postViewMedia.postView.myVote; switch (voteType) { case -1: - existingint == 1 ? newScore -= 2 : newScore--; - break; + existingVoteType == 1 ? newScore -= 2 : newScore--; + newDownvotes++; + if (existingVoteType == 1) newUpvotes--; case 1: - existingint == -1 ? newScore += 2 : newScore++; + existingVoteType == -1 ? newScore += 2 : newScore++; + newUpvotes++; + if (existingVoteType == -1) newDownvotes--; break; case 0: // Determine score from existing - if (existingint == -1) { + if (existingVoteType == -1) { newScore++; - } else if (existingint == 1) { + newDownvotes--; + } else if (existingVoteType == 1) { newScore--; + newUpvotes--; } break; } - return postViewMedia.postView.copyWith(myVote: voteType, counts: postViewMedia.postView.counts.copyWith(score: newScore)); + return postViewMedia.postView.copyWith( + myVote: voteType, + counts: postViewMedia.postView.counts.copyWith( + score: newScore, + upvotes: newUpvotes, + downvotes: newDownvotes, + ), + ); } // Optimistically saves a post. This changes the value of the post locally, without sending the network request