Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optimistic voting #1392

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading