Skip to content

Commit d11e64e

Browse files
committed
分类置顶功能
1 parent 222ebb4 commit d11e64e

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

SwiftPamphletApp/InfoOrganizer/Category/CategoryRowView.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,35 @@ struct CategoryRowView: View {
1717

1818
var body: some View {
1919
HStack {
20+
if cate.pin == 1 {
21+
Image(systemName: "pin.fill")
22+
}
2023
TextField("name", text: $cate.name)
2124
Spacer()
2225
Text("\(cate.infos?.count ?? 0)")
2326
}
2427
.swipeActions {
28+
Button {
29+
IOCategory.pin(cate)
30+
} label: {
31+
Label("置顶", systemImage: cate.pin == 1 ? "pin.slash.fill" : "pin.fill")
32+
}
2533
Button(role: .destructive) {
2634
IOCategory.delete(cate)
2735
} label: {
2836
Label("删除", systemImage: "trash")
2937
}
3038
}
3139
.contextMenu {
32-
Button("删除") {
40+
Button {
41+
IOCategory.pin(cate)
42+
} label: {
43+
Label("置顶", systemImage: cate.pin == 1 ? "pin.slash.fill" : "pin.fill")
44+
}
45+
Button(role: .destructive) {
3346
IOCategory.delete(cate)
47+
} label: {
48+
Label("删除", systemImage: "trash")
3449
}
3550
}
3651

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ struct EditInfoView: View {
8484
if categories.isEmpty == false {
8585
Divider()
8686
ForEach(categories) { cate in
87-
Text(cate.name)
88-
.tag(Optional(cate))
87+
HStack {
88+
if cate.pin == 1 {
89+
Image(systemName: "pin.fill")
90+
}
91+
Text(cate.name)
92+
}
93+
.tag(Optional(cate))
8994
}
9095
}
9196
}

SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct InfoListView: View {
1414
@Binding var selectInfo:IOInfo?
1515
@State private var sortOrder = [SortDescriptor(\IOInfo.updateDate, order: .reverse)]
1616

17-
@Query(sort: [SortDescriptor(\IOCategory.name, order: .forward)]) var cates: [IOCategory]
17+
@Query(IOCategory.allOrderByName) var cates: [IOCategory]
1818
@State private var filterCate = ""
1919
@State var limit: Int = 100
2020
@State var filterStar: Bool = false
@@ -28,12 +28,20 @@ struct InfoListView: View {
2828
}
2929
ToolbarItem(placement: .navigation) {
3030
Picker("分类", selection: $filterCate) {
31-
Text("全部")
32-
.tag("")
31+
HStack {
32+
Image(systemName: "books.vertical")
33+
Text("全部")
34+
}
35+
.tag("")
3336

3437
ForEach(cates) { cate in
35-
Text(cate.name)
36-
.tag(cate.name)
38+
HStack {
39+
if cate.pin == 1 {
40+
Image(systemName: "pin.fill")
41+
}
42+
Text(cate.name)
43+
}
44+
.tag(cate.name)
3745
}
3846
}
3947
}

SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift

+13-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class IOInfo {
4545
class IOCategory {
4646
var name: String = ""
4747
var infos: [IOInfo]? = [IOInfo]() // 关系字段,链接 IOInfo
48-
var star: Bool = false
48+
var pin: Int = 0
4949

5050
var createDate: Date = Date.now
5151
var updateDate: Date = Date.now
@@ -60,14 +60,24 @@ class IOCategory {
6060
}
6161

6262
static var all: FetchDescriptor<IOCategory> {
63-
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.updateDate, order: .reverse)])
63+
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.pin, order: .reverse), SortDescriptor(\IOCategory.updateDate, order: .reverse)])
6464
return fd
6565
}
6666
static var allOrderByName: FetchDescriptor<IOCategory> {
67-
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.name, order: .forward)])
67+
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.pin, order: .reverse), SortDescriptor(\IOCategory.name, order: .forward)])
6868
return fd
6969
}
7070

71+
static func pin(_ cate: IOCategory) {
72+
if cate.modelContext != nil {
73+
if cate.pin == 0 {
74+
cate.pin = 1
75+
} else {
76+
cate.pin = 0
77+
}
78+
}
79+
}
80+
7181
static func delete(_ cate: IOCategory) {
7282
if let context = cate.modelContext {
7383
context.delete(cate)

0 commit comments

Comments
 (0)