Skip to content

Commit bad6a2e

Browse files
weiranclaude
andcommitted
Consolidate unvote link clearing logic into VotingViewModel
Move the logic to clear the unvote link after a successful unvote operation from the UI layer into VotingViewModel.unvote(). This eliminates duplicate code that was present in both the swipe action and handleUnvote() method. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 445d55d commit bad6a2e

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

Features/Comments/Sources/Comments/CommentsComponents.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ struct CommentsContentView: View {
6363
var mutablePost = post
6464
await votingViewModel.unvote(post: &mutablePost)
6565
await MainActor.run {
66-
if !mutablePost.upvoted {
67-
if let existingLinks = mutablePost.voteLinks {
68-
mutablePost.voteLinks = VoteLinks(upvote: existingLinks.upvote, unvote: nil)
69-
}
70-
viewModel.post = mutablePost
71-
}
66+
viewModel.post = mutablePost
7267
}
7368
}
7469
} label: {
@@ -287,9 +282,6 @@ struct PostHeader: View {
287282
let wasUnvoted = !mutablePost.upvoted
288283

289284
if wasUnvoted {
290-
if let existingLinks = mutablePost.voteLinks {
291-
mutablePost.voteLinks = VoteLinks(upvote: existingLinks.upvote, unvote: nil)
292-
}
293285
await MainActor.run {
294286
onPostUpdated(mutablePost)
295287
}

Shared/Sources/Shared/ViewModels/VotingViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public final class VotingViewModel {
8686
post.upvoted = false
8787
post.score -= 1
8888

89+
// Clear unvote link after successful unvote
90+
if let existingLinks = post.voteLinks {
91+
post.voteLinks = VoteLinks(upvote: existingLinks.upvote, unvote: nil)
92+
}
93+
8994
isVoting = true
9095
lastError = nil
9196

0 commit comments

Comments
 (0)