Skip to content

Commit

Permalink
优化数据查询
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Mar 12, 2024
1 parent a4cbe2c commit 40c5796
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 40 deletions.
2 changes: 1 addition & 1 deletion SwiftPamphletApp/HomeUI/DataLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct DataLink: Identifiable {

extension DataLink {
static var dataLinks = [
DataLink(title: "动态", imageName: "", children: [
DataLink(title: "资料", imageName: "", children: [
DataLink(title: "资料整理", imageName: "p11")
]),
// DataLink(title: "Github", imageName: "", children: [
Expand Down
26 changes: 18 additions & 8 deletions SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
import SwiftUI
import SwiftData
import SwiftSoup
import CodeEditor

struct EditInfoView: View {
@Environment(\.modelContext) var modelContext
@Bindable var info: IOInfo

@Query(sort: [
SortDescriptor(\IOCategory.updateDate, order: .reverse)
]) var categories: [IOCategory]
@Query(IOCategory.all) var categories: [IOCategory]

@State var isShowInspector = false
@State var cate:IOCategory? = nil

@State var urlContent = ""

var body: some View {
VStack {
Form {
Expand All @@ -36,8 +35,13 @@ struct EditInfoView: View {
Button {
gotoWebBrowser(urlStr: info.url)
} label: {
Label("浏览器打开", systemImage: "safari")
} // end Button
Image(systemName: "safari")
}
Button {
info.des = urlContent
} label: {
Image(systemName: "square.and.arrow.down")
}
}
}

Expand All @@ -63,11 +67,16 @@ struct EditInfoView: View {
}

Section("备注") {
TextEditor(text: $info.des)
TabView {
TextEditor(text: $info.des)
.tabItem { Label("文本", systemImage: "circle") }
WebUIView(html: info.des)
.tabItem { Label("预览", systemImage: "circle") }
}
}
}
.navigationTitle("编辑资料")
.padding(30)
.padding(10)
.inspector(isPresented: $isShowInspector) {
EditCategoryView(cate: cate ?? IOCategory(name: "unavailable.com", createDate: Date.now, updateDate: Date.now))
}
Expand Down Expand Up @@ -110,6 +119,7 @@ struct EditInfoView: View {
title = soupTitle ?? "没找到标题"
}
info.name = title
urlContent = homepageHTML
}
}

Expand Down
61 changes: 34 additions & 27 deletions SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,51 @@ struct InfoListView: View {
InfosView(searchString: searchText, selectInfo: $selectInfo, sortOrder: sortOrder)
.navigationTitle("资料列表")
.toolbar {
Menu("Sort", systemImage: "arrow.up.arrow.down") {
Picker("分类", selection: $filterCate) {
ForEach(cates) { cate in
Text(cate.name)
.tag(cate.name)
ToolbarItem(placement: .navigation) {
Button("添加资料", systemImage: "plus", action: addInfo)
}
ToolbarItem(placement: .navigation) {
Menu("Sort", systemImage: "tag") {
Picker("分类", selection: $filterCate) {
ForEach(cates) { cate in
Text(cate.name)
.tag(cate.name)
}
}
Picker("排序", selection: $sortOrder) {
Text("正序")
.tag([SortDescriptor(\IOInfo.updateDate)])
Text("倒序")
.tag([SortDescriptor(\IOInfo.updateDate, order: .reverse)])
}
}
Picker("排序", selection: $sortOrder) {
Text("正序")
.tag([SortDescriptor(\IOInfo.updateDate)])
Text("倒序")
.tag([SortDescriptor(\IOInfo.updateDate, order: .reverse)])
}
}
Button("添加资料", systemImage: "plus", action: addInfo)

}
.searchable(text: $searchText)
} else {
InfosFilterWithCateView(filterCateName: filterCate, selectInfo: $selectInfo, sortOrder: sortOrder)
.navigationTitle("资料列表")
.navigationTitle("分类 - \(filterCate)")
.toolbar {
Menu("Sort", systemImage: "arrow.up.arrow.down") {
Picker("分类", selection: $filterCate) {
ForEach(cates) { cate in
Text(cate.name)
.tag(cate.name)
ToolbarItem(placement: .navigation) {
Button("添加资料", systemImage: "plus", action: addInfoWithCate)
}
ToolbarItem(placement: .navigation) {
Menu("Sort", systemImage: "tag") {
Picker("分类", selection: $filterCate) {
ForEach(cates) { cate in
Text(cate.name)
.tag(cate.name)
}
}
Picker("排序", selection: $sortOrder) {
Text("正序")
.tag([SortDescriptor(\IOInfo.updateDate)])
Text("倒序")
.tag([SortDescriptor(\IOInfo.updateDate, order: .reverse)])
}

}
Picker("排序", selection: $sortOrder) {
Text("正序")
.tag([SortDescriptor(\IOInfo.updateDate)])
Text("倒序")
.tag([SortDescriptor(\IOInfo.updateDate, order: .reverse)])
}

}
Button("添加资料", systemImage: "plus", action: addInfoWithCate)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ struct InfosFilterWithCateView: View {
@Binding var selectInfo: IOInfo?

init(filterCateName: String = "", selectInfo: Binding<IOInfo?>, sortOrder: [SortDescriptor<IOInfo>] = []) {
_infos = Query(filter: #Predicate { info in

var fd = FetchDescriptor<IOInfo>(predicate: #Predicate { info in
if filterCateName.isEmpty {
true
} else {
info.category?.name == filterCateName
}
}, sort: sortOrder)
}, sortBy: sortOrder)
fd.fetchLimit = 1000
_infos = Query(fd)

self._selectInfo = selectInfo
}
Expand Down
6 changes: 4 additions & 2 deletions SwiftPamphletApp/InfoOrganizer/Info/InfosView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ struct InfosView: View {
@Binding var selectInfo: IOInfo?

init(searchString: String = "", selectInfo: Binding<IOInfo?>, sortOrder: [SortDescriptor<IOInfo>] = []) {
_infos = Query(filter: #Predicate { info in
var fd = FetchDescriptor<IOInfo>(predicate: #Predicate { info in
if searchString.isEmpty {
true
} else {
info.name.localizedStandardContains(searchString)
|| info.url.localizedStandardContains(searchString)
|| info.des.localizedStandardContains(searchString)
}
}, sort: sortOrder)
}, sortBy: sortOrder)
fd.fetchLimit = 1000
_infos = Query(fd)

self._selectInfo = selectInfo
}
Expand Down
5 changes: 5 additions & 0 deletions SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class IOCategory {
self.updateDate = updateDate
}

static var all: FetchDescriptor<IOCategory> {
var fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.updateDate, order: .reverse)])
return fd
}

static func delete(_ cate: IOCategory) {
if let context = cate.modelContext {
context.delete(cate)
Expand Down

0 comments on commit 40c5796

Please sign in to comment.