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

116 lines
3.7 KiB
Swift
Raw Normal View History

2021-11-28 20:07:55 +05:30
import SwiftUI
struct SearchSuggestions: View {
@EnvironmentObject<NavigationModel> private var navigation
2021-11-28 20:07:55 +05:30
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var state
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-01-09 20:17:48 +05:30
if suggestion.hasPrefix(state.suggestionsText.lowercased()) {
Text(state.suggestionsText.lowercased())
2022-01-09 20:35:05 +05:30
.lineLimit(1)
.layoutPriority(2)
.foregroundColor(.secondary)
2022-01-09 20:17:48 +05:30
}
2021-11-28 20:07:55 +05:30
Text(querySuffix(suggestion))
.lineLimit(1)
.layoutPriority(1)
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
2022-03-26 19:52:29 +05:30
navigation.hideKeyboard()
}
recents.addQuery(queryText, navigation: navigation)
}
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
}
}
private func querySuffix(_ suggestion: String) -> String {
2021-12-06 23:42:33 +05:30
suggestion.replacingFirstOccurrence(of: state.suggestionsText.lowercased(), with: "")
2021-11-28 20:07:55 +05:30
}
#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()
}
}