Skip to content

Commit

Permalink
Fix issue where optimistic voting would not reflect upvotes/downvotes…
Browse files Browse the repository at this point in the history
… properly (#1392)
  • Loading branch information
micahmo authored May 30, 2024
1 parent 3246dbc commit 9827880
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
17 changes: 16 additions & 1 deletion lib/comment/utils/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions lib/post/utils/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,40 @@ Future<bool> 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
Expand Down

0 comments on commit 9827880

Please sign in to comment.