2021-11-28 20:07:55 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct SearchSuggestions: View {
|
2022-11-25 02:06:05 +05:30
|
|
|
@ObservedObject private var state = SearchModel.shared
|
2021-11-28 20:07:55 +05:30
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
List {
|
2022-08-17 21:04:25 +05:30
|
|
|
if !state.queryText.isEmpty {
|
|
|
|
Button {
|
|
|
|
runQueryAction(state.queryText)
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Image(systemName: "magnifyingglass")
|
2023-09-24 14:44:11 +05:30
|
|
|
Text(state.queryText.trimmingCharacters(in: .whitespacesAndNewlines))
|
2022-08-17 21:04:25 +05:30
|
|
|
.lineLimit(1)
|
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
2022-08-17 21:04:25 +05:30
|
|
|
.padding(.vertical, 5)
|
2022-01-03 01:02:18 +05:30
|
|
|
|
2022-08-17 21:04:25 +05:30
|
|
|
#if os(macOS)
|
|
|
|
.onHover(perform: onHover(_:))
|
|
|
|
#endif
|
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
|
|
|
|
ForEach(visibleSuggestions, id: \.self) { suggestion in
|
2022-01-03 01:02:18 +05:30
|
|
|
HStack {
|
|
|
|
Button {
|
2022-08-05 04:00:09 +05:30
|
|
|
runQueryAction(suggestion)
|
2022-01-03 01:02:18 +05:30
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Image(systemName: "magnifyingglass")
|
|
|
|
HStack(spacing: 0) {
|
2022-12-11 18:58:16 +05:30
|
|
|
Text(suggestion)
|
2022-01-03 01:02:18 +05:30
|
|
|
.lineLimit(1)
|
|
|
|
.layoutPriority(1)
|
2022-12-11 18:58:16 +05:30
|
|
|
.truncationMode(.head)
|
2022-01-05 04:04:09 +05:30
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
|
|
|
.contentShape(Rectangle())
|
2022-01-03 01:02:18 +05:30
|
|
|
}
|
2021-12-06 23:42:33 +05:30
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
2022-01-05 04:04:09 +05:30
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
2022-01-03 01:02:18 +05:30
|
|
|
.buttonStyle(.plain)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button {
|
2022-08-17 21:04:25 +05:30
|
|
|
state.queryText = suggestion
|
2022-01-03 01:02:18 +05:30
|
|
|
} label: {
|
|
|
|
Image(systemName: "arrow.up.left.circle")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
.padding(.horizontal, 10)
|
|
|
|
.padding(.vertical, 5)
|
|
|
|
.buttonStyle(.plain)
|
2021-11-28 20:07:55 +05:30
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
.onHover(perform: onHover(_:))
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if os(macOS)
|
2022-08-17 21:04:25 +05:30
|
|
|
.buttonStyle(.link)
|
2021-11-28 20:07:55 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-05 04:00:09 +05:30
|
|
|
private func runQueryAction(_ queryText: String) {
|
2022-08-17 21:04:25 +05:30
|
|
|
state.queryText = queryText
|
2022-08-05 04:00:09 +05:30
|
|
|
|
2022-01-03 01:02:18 +05:30
|
|
|
state.changeQuery { query in
|
2022-08-05 04:00:09 +05:30
|
|
|
query.query = queryText
|
2022-11-25 02:06:05 +05:30
|
|
|
NavigationModel.shared.hideKeyboard()
|
2022-01-03 01:02:18 +05:30
|
|
|
}
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
RecentsModel.shared.addQuery(queryText)
|
2022-01-03 01:02:18 +05:30
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var visibleSuggestions: [String] {
|
2022-08-17 21:04:25 +05:30
|
|
|
state.querySuggestions.filter {
|
2021-11-28 20:07:55 +05:30
|
|
|
$0.compare(state.queryText, options: .caseInsensitive) != .orderedSame
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
private func onHover(_ inside: Bool) {
|
|
|
|
if inside {
|
|
|
|
NSCursor.pointingHand.push()
|
|
|
|
} else {
|
|
|
|
NSCursor.pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SearchSuggestions_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
SearchSuggestions()
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|