Improve search on the share extension

This commit is contained in:
Jackson Harper 2023-01-23 10:19:22 +08:00
parent 8e1b4965dc
commit a3e217f2f3
4 changed files with 62 additions and 57 deletions

View file

@ -140,28 +140,6 @@ public struct ShareExtensionView: View {
}
}
var searchButton: some View {
HStack {
Image(systemName: "magnifyingglass")
.resizable()
.frame(width: 15, height: 15)
.foregroundColor(.appGrayText)
.padding(.leading, 10)
Text("Search labels")
.font(Font.system(size: 15))
.foregroundColor(.appGrayText)
Spacer()
}
.frame(height: 36)
.frame(maxWidth: .infinity)
.background(
Color.appButtonBackground
.cornerRadius(8)
)
}
var labelsSection: some View {
HStack {
if viewState != .editingLabels {
@ -197,12 +175,11 @@ public struct ShareExtensionView: View {
.font(.appCallout)
} else {
VStack(spacing: 15) {
searchButton
.onTapGesture { showSearchLabels = true }
SearchBar(searchTerm: $labelsViewModel.labelSearchFilter)
ScrollView {
LabelsMasonaryView(labels: labelsViewModel.labels,
selectedLabels: labelsViewModel.selectedLabels,
LabelsMasonaryView(labels: labelsViewModel.labels.applySearchFilter(labelsViewModel.labelSearchFilter),
selectedLabels: labelsViewModel.selectedLabels.applySearchFilter(labelsViewModel.labelSearchFilter),
onLabelTap: onLabelTap)
Spacer()
}
@ -384,22 +361,6 @@ public struct ShareExtensionView: View {
.padding(.bottom, 12)
} else {
ZStack {
Button(action: {
withAnimation {
if viewState == .editingLabels {
if let linkedItem = self.viewModel.linkedItem {
self.labelsViewModel.selectedLabels = previousLabels ?? []
self.labelsViewModel.saveItemLabelChanges(itemID: linkedItem.unwrappedID,
dataService: self.viewModel.services.dataService)
}
}
viewState = .mainView
}
}, label: { Text(LocalText.cancelGeneric) })
.frame(maxWidth: .infinity, alignment: .leading)
.opacity(viewState == .viewingHighlight ? 0.0 : 1.0)
// Don't show viewState when viewing the highlight
Text(editingViewTitle).bold()
.frame(maxWidth: .infinity, alignment: .center)

View file

@ -50,7 +50,9 @@ struct ApplyLabelsView: View {
var innerBody: some View {
List {
Section(header: Spacer(minLength: 0)) {
SearchBar(searchTerm: $viewModel.labelSearchFilter, initialFocus: isSearchFocused)
SearchBar(searchTerm: $viewModel.labelSearchFilter)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
.listRowBackground(Color.clear)
}
Section {
Button(
@ -85,6 +87,7 @@ struct ApplyLabelsView: View {
}
}
)
.listRowInsets(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
#if os(macOS)
.buttonStyle(PlainButtonStyle())
#endif

View file

@ -15,7 +15,30 @@
]
if let heightRatio = heightRatio {
constraints.append(child.view.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: heightRatio))
let constraint = child.view.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: heightRatio)
constraints.append(constraint)
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: OperationQueue.main) { _ in
UIView.animate(withDuration: 0.2) {
if let parent = self.parent, let frame = constraint.firstItem?.frame {
constraint.constant = parent.view.frame.height - frame.height - 10
}
child.view.setNeedsLayout()
child.view.layoutIfNeeded()
}
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: OperationQueue.main) { _ in
UIView.animate(withDuration: 0.2) {
constraint.constant = 0
child.view.setNeedsLayout()
child.view.layoutIfNeeded()
}
}
} else {
constraints.append(child.view.topAnchor.constraint(equalTo: view.topAnchor))
}
@ -24,6 +47,23 @@
child.didMove(toParent: self)
}
//
// @objc func keyboardWillShow(notification: Notification) {
// if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
// if self.view.frame.origin.y == 0{
// self.view.frame.origin.y -= keyboardSize.height
// }
// }
//
// }
//
// @objc func keyboardWillHide(notification: Notification) {
// if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
// if self.view.frame.origin.y != 0 {
// self.view.frame.origin.y += keyboardSize.height
// }
// }
// }
}
#endif

View file

@ -3,14 +3,11 @@ import SwiftUI
public struct SearchBar: View {
@Binding var searchTerm: String
@FocusState private var isFocused: Bool
@State private var initialFocus: Bool
public init(
searchTerm: Binding<String>,
initialFocus: Bool
searchTerm: Binding<String>
) {
self._searchTerm = searchTerm
self.initialFocus = initialFocus
}
public var body: some View {
@ -18,17 +15,16 @@ public struct SearchBar: View {
TextField("Search", text: $searchTerm)
.frame(height: 36)
.frame(maxWidth: .infinity)
.background(Color.appButtonBackground)
.cornerRadius(8)
.padding(.leading, 28)
.padding(.trailing, 28)
.focused($isFocused)
.padding(.leading, 24)
.overlay(
HStack {
Image(systemName: "magnifyingglass")
.resizable()
.frame(width: 15, height: 15)
.frame(width: 14, height: 14)
.foregroundColor(.appGrayText)
.padding(.leading, 2)
.padding(.leading, 8)
Spacer()
}
@ -37,7 +33,6 @@ public struct SearchBar: View {
if isFocused {
Button(
action: {
self.searchTerm = ""
self.isFocused = false
},
label: {
@ -45,11 +40,17 @@ public struct SearchBar: View {
.foregroundColor(.gray)
}
)
.padding(.trailing, 0)
.padding(.trailing, 8)
.transition(.move(edge: .trailing))
}
}.onAppear {
self.isFocused = initialFocus
}
.background(Color.appButtonBackground)
.cornerRadius(8)
.frame(height: 36)
.onChange(of: isFocused) { isFocused in
if !isFocused {
searchTerm = ""
}
}
}
}