Skip to content

Commit e7695a1

Browse files
authored
Merge pull request #21 from nicoster/main
add avatars for topics other than "ALL"
2 parents 6402da1 + 7a3e02c commit e7695a1

File tree

8 files changed

+73
-45
lines changed

8 files changed

+73
-45
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
V2exOS.xcodeproj/project.xcworkspace/xcuserdata/
2+
V2exOS.xcodeproj/xcuserdata/*.xcuserdatad/
3+
.python-version

V2exOS.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

V2exOS/Utils/PreviewData.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import Foundation
99
import V2exAPI
1010

11+
12+
13+
1114
struct PreviewData {
1215
static let member = V2Member(id: 79764,
1316
username: "ljsh093",

V2exOS/Utils/StringExt.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
extension String {
1111
func htmlToString() -> String? {
1212
do {
13+
1314
let text = try NSAttributedString(data: self.data(using: .unicode)!,
1415
options: [.documentType: NSAttributedString.DocumentType.html],
1516
documentAttributes: nil).string

V2exOS/Views/SidebarView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ struct SidebarView: View {
1414
@EnvironmentObject private var currentUser: CurrentUserStore
1515

1616
@State var nodeList : [V2Node]?
17-
@State var isLoading = true
17+
@State var isLoading = true
1818

1919
var body: some View {
2020
List() {
2121

2222
Section(header: Text("Home")) {
23+
NavigationLink(destination: TopicListView(nodeName: "HOT")) {
24+
Label("最热", systemImage: "flame")
25+
}
2326
NavigationLink(destination: TopicListView(nodeName: "ALL")) {
24-
Label("全部", systemImage: "chart.bar")
27+
Label("最新", systemImage: "chart.bar")
2528
}
2629
}
2730

V2exOS/Views/TopicDetail/TopicDetailView.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ struct TopicDetailView: View {
1717

1818
@State var commentList: [V2Comment]?
1919
@State var page = 0
20-
@State var commenEnd = false
20+
@State var commentEnd = false
2121
@State var isCommentLoading = false
2222

23-
func hasCommen() -> Bool {
24-
return currentUser.user != nil && (commenEnd || commentList?.count ?? 0 < topic.replies ?? 0)
23+
func hasMoreComments() -> Bool {
24+
return currentUser.user != nil && (commentEnd || commentList?.count ?? 0 < topic.replies ?? 0)
2525
}
2626

2727
var body: some View {
@@ -64,12 +64,16 @@ struct TopicDetailView: View {
6464

6565
CommentListView(commentCount: topic.replies, commentList: commentList)
6666

67-
if hasCommen() {
67+
if hasMoreComments() {
6868
Spacer()
69-
ProgressView()
70-
.onAppear {
71-
loadComments(page: page + 1)
72-
}
69+
HStack {
70+
Spacer()
71+
ProgressView()
72+
.onAppear {
73+
loadComments(page: page + 1)
74+
}
75+
Spacer()
76+
}
7377
}
7478
}
7579
.foregroundColor(Color(NSColor.labelColor))
@@ -92,20 +96,20 @@ struct TopicDetailView: View {
9296
commentList = res?.result
9397
}else{
9498
if let list = res?.result {
95-
if !hasCommen() {
99+
if !hasMoreComments() {
96100
isCommentLoading = false
97101
return
98102
}
99103

100104
commentList?.append(contentsOf: list)
101105
// 到底了
102106
if list.count == 0 {
103-
commenEnd = true
107+
commentEnd = true
104108
}
105109
}
106110
}
107111
} catch {
108-
commenEnd = true
112+
commentEnd = true
109113
print(error)
110114
}
111115

V2exOS/Views/TopicList/TopicListCellView.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,49 @@ import V2exAPI
1010
import Kingfisher
1111

1212
struct TopicListCellView: View {
13+
@State var isMemberLoading = false
1314

1415
@Environment(\.colorScheme) var colorScheme
1516

1617
let topic : V2Topic
18+
@State var member : V2Member?
19+
20+
init(topic: V2Topic) {
21+
self.topic = topic
22+
self.isMemberLoading = topic.member == nil
23+
}
1724

1825
var body: some View {
1926
NavigationLink {
2027
TopicDetailView(topic: topic)
2128
} label: {
2229

2330
HStack {
24-
if let avatarUrl = topic.member?.avatarLarge {
25-
KFImage.url(URL(string: avatarUrl))
26-
.resizable()
27-
.fade(duration: 0.25)
28-
.scaledToFit()
29-
.frame(width: 48, height: 48)
30-
.mask(RoundedRectangle(cornerRadius: 8))
31+
32+
if !isMemberLoading {
33+
let user = topic.member ?? member
34+
if let avatarUrl = user?.avatarLarge {
35+
KFImage.url(URL(string: avatarUrl))
36+
.resizable()
37+
.fade(duration: 0.25)
38+
.scaledToFit()
39+
.frame(width: 48, height: 48)
40+
.mask(RoundedRectangle(cornerRadius: 8))
41+
}
3142
}
43+
3244
VStack(alignment: .leading, spacing: 6) {
3345

3446
Text(topic.title ?? "")
3547
.lineLimit(2)
3648

3749
HStack() {
38-
if let username = topic.member?.username {
50+
51+
if let username = topic.member?.username ?? topic.lastReplyBy {
3952
UserName(username)
40-
4153
Text("")
4254
}
4355

44-
4556
if let lastModified = topic.lastModified {
4657
Text(Date.init(timeIntervalSince1970: TimeInterval(lastModified)).fromNow())
4758
}
@@ -60,6 +71,12 @@ struct TopicListCellView: View {
6071

6172
}
6273
.foregroundColor(Color(NSColor.labelColor))
74+
.task {
75+
if let name = topic.lastReplyBy {
76+
member = try? await v2ex.memberShow(username: name)
77+
isMemberLoading = member == nil
78+
}
79+
}
6380
}
6481
}
6582
}

V2exOS/Views/TopicList/TopicListView.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Kingfisher
1212
struct TopicListView: View {
1313

1414
var nodeName: String
15-
var node: V2Node?
1615

1716
@State var isLoading = true
1817
@State var topics: [V2Topic]?
@@ -32,13 +31,17 @@ struct TopicListView: View {
3231
TopicListCellView(topic: topic)
3332
}
3433

35-
if topics.count > 0 && nodeName != "ALL" {
36-
ProgressView()
37-
.onAppear {
38-
Task {
39-
await self.loadData(page: self.page + 1)
34+
if topics.count > 0 && nodeName != "ALL" && nodeName != "HOT" {
35+
HStack {
36+
Spacer()
37+
ProgressView()
38+
.onAppear {
39+
Task {
40+
await self.loadData(page: self.page + 1)
41+
}
4042
}
41-
}
43+
Spacer()
44+
}
4245
}
4346
}
4447

@@ -76,14 +79,17 @@ struct TopicListView: View {
7679

7780
if nodeName == "ALL" {
7881
topics = try await v2ex.latestTopics()
79-
}else{
82+
} else if nodeName == "HOT" {
83+
topics = try await v2ex.hotTopics()
84+
85+
} else {
8086
topics = try await v2ex.topics(nodeName: nodeName, page: page)?.result
8187
_node = try await v2ex.nodesShow(name: nodeName)
8288
}
8389

8490
if page == 1 {
8591
self.topics = topics
86-
}else{
92+
} else {
8793
self.page = page
8894
if let topics = topics {
8995
self.topics?.append(contentsOf: topics)

0 commit comments

Comments
 (0)