forked from mazzzystar/Queryable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchBarView.swift
More file actions
73 lines (64 loc) · 2.61 KB
/
SearchBarView.swift
File metadata and controls
73 lines (64 loc) · 2.61 KB
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
//
// SearchBarView.swift
// Queryable
//
// Created by Ke Fang on 2022/12/14.
//
import SwiftUI
struct SearchBarView: View {
@FocusState private var inputFocused: Bool
@ObservedObject var photoSearcher: PhotoSearcher
@State var searchText: String = ""
private let showString = ["My love", "Dark night room with a lamp", "Snow outside the window", "Deep blue", "Cute kitten", "Photos of our gathering", "Beach, waves, sunset", "In car view, car on the road", "Screen display of traffic info", "Selfie in front of mirror", "Cheers"].randomElement()
var body: some View {
HStack {
Image(systemName: "magnifyingglass")
.accessibilityAddTraits(.isImage)
.accessibilityHint(Text("An icon, no actual usage"))
// TextField("Input here to search Photos", text: $searchText)
TextField("\"\(NSLocalizedString(showString ?? "My love", comment: ""))\"", text: $searchText)
// TextField("Input text here, e.g. \"We fell in love\"", text: $searchText)
.modifier(TextFieldClearButton(photoSearcher: photoSearcher, inputFocused: $inputFocused, text: $searchText))
.multilineTextAlignment(.leading)
.focused($inputFocused)
.accessibilityAddTraits(.isSearchField)
.accessibilityLabel(Text("Text Field"))
.accessibilityHint(Text("Input your sentences here, then press enter"))
.onSubmit {
print("Searching...")
Task {
await photoSearcher.search(with: searchText)
}
}
.submitLabel(.search)
}
}
}
struct TextFieldClearButton: ViewModifier {
@ObservedObject var photoSearcher: PhotoSearcher
var inputFocused: FocusState<Bool>.Binding
@Binding var text: String
func body(content: Content) -> some View {
HStack {
content
if !text.isEmpty {
Button(
action: {
self.text = ""
inputFocused.wrappedValue = true
photoSearcher.searchResultCode = .MODEL_PREPARED
},
label: {
Image(systemName: "delete.left")
.foregroundColor(Color(UIColor.opaqueSeparator))
}
)
}
}
}
}
struct SearchBarView_Previews: PreviewProvider {
static var previews: some View {
SearchBarView(photoSearcher: PhotoSearcher())
}
}