-
Notifications
You must be signed in to change notification settings - Fork 47
feat: add reply sorting by popularity #121
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -59,6 +59,23 @@ struct FeedDetailPage: StateView, KeyboardReadable, InstanceIdentifiable { | |||||||
| !state.replyContent.isEmpty | ||||||||
| } | ||||||||
|
|
||||||||
| /// 根据当前排序方式返回排序后的回复列表 | ||||||||
| private var sortedReplies: [FeedDetailInfo.ReplyInfo.Item] { | ||||||||
| let items = state.model.replyInfo.items | ||||||||
| switch state.replySortType { | ||||||||
| case .byTime: | ||||||||
| return items // 按时间排序(保持原始楼层顺序) | ||||||||
| case .byPopularity: | ||||||||
| // 按点赞数降序,相同点赞数按楼层升序 | ||||||||
| return items.sorted { (a, b) in | ||||||||
| if a.loveCount != b.loveCount { | ||||||||
| return a.loveCount > b.loveCount | ||||||||
| } | ||||||||
| return a.floor < b.floor | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private var isContentEmpty: Bool { | ||||||||
| let contentInfo = state.model.contentInfo | ||||||||
| return contentInfo == nil || contentInfo!.html.isEmpty | ||||||||
|
|
@@ -167,8 +184,16 @@ struct FeedDetailPage: StateView, KeyboardReadable, InstanceIdentifiable { | |||||||
| .listRowBackground(Color.itemBg) | ||||||||
| } | ||||||||
|
|
||||||||
| // Reply Section Header with Sort Toggle | ||||||||
| if !state.model.replyInfo.items.isEmpty { | ||||||||
| replySectionHeader | ||||||||
| .listRowInsets(EdgeInsets()) | ||||||||
| .listRowSeparator(.hidden) | ||||||||
| .listRowBackground(Color.itemBg) | ||||||||
| } | ||||||||
|
|
||||||||
| // Reply Section | ||||||||
| ForEach(state.model.replyInfo.items) { item in | ||||||||
| ForEach(sortedReplies, id: \.floor) { item in | ||||||||
| ReplyItemView(info: item, topicId: id) | ||||||||
| .listRowInsets(EdgeInsets()) | ||||||||
| .listRowSeparator(.hidden) | ||||||||
|
|
@@ -291,6 +316,55 @@ struct FeedDetailPage: StateView, KeyboardReadable, InstanceIdentifiable { | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| /// 选中项的文字颜色(与 tintColor 背景形成对比) | ||||||||
| private var segmentSelectedTextColor: Color { | ||||||||
| Color.dynamicHex(light: 0xFFFFFF, dark: 0x1C1C1E) | ||||||||
| } | ||||||||
|
|
||||||||
| @ViewBuilder | ||||||||
| private var replySectionHeader: some View { | ||||||||
| HStack { | ||||||||
| Text("回复") | ||||||||
| .font(.subheadline.weight(.medium)) | ||||||||
| .foregroundColor(.primaryText) | ||||||||
|
|
||||||||
| Spacer() | ||||||||
|
|
||||||||
| // Segmented sort picker | ||||||||
| HStack(spacing: 0) { | ||||||||
| ForEach(ReplySortType.allCases, id: \.self) { sortType in | ||||||||
| Button { | ||||||||
| store.appState.feedDetailStates[instanceId]?.replySortType = sortType | ||||||||
| } label: { | ||||||||
| HStack(spacing: 3) { | ||||||||
| Image(systemName: sortType.iconName) | ||||||||
| .font(.caption2) | ||||||||
| Text(sortType.displayName) | ||||||||
| .font(.caption) | ||||||||
| } | ||||||||
| .foregroundColor(state.replySortType == sortType ? segmentSelectedTextColor : .secondaryText) | ||||||||
| .padding(.horizontal, 10) | ||||||||
| .padding(.vertical, 5) | ||||||||
| .background( | ||||||||
| state.replySortType == sortType | ||||||||
| ? Color.tintColor | ||||||||
| : Color.clear | ||||||||
| ) | ||||||||
| } | ||||||||
|
||||||||
| } | |
| } | |
| .buttonStyle(.plain) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
flooras the ForEach identifier instead of the defaultid(UUID) property will cause SwiftUI view identity issues when sorting changes. When the sort order toggles between time and popularity, SwiftUI will incorrectly reuse views because the floor numbers don't change, leading to:The Item struct is already Identifiable with a UUID
idproperty. Keep using the default identifier by changing this line back toForEach(sortedReplies)without theid:parameter.