Skip to content

Commit cb9dd3d

Browse files
committed
Fix loadmore
1 parent 7f7a553 commit cb9dd3d

File tree

4 files changed

+83
-77
lines changed

4 files changed

+83
-77
lines changed

V2exOS.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
0DA3F3A128A40ACF00E7224D /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DA3F3A028A40ACF00E7224D /* ProfileView.swift */; };
2424
0DA3F3A428A411ED00E7224D /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = 0DA3F3A328A411ED00E7224D /* KeychainAccess */; };
2525
0DA3F3A728A412FE00E7224D /* CurrentUserStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DA3F3A628A412FE00E7224D /* CurrentUserStore.swift */; };
26-
0DC5364528A7A9D80093005B /* LoadingCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DC5364428A7A9D80093005B /* LoadingCell.swift */; };
2726
0DF3300E28A55DEA00AEF312 /* InboxListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DF3300D28A55DEA00AEF312 /* InboxListView.swift */; };
2827
0DF3301028A56A3600AEF312 /* StringExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DF3300F28A56A3600AEF312 /* StringExt.swift */; };
2928
/* End PBXBuildFile section */
@@ -44,7 +43,6 @@
4443
0D8F3358288D35D800C27AF1 /* TimeFormat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeFormat.swift; sourceTree = "<group>"; };
4544
0DA3F3A028A40ACF00E7224D /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; };
4645
0DA3F3A628A412FE00E7224D /* CurrentUserStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentUserStore.swift; sourceTree = "<group>"; };
47-
0DC5364428A7A9D80093005B /* LoadingCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingCell.swift; sourceTree = "<group>"; };
4846
0DF3300D28A55DEA00AEF312 /* InboxListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InboxListView.swift; sourceTree = "<group>"; };
4947
0DF3300F28A56A3600AEF312 /* StringExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExt.swift; sourceTree = "<group>"; };
5048
/* End PBXFileReference section */
@@ -122,7 +120,6 @@
122120
0D8F334F288D1F8800C27AF1 /* TopicListCellView.swift */,
123121
0D8A2E6E2896AB49001B2769 /* CommentListView.swift */,
124122
0DF3300D28A55DEA00AEF312 /* InboxListView.swift */,
125-
0DC5364428A7A9D80093005B /* LoadingCell.swift */,
126123
);
127124
path = Views;
128125
sourceTree = "<group>";
@@ -241,7 +238,6 @@
241238
0D8F3350288D1F8800C27AF1 /* TopicListCellView.swift in Sources */,
242239
0D033B42288D15CF00129C9B /* TopicDetailView.swift in Sources */,
243240
0D033B40288D140C00129C9B /* TopicListView.swift in Sources */,
244-
0DC5364528A7A9D80093005B /* LoadingCell.swift in Sources */,
245241
0D033B26288CD64500129C9B /* V2exOSApp.swift in Sources */,
246242
0D033B3E288D0FF700129C9B /* SidebarView.swift in Sources */,
247243
0DF3300E28A55DEA00AEF312 /* InboxListView.swift in Sources */,

V2exOS.xcodeproj/xcuserdata/isaced.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,4 @@
33
uuid = "2EBA2181-B07E-4F02-9460-DFF7ACE08988"
44
type = "1"
55
version = "2.0">
6-
<Breakpoints>
7-
<BreakpointProxy
8-
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
9-
<BreakpointContent
10-
uuid = "3F03D402-B2BA-4C74-8696-105EFCF3676F"
11-
shouldBeEnabled = "Yes"
12-
ignoreCount = "0"
13-
continueAfterRunningActions = "No"
14-
breakpointStackSelectionBehavior = "1"
15-
scope = "1"
16-
stopOnStyle = "0">
17-
</BreakpointContent>
18-
</BreakpointProxy>
19-
<BreakpointProxy
20-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
21-
<BreakpointContent
22-
uuid = "A263DB1C-EEF2-49AE-98BA-F16F73964535"
23-
shouldBeEnabled = "Yes"
24-
ignoreCount = "0"
25-
continueAfterRunningActions = "No"
26-
filePath = "../V2exAPI/Sources/V2exAPI/V2exAPI.swift"
27-
startingColumnNumber = "9223372036854775807"
28-
endingColumnNumber = "9223372036854775807"
29-
startingLineNumber = "87"
30-
endingLineNumber = "87"
31-
landmarkName = "hotTopics()"
32-
landmarkType = "7">
33-
</BreakpointContent>
34-
</BreakpointProxy>
35-
</Breakpoints>
366
</Bucket>

