Skip to content

Commit 445d55d

Browse files
weiranclaude
andcommitted
Sync post updates from comments view back to feed view
When post data changes in the comments view (e.g., votes, title, comment count), those changes are now reflected in the feed view when navigating back. This is achieved by updating the NavigationStore's selectedPost property whenever the post changes in CommentsViewModel, and having FeedView observe and apply those updates. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6f90ebe commit 445d55d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Features/Comments/Sources/Comments/CommentsView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ public struct CommentsView<NavigationStore: NavigationStoreProtocol>: View {
114114
}
115115
.task {
116116
votingViewModel.navigationStore = navigationStore
117+
// Set up callback to update navigation store when post changes
118+
viewModel.onPostUpdated = { [weak navigationStore] updatedPost in
119+
if let updatedPost {
120+
navigationStore?.selectedPost = updatedPost
121+
}
122+
}
117123
await viewModel.loadComments()
118124
if let targetID = pendingCommentID {
119125
_ = await viewModel.revealComment(withId: targetID)

Features/Comments/Sources/Comments/CommentsViewModel.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ import SwiftUI
1414
@Observable
1515
public final class CommentsViewModel: @unchecked Sendable {
1616
public let postID: Int
17-
public var post: Post?
17+
public var post: Post? {
18+
didSet {
19+
onPostUpdated?(post)
20+
}
21+
}
1822
public var visibleComments: [Comment] = []
1923
public var showThumbnails: Bool
2024

2125
// Callback for when comments are loaded (used for HTML parsing in the view layer)
2226
public var onCommentsLoaded: (([Comment]) -> Void)?
27+
// Callback for when post is updated
28+
public var onPostUpdated: ((Post?) -> Void)?
2329

2430
private let postUseCase: any PostUseCase
2531
private let commentUseCase: any CommentUseCase

Features/Feed/Sources/Feed/FeedView.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ public struct FeedView<NavigationStore: NavigationStoreProtocol>: View {
9797
votingViewModel.navigationStore = navigationStore
9898
await viewModel.loadFeed()
9999
}
100+
.onChange(of: navigationStore.selectedPost) { oldPost, newPost in
101+
// When selectedPost changes in navigation store (e.g., from comments view),
102+
// update it in the feed
103+
if let updatedPost = newPost {
104+
viewModel.replacePost(updatedPost)
105+
}
106+
}
100107
.alert(
101108
"Vote Error",
102109
isPresented: Binding(

0 commit comments

Comments
 (0)