Skip to content

Commit

Permalink
分类置顶功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Mar 20, 2024
1 parent 222ebb4 commit d11e64e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
17 changes: 16 additions & 1 deletion SwiftPamphletApp/InfoOrganizer/Category/CategoryRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,35 @@ 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: {
Label("删除", systemImage: "trash")
}
}
.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")
}
}

Expand Down
9 changes: 7 additions & 2 deletions SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,14 +60,24 @@ class IOCategory {
}

static var all: FetchDescriptor<IOCategory> {
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<IOCategory> {
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)
Expand Down

0 comments on commit d11e64e

Please sign in to comment.