-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSearchBarView.swift
More file actions
112 lines (103 loc) · 4.02 KB
/
SearchBarView.swift
File metadata and controls
112 lines (103 loc) · 4.02 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
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
//
// SearchBarView.swift
// berkeley-mobile
//
// Created by Baurzhan on 3/6/25.
// Copyright © 2025 ASUC OCTO. All rights reserved.
//
import MapKit
import SwiftUI
struct SearchBarView: View {
@EnvironmentObject var viewModel: SearchViewModel
@FocusState private var isFocused: Bool
@State private var isPresentingFeedbackForm = false
private var onSearchBarTap: ((Bool) -> Void)?
var body: some View {
HStack {
searchOrBackIcon
TextField("What are you looking for?", text: $viewModel.searchText)
.focused($isFocused)
.autocorrectionDisabled()
.onChange(of: viewModel.searchText) { _ in
viewModel.searchLocations(viewModel.searchText)
}
if viewModel.searchText.isEmpty {
Button(action: {
isPresentingFeedbackForm = true
}) {
Image(systemName: "bubble.left.and.bubble.right")
.foregroundStyle(Color(BMColor.searchBarIconColor))
.fontWeight(.semibold)
.frame(width: 30, alignment: .center)
}
.accessibilityLabel("Send Feedback")
} else {
Button(action: {
viewModel.clearSearchText()
}) {
Image(systemName: "xmark")
.foregroundStyle(Color(BMColor.searchBarIconColor))
.fontWeight(.semibold)
.frame(width: 30, alignment: .center)
}
.accessibilityLabel("Clear search text")
}
}
.padding()
.modify {
if #available(iOS 26.0, *) {
$0.glassEffect(.regular.interactive(), in: .rect(cornerRadius: 15))
} else {
$0.background(.regularMaterial)
}
}
.clipShape(.rect(cornerRadius: 15))
.shadow(color: .black.opacity(0.3), radius: 8)
.onChange(of: isFocused) { newValue in // onChange syntax will need to change in later iOS
switch viewModel.state {
case .populated,
.idle where (!viewModel.recentSearches.isEmpty && viewModel.isSearching):
/*
.scrollDismissesKeyboard(.immediately) in SearchResultsView
triggers FocusState change by default.
To keep searching state active when we want to scroll through
the list of search items (case = .populated) or recent search
items (.idle + !viewModel.recentSearch.isEmpty) we pass through
this FocusState change.
viewModel.isSearching condition is needed for scenario when
searchBar is initally tapped from the HomeView.
*/
break
default:
viewModel.isSearching = newValue
}
}
.onChange(of: viewModel.isSearching) { newValue in
isFocused = newValue
onSearchBarTap?(newValue)
}
.sheet(isPresented: $isPresentingFeedbackForm) { FeedbackFormView() }
}
private var searchOrBackIcon: some View {
Button(action: {
if viewModel.isSearching {
viewModel.clearSearchText()
}
viewModel.isSearching.toggle()
}) {
Image(systemName: viewModel.isSearching ? "chevron.left" : "magnifyingglass")
.foregroundStyle(Color(BMColor.searchBarIconColor))
.fontWeight(.semibold)
.frame(width: 20, alignment: .center) // So that changing an icon didn't move the TextField
}
}
init(onSearchBarTap: ((Bool) -> Void)? = nil) {
self.onSearchBarTap = onSearchBarTap
}
}
#Preview {
SearchBarView()
.padding()
.ignoresSafeArea()
.environmentObject(SearchViewModel(chooseMapMarker: { _ in }) { _ in })
}