1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 14:20:32 +05:30
yattee/Shared/Search/SearchSuggestions.swift

104 lines
3.1 KiB
Swift
Raw Normal View History

2021-11-28 20:07:55 +05:30
import SwiftUI
struct SearchSuggestions: View {
@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")
Text(state.queryText)
.lineLimit(1)
}
2021-11-28 20:07:55 +05:30
}
2022-08-17 21:04:25 +05:30
.padding(.vertical, 5)
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
HStack {
Button {
runQueryAction(suggestion)
} label: {
HStack {
Image(systemName: "magnifyingglass")
HStack(spacing: 0) {
2022-12-11 18:58:16 +05:30
Text(suggestion)
.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())
}
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)
.buttonStyle(.plain)
Spacer()
Button {
2022-08-17 21:04:25 +05:30
state.queryText = suggestion
} 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
}
}
2022-08-17 21:04:25 +05:30
#if os(iOS)
.padding(.bottom, 90)
#endif
2021-11-28 20:07:55 +05:30
#if os(macOS)
2022-08-17 21:04:25 +05:30
.buttonStyle(.link)
2021-11-28 20:07:55 +05:30
#endif
}
private func runQueryAction(_ queryText: String) {
2022-08-17 21:04:25 +05:30
state.queryText = queryText
state.changeQuery { query in
query.query = queryText
NavigationModel.shared.hideKeyboard()
}
RecentsModel.shared.addQuery(queryText)
}
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()
}
}