-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathQuerySuggestionsDemoView.swift
159 lines (131 loc) · 3.77 KB
/
QuerySuggestionsDemoView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// QuerySuggestionsDemoView.swift
// QuerySuggestions
//
// Created by Vladislav Fitc on 02/06/2023.
//
import Foundation
import SwiftUI
import InstantSearchCore
import InstantSearchSwiftUI
struct Item: Codable {
let name: String
let image: URL
}
struct ItemHitRow: View {
let itemHit: Hit<Item>
init(_ itemHit: Hit<Item>) {
self.itemHit = itemHit
}
var body: some View {
HStack(spacing: 14) {
AsyncImage(url: itemHit.object.image, content: { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
}, placeholder: {
ProgressView()
})
.frame(width: 40, height: 40)
if let highlightedName = itemHit.hightlightedString(forKey: "name") {
Text(highlightedString: highlightedName,
highlighted: { Text($0).bold() })
} else {
Text(itemHit.object.name)
}
Spacer()
}
}
}
final class SearchViewModel: ObservableObject {
@Published var searchQuery: String {
didSet {
notifyQueryChanged()
}
}
@Published var suggestions: [QuerySuggestion]
var hits: PaginatedDataViewModel<AlgoliaHitsPage<Hit<Item>>>
private var itemsSearcher: HitsSearcher
private var suggestionsSearcher: HitsSearcher
private var didSubmitSuggestion: Bool
init() {
let appID: ApplicationID = "latency"
let apiKey: APIKey = "af044fb0788d6bb15f807e4420592bc5"
let itemsSearcher = HitsSearcher(appID: appID,
apiKey: apiKey,
indexName: "instant_search")
self.itemsSearcher = itemsSearcher
self.suggestionsSearcher = HitsSearcher(appID: appID,
apiKey: apiKey,
indexName: "query_suggestions")
self.hits = itemsSearcher.paginatedData(of: Hit<Item>.self)
searchQuery = ""
suggestions = []
didSubmitSuggestion = false
suggestionsSearcher.onResults.subscribe(with: self) { _, response in
do {
self.suggestions = try response.extractHits()
} catch _ {
self.suggestions = []
}
}.onQueue(.main)
suggestionsSearcher.search()
}
func completeSuggestion(_ suggestion: String) {
searchQuery = suggestion
}
func submitSuggestion(_ suggestion: String) {
didSubmitSuggestion = true
searchQuery = suggestion
}
func submitSearch() {
suggestions = []
itemsSearcher.request.query.query = searchQuery
itemsSearcher.search()
}
private func notifyQueryChanged() {
if didSubmitSuggestion {
didSubmitSuggestion = false
submitSearch()
} else {
suggestionsSearcher.request.query.query = searchQuery
suggestionsSearcher.search()
itemsSearcher.request.query.query = searchQuery
itemsSearcher.search()
}
}
deinit {
suggestionsSearcher.onResults.cancelSubscription(for: self)
}
}
public struct SearchView: View {
@StateObject var viewModel = SearchViewModel()
public var body: some View {
InfiniteList(viewModel.hits, itemView: { hit in
ItemHitRow(hit)
.padding()
Divider()
}, noResults: {
Text("No results found")
})
.navigationTitle("Query suggestions")
.searchable(text: $viewModel.searchQuery,
prompt: "Laptop, smartphone, tv",
suggestions: {
ForEach(viewModel.suggestions, id: \.query) { suggestion in
SuggestionRow(suggestion: suggestion,
onSelection: viewModel.submitSuggestion,
onTypeAhead: viewModel.completeSuggestion)
}
})
.onSubmit(of: .search, viewModel.submitSearch)
}
}
@available(iOS 15.0, *)
class SearchPreview: PreviewProvider {
static var previews: some View {
NavigationView {
SearchView()
}
}
}