Skip to content

Commit

Permalink
优化自定检索视图
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Apr 13, 2024
1 parent 84ca76d commit 785c6fb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 23 deletions.
7 changes: 7 additions & 0 deletions SwiftPamphletApp/App/SwiftPamphletAppConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ struct SPC {

static let detailMinWidth: CGFloat = 550
static let githubHost = "https://github.com/"

// MARK: AppStorage
static let selectedDataLinkString = "selectedDataLinkString"
static let isFirstRun = "isFirstRun"
static let customSearchTerm = "customSearchTerm"
static let isShowInspector = "isShowInspector"
static let inspectorType = "inspectorType"

static func loadCustomIssues(jsonFileName: String) -> [CustomIssuesModel] {
let lc: [CustomIssuesModel] = loadBundleJSONFile(jsonFileName + ".json")
Expand Down
4 changes: 2 additions & 2 deletions SwiftPamphletApp/HomeUI/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ struct HomeView: View {
@State private var selectedDataLinkString: String = ""
@State private var selectInfo: IOInfo? = nil
@State private var selectDev: DeveloperModel? = nil
@AppStorage("selectedDataLinkString") var sdLinkStr: String = ""
@AppStorage(SPC.selectedDataLinkString) var sdLinkStr: String = ""

@AppStorage("isFirstRun") var isFirstRun = true
@AppStorage(SPC.isFirstRun) var isFirstRun = true
@Environment(\.scenePhase) var scenePhase

var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct EditCustomSearchView: View {
@AppStorage("customSearchTerm") var term = ""
@AppStorage(SPC.customSearchTerm) var term = ""

var body: some View {
VStack {
Expand Down
83 changes: 81 additions & 2 deletions SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ struct EditInfoView: View {

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

@State private var showSheet = false

// Inspector
@State var isShowInspector = false
enum InspectorType {
case category, customSearch
}
@State var inspectorType: InspectorType = .category
@AppStorage(SPC.isShowInspector) var asIsShowInspector: Bool = false
@AppStorage(SPC.inspectorType) var asInspectorType: Int = 0

// Tab
@State var selectedTab = 1

@State var isStopLoadingWeb = false
// webarchive
@State var savingDataTrigger = false

// 图集
@State var selectedPhotos = [PhotosPickerItem]()
@State var addWebImageUrl = ""
Expand Down Expand Up @@ -119,8 +123,35 @@ struct EditInfoView: View {
info.category?.updateDate = Date.now
})
Button("管理分类", action: manageCate)
Button("自定检索") {
showSheet = true
}
.help("command + s")
.sheet(isPresented: $showSheet, content: {
ScrollView(.vertical) {
ForEach(parseSearchTerms(), id: \.self) { term in
HStack {
ForEach(term, id: \.self) { oneTerm in
if oneTerm.description.hasPrefix("") {
Text(oneTerm)
.bold()
} else {
Button(oneTerm) {
showSheet = false
info.des = oneTerm + "\n" + info.des
}
}
}
Spacer()
}
.padding(.leading, 1)
}
}
.padding(20)
})
.keyboardShortcut(KeyEquivalent("s"), modifiers: .command)

Button("管理自定检索", action: manageCustomSearch)

}
}
// MARK: Tab 切换
Expand Down Expand Up @@ -277,10 +308,58 @@ struct EditInfoView: View {
isShowInspector.toggle()
}
}
.onAppear(perform: {
_ = parseSearchTerms()
// AppStorage
if asInspectorType == 0 {
inspectorType = .category
} else if asInspectorType == 1 {
inspectorType = .customSearch
}
isShowInspector = asIsShowInspector
})
.onChange(of: term) { oldValue, newValue in
_ = parseSearchTerms()
}
.onChange(of: isShowInspector) { oldValue, newValue in
asIsShowInspector = newValue
}
.onChange(of: inspectorType) { oldValue, newValue in
if newValue == InspectorType.category {
asInspectorType = 0
} else if newValue == InspectorType.customSearch {
asInspectorType = 1
}
}
Spacer()
} // end VStack
}

// MARK: 自定检索
@AppStorage(SPC.customSearchTerm) var term = ""
@State private var searchTerms: [[String]] = [[String]]()
func parseSearchTerms() -> [[String]] {
let terms = term.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n")
var sterms = [[String]]()
for t in terms {
if t.isEmpty == false {
let tWithoutWhitespaces = t.trimmingCharacters(in: .whitespaces)
if tWithoutWhitespaces.hasPrefix("//") { continue }
let ts = t.trimmingCharacters(in: .whitespaces).split(separator: ",")
var lineTs = [String]()
if ts.count > 1 {
for oneT in ts {
lineTs.append(String(oneT.trimmingCharacters(in: .whitespaces)))
}
} else {
lineTs.append(String(tWithoutWhitespaces))
}
sterms.append(lineTs)
} // end if
} // end for
searchTerms = sterms
return sterms
}

// MARK: 数据管理
func tabSwitch() {
Expand Down
59 changes: 42 additions & 17 deletions SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ struct InfoListView: View {
@State var limit: Int = 50
@State var filterStar: Bool = false

@State private var showSheet = false

var body: some View {
InfosView(filterCateName: filterCate, searchString: searchText, filterStar: filterStar, selectInfo: $selectInfo, sortOrder: sortOrder, limit: $limit)
.navigationTitle("资料列表")
.toolbar {
ToolbarItem(placement: .navigation) {
Button("添加资料", systemImage: "plus", action: addInfo)
.keyboardShortcut(KeyEquivalent("i"), modifiers: .command)
.help("command + =")
.keyboardShortcut(KeyEquivalent("="), modifiers: .command)
}
ToolbarItem(placement: .navigation) {
Picker("分类", selection: $filterCate) {
Expand Down Expand Up @@ -88,47 +91,69 @@ struct InfoListView: View {
}
.searchable(text: $searchText)
.onAppear {
parseSearchTerms()
_ = parseSearchTerms()
}
.onChange(of: term) { oldValue, newValue in
parseSearchTerms()
_ = parseSearchTerms()
}
}


// MARK: 自定义搜索
@ViewBuilder
func customSearchView() -> some View {
Picker("自定检索", selection: $searchText) {
Text("自定检索")
.tag("")
Divider()
ForEach(searchTerms, id: \.self) { term in
Text(customSearchLabel(term))
.tag(term)
Button(action: {
showSheet = true
}, label: {
Image(systemName: "mail.and.text.magnifyingglass")
})
.sheet(isPresented: $showSheet, content: {
ScrollView(.vertical) {
ForEach(parseSearchTerms(), id: \.self) { term in
HStack {
ForEach(term, id: \.self) { oneTerm in
if oneTerm.description.hasPrefix("") {
Text(oneTerm)
.bold()
} else {
Button(oneTerm) {
showSheet = false
searchText = oneTerm
}
}
}
Spacer()
}
.padding(.leading, 1)
}
}
}
.padding(20)
})
}

@AppStorage("customSearchTerm") var term = ""
@State private var searchTerms: [String] = [String]()
func parseSearchTerms() {
@AppStorage(SPC.customSearchTerm) var term = ""
@State private var searchTerms: [[String]] = [[String]]()
func parseSearchTerms() -> [[String]] {
let terms = term.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n")
searchTerms = [String]()
var sterms = [[String]]()
for t in terms {
if t.isEmpty == false {
let tWithoutWhitespaces = t.trimmingCharacters(in: .whitespaces)
if tWithoutWhitespaces.hasPrefix("//") { continue }
let ts = t.trimmingCharacters(in: .whitespaces).split(separator: ",")
var lineTs = [String]()
if ts.count > 1 {
for oneT in ts {
searchTerms.append(String(oneT.trimmingCharacters(in: .whitespaces)))
lineTs.append(String(oneT.trimmingCharacters(in: .whitespaces)))
}
} else {
searchTerms.append(String(tWithoutWhitespaces))
lineTs.append(String(tWithoutWhitespaces))
}
sterms.append(lineTs)
} // end if
} // end for
searchTerms = sterms
return sterms
}
func customSearchLabel(_ string: String) -> String {
let strs = string.split(separator: "/")
Expand Down
2 changes: 1 addition & 1 deletion SwiftPamphletApp/Setting/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct SettingView: View {
}

// MARK: custom search
@AppStorage("customSearchTerm") var term = ""
@AppStorage(SPC.customSearchTerm) var term = ""
@ViewBuilder
func customSearch() -> some View {
VStack {
Expand Down

0 comments on commit 785c6fb

Please sign in to comment.