V2exOS/Views/TopicDetailView.swift

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ import MarkdownUI
1212
struct TopicDetailView: View {
1313

1414
var topic: V2Topic
15+
1516
@State var commentList: [V2Comment]?
17+
@State var page = 1
18+
@State var commenEnd = false
19+
20+
func hasCommen() -> Bool {
21+
return commenEnd || commentList?.count ?? 0 < topic.replies ?? 0
22+
}
1623

1724
var body: some View {
1825
List {
@@ -44,19 +51,49 @@ struct TopicDetailView: View {
4451
.fixedSize(horizontal: false, vertical: true)
4552
}
4653

47-
if let commentList = commentList{
54+
if let commentList = commentList {
4855
Spacer()
4956
CommentListView(commentList: commentList)
57+
58+
if hasCommen() {
59+
ProgressView()
60+
.onAppear {
61+
loadComments(page: page + 1)
62+
}
63+
}
5064
}
5165
}
5266
.foregroundColor(Color(NSColor.labelColor))
53-
.onAppear(perform: loadComments)
67+
.task {
68+
loadComments(page: 1)
69+
}
5470
}
5571

56-
func loadComments(){
72+
func loadComments(page: Int) {
73+
print("loadComments, page:", page)
74+
5775
Task {
58-
let res = try await v2ex.replies(topicId: topic.id)
59-
commentList = res?.result
76+
do {
77+
let res = try await v2ex.replies(topicId: topic.id, page: page)
78+
if page == 1 {
79+
commentList = res?.result
80+
}else{
81+
if let list = res?.result {
82+
if !hasCommen() {
83+
return
84+
}
85+
86+
commentList?.append(contentsOf: list)
87+
// 到底了
88+
if list.count == 0 {
89+
commenEnd = true
90+
}
91+
}
92+
}
93+
} catch {
94+
commenEnd = true
95+
print(error)
96+
}
6097
}
6198
}
6299
}

V2exOS/Views/TopicListView.swift

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,75 @@ struct TopicListView: View {
1212

1313
var nodeName: String
1414

15-
@State var isLoading = false
15+
@State var isLoading = true
1616
@State var topics: [V2Topic]?
1717
@State var page = 1
18+
@State var error: Error?
1819

1920
var body: some View {
2021
NavigationView{
21-
List {
22-
if isLoading {
23-
VStack(alignment: .center) {
24-
ProgressView()
25-
}
26-
}else{
22+
if isLoading {
23+
ProgressView()
24+
.frame(minWidth: 400)
25+
}else{
26+
List {
2727
if let topics = topics {
2828
ForEach(topics) { topic in
2929
TopicListCellView(topic: topic)
3030
}
31+
32+
if topics.count > 0 && nodeName != "ALL" {
33+
ProgressView()
34+
.onAppear {
35+
Task {
36+
await self.loadData(page: self.page + 1)
37+
}
38+
}
39+
}
3140
}
3241

33-
if topics != nil && nodeName != "ALL" {
34-
ProgressView()
35-
.task {
36-
await self.onNextPage()
37-
}
38-
}
3942
}
43+
.listStyle(.inset)
44+
.frame(minWidth: 400, idealWidth: 500, maxWidth: 700)
45+
.foregroundColor(.black)
4046
}
41-
.listStyle(.inset)
42-
.frame(minWidth: 400, idealWidth: 500, maxWidth: 700)
43-
.foregroundColor(.black)
44-
.task {
45-
await loadData()
46-
}
47+
}
48+
.task {
49+
await loadData()
4750
}
4851
}
4952

50-
func loadData() async {
51-
isLoading = true
53+
func loadData(page: Int = 1) async {
54+
if error != nil {
55+
return
56+
}
57+
58+
if page == 1 {
59+
isLoading = true
60+
}
5261

5362
do {
5463
var topics : [V2Topic]? = nil
5564

5665
if nodeName == "ALL" {
5766
topics = try await v2ex.latestTopics()
5867
}else{
59-
topics = try await v2ex.topics(nodeName: nodeName)?.result
68+
topics = try await v2ex.topics(nodeName: nodeName, page: page)?.result
6069
}
61-
self.topics = topics
6270

63-
} catch {
64-
print(error)
65-
}
66-
67-
isLoading = false
68-
}
69-
70-
func onNextPage() async {
71-
isLoading = true
72-
73-
do {
74-
if nodeName != "ALL" {
75-
if let topics = try await v2ex.topics(nodeName: nodeName, page: page + 1)?.result {
76-
page = page + 1
71+
if page == 1 {
72+
self.topics = topics
73+
}else{
74+
self.page = page
75+
if let topics = topics {
7776
self.topics?.append(contentsOf: topics)
7877
}
7978
}
80-
} catch {}
79+
80+
} catch {
81+
self.error = error;
82+
print(error)
83+
}
8184

8285
isLoading = false
8386
}

0 commit comments

Comments
 (0)