diff --git a/SwiftPamphletApp/InfoOrganizer/Category/CategoryRowView.swift b/SwiftPamphletApp/InfoOrganizer/Category/CategoryRowView.swift index a05e9a6a1..61c1f20d3 100644 --- a/SwiftPamphletApp/InfoOrganizer/Category/CategoryRowView.swift +++ b/SwiftPamphletApp/InfoOrganizer/Category/CategoryRowView.swift @@ -17,11 +17,19 @@ struct CategoryRowView: View { var body: some View { HStack { + if cate.pin == 1 { + Image(systemName: "pin.fill") + } TextField("name", text: $cate.name) Spacer() Text("\(cate.infos?.count ?? 0)") } .swipeActions { + Button { + IOCategory.pin(cate) + } label: { + Label("置顶", systemImage: cate.pin == 1 ? "pin.slash.fill" : "pin.fill") + } Button(role: .destructive) { IOCategory.delete(cate) } label: { @@ -29,8 +37,15 @@ struct CategoryRowView: View { } } .contextMenu { - Button("删除") { + Button { + IOCategory.pin(cate) + } label: { + Label("置顶", systemImage: cate.pin == 1 ? "pin.slash.fill" : "pin.fill") + } + Button(role: .destructive) { IOCategory.delete(cate) + } label: { + Label("删除", systemImage: "trash") } } diff --git a/SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift b/SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift index 5b8bda286..75b3c2915 100644 --- a/SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift +++ b/SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift @@ -84,8 +84,13 @@ struct EditInfoView: View { if categories.isEmpty == false { Divider() ForEach(categories) { cate in - Text(cate.name) - .tag(Optional(cate)) + HStack { + if cate.pin == 1 { + Image(systemName: "pin.fill") + } + Text(cate.name) + } + .tag(Optional(cate)) } } } diff --git a/SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift b/SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift index a05d57229..af125e787 100644 --- a/SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift +++ b/SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift @@ -14,7 +14,7 @@ struct InfoListView: View { @Binding var selectInfo:IOInfo? @State private var sortOrder = [SortDescriptor(\IOInfo.updateDate, order: .reverse)] - @Query(sort: [SortDescriptor(\IOCategory.name, order: .forward)]) var cates: [IOCategory] + @Query(IOCategory.allOrderByName) var cates: [IOCategory] @State private var filterCate = "" @State var limit: Int = 100 @State var filterStar: Bool = false @@ -28,12 +28,20 @@ struct InfoListView: View { } ToolbarItem(placement: .navigation) { Picker("分类", selection: $filterCate) { - Text("全部") - .tag("") + HStack { + Image(systemName: "books.vertical") + Text("全部") + } + .tag("") ForEach(cates) { cate in - Text(cate.name) - .tag(cate.name) + HStack { + if cate.pin == 1 { + Image(systemName: "pin.fill") + } + Text(cate.name) + } + .tag(cate.name) } } } diff --git a/SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift b/SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift index 6e8dde932..0d4ebd8e6 100644 --- a/SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift +++ b/SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift @@ -45,7 +45,7 @@ final class IOInfo { class IOCategory { var name: String = "" var infos: [IOInfo]? = [IOInfo]() // 关系字段,链接 IOInfo - var star: Bool = false + var pin: Int = 0 var createDate: Date = Date.now var updateDate: Date = Date.now @@ -60,14 +60,24 @@ class IOCategory { } static var all: FetchDescriptor { - let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.updateDate, order: .reverse)]) + let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.pin, order: .reverse), SortDescriptor(\IOCategory.updateDate, order: .reverse)]) return fd } static var allOrderByName: FetchDescriptor { - let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.name, order: .forward)]) + let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.pin, order: .reverse), SortDescriptor(\IOCategory.name, order: .forward)]) return fd } + static func pin(_ cate: IOCategory) { + if cate.modelContext != nil { + if cate.pin == 0 { + cate.pin = 1 + } else { + cate.pin = 0 + } + } + } + static func delete(_ cate: IOCategory) { if let context = cate.modelContext { context.delete(cate)