Skip to content

Commit f319bf5

Browse files
committed
检索数据分页加载,添加收藏字段
1 parent d137a4a commit f319bf5

File tree

7 files changed

+81
-43
lines changed

7 files changed

+81
-43
lines changed

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

+25-21
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ struct EditInfoView: View {
2626
VStack {
2727
Form {
2828
Section {
29-
TextField("标题:", text: $info.name)
29+
HStack {
30+
TextField("标题:", text: $info.name)
31+
Toggle(isOn: $info.star) {
32+
Image(systemName: info.star ? "star.fill" : "star")
33+
}
34+
.toggleStyle(.button)
35+
}
3036
HStack {
3137
TextField("地址:", text: $info.url)
3238
.onSubmit {
@@ -41,9 +47,9 @@ struct EditInfoView: View {
4147
}
4248
} // end Task
4349
}
44-
// .onChange(of: info.url) { oldValue, newValue in
45-
//
46-
// }
50+
.onChange(of: info.url) { oldValue, newValue in
51+
52+
}
4753
if info.url.isEmpty == false {
4854
Button {
4955
gotoWebBrowser(urlStr: info.url)
@@ -70,29 +76,28 @@ struct EditInfoView: View {
7076
} // end Section
7177

7278
Section {
73-
Picker("分类:", selection: $info.category) {
74-
Text("未分类")
75-
.tag(Optional<IOCategory>.none)
76-
if categories.isEmpty == false {
77-
Divider()
78-
ForEach(categories) { cate in
79-
Text(cate.name)
80-
.tag(Optional(cate))
79+
80+
HStack {
81+
Picker("分类:", selection: $info.category) {
82+
Text("未分类")
83+
.tag(Optional<IOCategory>.none)
84+
if categories.isEmpty == false {
85+
Divider()
86+
ForEach(categories) { cate in
87+
Text(cate.name)
88+
.tag(Optional(cate))
89+
}
8190
}
8291
}
83-
}
84-
.onChange(of: info.category) { oldValue, newValue in
85-
info.category?.updateDate = Date.now
86-
}
87-
HStack {
92+
.onHover(perform: { hovering in
93+
info.category?.updateDate = Date.now
94+
})
8895
Button("添加分类", action: addCate)
8996
Button("管理分类", action: manageCate)
9097
}
9198
}
9299

93-
Section {
94-
// TextEditor(text: $info.des)
95-
// .tabItem { Label("文本", systemImage: "circle") }
100+
Section(footer: Text("支持 markdown 格式的文本")) {
96101
// TODO: markdown 获取图片链接,并能显示
97102
TabView {
98103
TextEditor(text: $info.des)
@@ -102,7 +107,6 @@ struct EditInfoView: View {
102107
}
103108
}
104109
} // end form
105-
.navigationTitle("编辑资料")
106110
.padding(10)
107111
.inspector(isPresented: $isShowInspector) {
108112
EditCategoryView(cate: cate ?? IOCategory(name: "unavailable.com", createDate: Date.now, updateDate: Date.now))

SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ struct InfoListView: View {
1616

1717
@Query(sort: [SortDescriptor(\IOCategory.updateDate, order: .reverse)]) var cates: [IOCategory]
1818
@State private var filterCate = ""
19+
@State var limit: Int = 100
1920

2021
var body: some View {
21-
InfosView(filterCateName: filterCate, searchString: searchText, selectInfo: $selectInfo, sortOrder: sortOrder)
22+
InfosView(filterCateName: filterCate, searchString: searchText, selectInfo: $selectInfo, sortOrder: sortOrder, limit: $limit)
2223
.navigationTitle("资料列表")
2324
.toolbar {
2425
ToolbarItem(placement: .navigation) {
@@ -49,15 +50,17 @@ struct InfoListView: View {
4950
Button(action: {
5051
modelContext.undoManager?.undo()
5152
}, label: {
52-
Image(systemName: "arrow.left")
53+
Image(systemName: "arrow.uturn.backward.circle")
54+
Text("撤回")
5355
})
5456
.disabled(modelContext.undoManager?.canUndo == false)
5557
}
5658
ToolbarItem(placement: .primaryAction) {
5759
Button(action: {
5860
modelContext.undoManager?.redo()
5961
}, label: {
60-
Image(systemName: "arrow.right")
62+
Image(systemName: "arrow.uturn.forward.circle")
63+
Text("重做")
6164
})
6265
.disabled(modelContext.undoManager?.canRedo == false)
6366
}

SwiftPamphletApp/InfoOrganizer/Info/InfoRowView.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ struct InfoRowView: View {
1616
}
1717

1818
var body: some View {
19-
HStack {
19+
VStack(alignment:.leading) {
20+
HStack {
21+
Text(info.category?.name ?? "")
22+
if info.star == true {
23+
Image(systemName: "star.fill")
24+
}
25+
Spacer()
26+
}
27+
.foregroundColor(light: .secondary, dark: .secondary)
2028
Text(info.name)
21-
Spacer()
22-
Text(info.category?.name ?? "")
2329
}
2430
.swipeActions {
2531
Button(role: .destructive) {

SwiftPamphletApp/InfoOrganizer/Info/InfosView.swift

+17-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ struct InfosView: View {
1212
@Environment(\.modelContext) var modelContext
1313
@Query var infos: [IOInfo]
1414
@Binding var selectInfo: IOInfo?
15+
@Binding var limit: Int
1516

16-
init(filterCateName: String = "", searchString: String = "", selectInfo: Binding<IOInfo?>, sortOrder: [SortDescriptor<IOInfo>] = []) {
17+
init(filterCateName: String = "", searchString: String = "", selectInfo: Binding<IOInfo?>, sortOrder: [SortDescriptor<IOInfo>] = [], limit: Binding<Int>) {
1718
var fd = FetchDescriptor<IOInfo>(predicate: #Predicate { info in
18-
if !filterCateName.isEmpty {
19+
if !filterCateName.isEmpty && !searchString.isEmpty {
20+
(info.name.localizedStandardContains(searchString)
21+
|| info.url.localizedStandardContains(searchString)
22+
|| info.des.localizedStandardContains(searchString)) && info.category?.name == filterCateName
23+
} else if !filterCateName.isEmpty {
1924
info.category?.name == filterCateName
2025
} else if searchString.isEmpty {
2126
true
@@ -25,18 +30,27 @@ struct InfosView: View {
2530
|| info.des.localizedStandardContains(searchString)
2631
}
2732
}, sortBy: sortOrder)
28-
fd.fetchLimit = 10000
33+
fd.fetchLimit = limit.wrappedValue
2934
_infos = Query(fd)
3035

3136
self._selectInfo = selectInfo
37+
self._limit = limit
3238
}
3339

3440
var body: some View {
3541
List(selection: $selectInfo) {
3642
ForEach(infos) { info in
3743
InfoRowView(info: info, selectedInfo: selectInfo)
3844
.tag(info)
45+
.onAppear {
46+
if info == infos.last {
47+
if limit <= infos.count {
48+
limit += 100
49+
}
50+
}
51+
}
3952
}
53+
4054
}
4155
.listStyle(.inset)
4256
.alternatingRowBackgrounds()

SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class IOInfo {
1414
var url: String = ""
1515
var des: String = ""
1616
var category: IOCategory? = nil// 关系字段,链接 IOCategory
17+
var star: Bool = false
1718

1819
var createDate: Date = Date.now
1920
var updateDate: Date = Date.now
@@ -44,6 +45,7 @@ final class IOInfo {
4445
class IOCategory {
4546
var name: String = ""
4647
var infos: [IOInfo]? = [IOInfo]() // 关系字段,链接 IOInfo
48+
var star: Bool = false
4749

4850
var createDate: Date = Date.now
4951
var updateDate: Date = Date.now

SwiftPamphletApp/Setting/SettingView.swift

+19-13
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ import SwiftUI
1010
struct SettingView: View {
1111
@State private var tokenString = ""
1212
var body: some View {
13-
Form {
14-
Section {
15-
Text("在这里写上 Github 的 access token")
16-
TextField("", text: $tokenString)
17-
.padding(10)
18-
.onSubmit {
19-
let ud = UserDefaults.standard
20-
ud.set(tokenString, forKey: SPC.githubUDTokenKey)
21-
}
13+
TabView {
14+
Form {
15+
Section {
16+
Text("在这里写上 Github 的 access token")
17+
TextField("", text: $tokenString)
18+
.padding(10)
19+
.onSubmit {
20+
let ud = UserDefaults.standard
21+
ud.set(tokenString, forKey: SPC.githubUDTokenKey)
22+
}
23+
}
24+
.onAppear(perform: {
25+
let ud = UserDefaults.standard
26+
tokenString = ud.string(forKey: SPC.githubUDTokenKey) ?? ""
27+
})
28+
}
29+
.padding(20)
30+
.tabItem {
31+
Label("通常", systemImage: "gearshape")
2232
}
23-
.onAppear(perform: {
24-
let ud = UserDefaults.standard
25-
tokenString = ud.string(forKey: SPC.githubUDTokenKey) ?? ""
26-
})
2733
}
2834
}
2935
}

SwiftPamphletApp/SwiftPamphletAppApp.swift

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ struct SwiftPamphletAppApp: App {
2323

2424
}
2525
.windowToolbarStyle(UnifiedWindowToolbarStyle(showsTitle: true)) // 用来控制是否展示标题
26+
Settings {
27+
SettingView()
28+
}
2629
}
2730
}
2831

0 commit comments

Comments
 (0)