Skip to content

Commit 785c6fb

Browse files
committed
优化自定检索视图
1 parent 84ca76d commit 785c6fb

File tree

6 files changed

+134
-23
lines changed

6 files changed

+134
-23
lines changed

SwiftPamphletApp/App/SwiftPamphletAppConfig.swift

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ struct SPC {
1717

1818
static let detailMinWidth: CGFloat = 550
1919
static let githubHost = "https://github.com/"
20+
21+
// MARK: AppStorage
22+
static let selectedDataLinkString = "selectedDataLinkString"
23+
static let isFirstRun = "isFirstRun"
24+
static let customSearchTerm = "customSearchTerm"
25+
static let isShowInspector = "isShowInspector"
26+
static let inspectorType = "inspectorType"
2027

2128
static func loadCustomIssues(jsonFileName: String) -> [CustomIssuesModel] {
2229
let lc: [CustomIssuesModel] = loadBundleJSONFile(jsonFileName + ".json")

SwiftPamphletApp/HomeUI/HomeView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ struct HomeView: View {
1111
@State private var selectedDataLinkString: String = ""
1212
@State private var selectInfo: IOInfo? = nil
1313
@State private var selectDev: DeveloperModel? = nil
14-
@AppStorage("selectedDataLinkString") var sdLinkStr: String = ""
14+
@AppStorage(SPC.selectedDataLinkString) var sdLinkStr: String = ""
1515

16-
@AppStorage("isFirstRun") var isFirstRun = true
16+
@AppStorage(SPC.isFirstRun) var isFirstRun = true
1717
@Environment(\.scenePhase) var scenePhase
1818

1919
var body: some View {

SwiftPamphletApp/InfoOrganizer/Category/EditCustomSearchView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
struct EditCustomSearchView: View {
11-
@AppStorage("customSearchTerm") var term = ""
11+
@AppStorage(SPC.customSearchTerm) var term = ""
1212

1313
var body: some View {
1414
VStack {

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

+81-2
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ struct EditInfoView: View {
1717

1818
@Query(IOCategory.all) var categories: [IOCategory]
1919

20+
@State private var showSheet = false
21+
2022
// Inspector
2123
@State var isShowInspector = false
2224
enum InspectorType {
2325
case category, customSearch
2426
}
2527
@State var inspectorType: InspectorType = .category
28+
@AppStorage(SPC.isShowInspector) var asIsShowInspector: Bool = false
29+
@AppStorage(SPC.inspectorType) var asInspectorType: Int = 0
2630

2731
// Tab
2832
@State var selectedTab = 1
2933

3034
@State var isStopLoadingWeb = false
3135
// webarchive
3236
@State var savingDataTrigger = false
33-
37+
3438
// 图集
3539
@State var selectedPhotos = [PhotosPickerItem]()
3640
@State var addWebImageUrl = ""
@@ -119,8 +123,35 @@ struct EditInfoView: View {
119123
info.category?.updateDate = Date.now
120124
})
121125
Button("管理分类", action: manageCate)
126+
Button("自定检索") {
127+
showSheet = true
128+
}
129+
.help("command + s")
130+
.sheet(isPresented: $showSheet, content: {
131+
ScrollView(.vertical) {
132+
ForEach(parseSearchTerms(), id: \.self) { term in
133+
HStack {
134+
ForEach(term, id: \.self) { oneTerm in
135+
if oneTerm.description.hasPrefix("") {
136+
Text(oneTerm)
137+
.bold()
138+
} else {
139+
Button(oneTerm) {
140+
showSheet = false
141+
info.des = oneTerm + "\n" + info.des
142+
}
143+
}
144+
}
145+
Spacer()
146+
}
147+
.padding(.leading, 1)
148+
}
149+
}
150+
.padding(20)
151+
})
152+
.keyboardShortcut(KeyEquivalent("s"), modifiers: .command)
153+
122154
Button("管理自定检索", action: manageCustomSearch)
123-
124155
}
125156
}
126157
// MARK: Tab 切换
@@ -277,10 +308,58 @@ struct EditInfoView: View {
277308
isShowInspector.toggle()
278309
}
279310
}
311+
.onAppear(perform: {
312+
_ = parseSearchTerms()
313+
// AppStorage
314+
if asInspectorType == 0 {
315+
inspectorType = .category
316+
} else if asInspectorType == 1 {
317+
inspectorType = .customSearch
318+
}
319+
isShowInspector = asIsShowInspector
320+
})
321+
.onChange(of: term) { oldValue, newValue in
322+
_ = parseSearchTerms()
323+
}
324+
.onChange(of: isShowInspector) { oldValue, newValue in
325+
asIsShowInspector = newValue
326+
}
327+
.onChange(of: inspectorType) { oldValue, newValue in
328+
if newValue == InspectorType.category {
329+
asInspectorType = 0
330+
} else if newValue == InspectorType.customSearch {
331+
asInspectorType = 1
332+
}
333+
}
280334
Spacer()
281335
} // end VStack
282336
}
283337

338+
// MARK: 自定检索
339+
@AppStorage(SPC.customSearchTerm) var term = ""
340+
@State private var searchTerms: [[String]] = [[String]]()
341+
func parseSearchTerms() -> [[String]] {
342+
let terms = term.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n")
343+
var sterms = [[String]]()
344+
for t in terms {
345+
if t.isEmpty == false {
346+
let tWithoutWhitespaces = t.trimmingCharacters(in: .whitespaces)
347+
if tWithoutWhitespaces.hasPrefix("//") { continue }
348+
let ts = t.trimmingCharacters(in: .whitespaces).split(separator: ",")
349+
var lineTs = [String]()
350+
if ts.count > 1 {
351+
for oneT in ts {
352+
lineTs.append(String(oneT.trimmingCharacters(in: .whitespaces)))
353+
}
354+
} else {
355+
lineTs.append(String(tWithoutWhitespaces))
356+
}
357+
sterms.append(lineTs)
358+
} // end if
359+
} // end for
360+
searchTerms = sterms
361+
return sterms
362+
}
284363

285364
// MARK: 数据管理
286365
func tabSwitch() {

SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift

+42-17
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ struct InfoListView: View {
1919
@State var limit: Int = 50
2020
@State var filterStar: Bool = false
2121

22+
@State private var showSheet = false
23+
2224
var body: some View {
2325
InfosView(filterCateName: filterCate, searchString: searchText, filterStar: filterStar, selectInfo: $selectInfo, sortOrder: sortOrder, limit: $limit)
2426
.navigationTitle("资料列表")
2527
.toolbar {
2628
ToolbarItem(placement: .navigation) {
2729
Button("添加资料", systemImage: "plus", action: addInfo)
28-
.keyboardShortcut(KeyEquivalent("i"), modifiers: .command)
30+
.help("command + =")
31+
.keyboardShortcut(KeyEquivalent("="), modifiers: .command)
2932
}
3033
ToolbarItem(placement: .navigation) {
3134
Picker("分类", selection: $filterCate) {
@@ -88,47 +91,69 @@ struct InfoListView: View {
8891
}
8992
.searchable(text: $searchText)
9093
.onAppear {
91-
parseSearchTerms()
94+
_ = parseSearchTerms()
9295
}
9396
.onChange(of: term) { oldValue, newValue in
94-
parseSearchTerms()
97+
_ = parseSearchTerms()
9598
}
9699
}
97100

98101

99102
// MARK: 自定义搜索
100103
@ViewBuilder
101104
func customSearchView() -> some View {
102-
Picker("自定检索", selection: $searchText) {
103-
Text("自定检索")
104-
.tag("")
105-
Divider()
106-
ForEach(searchTerms, id: \.self) { term in
107-
Text(customSearchLabel(term))
108-
.tag(term)
105+
Button(action: {
106+
showSheet = true
107+
}, label: {
108+
Image(systemName: "mail.and.text.magnifyingglass")
109+
})
110+
.sheet(isPresented: $showSheet, content: {
111+
ScrollView(.vertical) {
112+
ForEach(parseSearchTerms(), id: \.self) { term in
113+
HStack {
114+
ForEach(term, id: \.self) { oneTerm in
115+
if oneTerm.description.hasPrefix("") {
116+
Text(oneTerm)
117+
.bold()
118+
} else {
119+
Button(oneTerm) {
120+
showSheet = false
121+
searchText = oneTerm
122+
}
123+
}
124+
}
125+
Spacer()
126+
}
127+
.padding(.leading, 1)
128+
}
109129
}
110-
}
130+
.padding(20)
131+
})
111132
}
112133

113-
@AppStorage("customSearchTerm") var term = ""
114-
@State private var searchTerms: [String] = [String]()
115-
func parseSearchTerms() {
134+
@AppStorage(SPC.customSearchTerm) var term = ""
135+
@State private var searchTerms: [[String]] = [[String]]()
136+
func parseSearchTerms() -> [[String]] {
116137
let terms = term.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n")
117-
searchTerms = [String]()
138+
var sterms = [[String]]()
118139
for t in terms {
119140
if t.isEmpty == false {
120141
let tWithoutWhitespaces = t.trimmingCharacters(in: .whitespaces)
121142
if tWithoutWhitespaces.hasPrefix("//") { continue }
122143
let ts = t.trimmingCharacters(in: .whitespaces).split(separator: ",")
144+
var lineTs = [String]()
123145
if ts.count > 1 {
124146
for oneT in ts {
125-
searchTerms.append(String(oneT.trimmingCharacters(in: .whitespaces)))
147+
lineTs.append(String(oneT.trimmingCharacters(in: .whitespaces)))
126148
}
127149
} else {
128-
searchTerms.append(String(tWithoutWhitespaces))
150+
lineTs.append(String(tWithoutWhitespaces))
129151
}
152+
sterms.append(lineTs)
130153
} // end if
131154
} // end for
155+
searchTerms = sterms
156+
return sterms
132157
}
133158
func customSearchLabel(_ string: String) -> String {
134159
let strs = string.split(separator: "/")

SwiftPamphletApp/Setting/SettingView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct SettingView: View {
2525
}
2626

2727
// MARK: custom search
28-
@AppStorage("customSearchTerm") var term = ""
28+
@AppStorage(SPC.customSearchTerm) var term = ""
2929
@ViewBuilder
3030
func customSearch() -> some View {
3131
VStack {

0 commit comments

Comments
 (0)