diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift index 084a085c..8aacff6c 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift @@ -38,7 +38,7 @@ struct SelectBankDialog: View { Form { Section { VStack { - UIKitSearchBar(text: searchTextBinding) // TODO: try to get rid of the two horizontal lines + UIKitSearchBar(text: searchTextBinding, focusOnStart: true) // TODO: try to get rid of the two horizontal lines HStack { Text("Search by bank code, bank name or city") diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift index d8eb46bf..f742ecb6 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift @@ -5,11 +5,37 @@ struct UIKitSearchBar : UIViewRepresentable { @Binding var text : String + private var placeholder: String + + private var focusOnStart = false + + private var actionOnReturnKeyPress: (() -> Bool)? = nil + + private var textChanged: ((String) -> Void)? = nil + + + init(text: Binding, placeholder: String = "", focusOnStart: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { + _text = text + self.placeholder = placeholder + + self.focusOnStart = focusOnStart + + self.actionOnReturnKeyPress = actionOnReturnKeyPress + self.textChanged = textChanged + } + func makeUIView(context: UIViewRepresentableContext) -> UISearchBar { let searchBar = UISearchBar(frame: .zero) + searchBar.placeholder = placeholder.localize() + searchBar.delegate = context.coordinator + searchBar.searchTextField.delegate = context.coordinator + + if focusOnStart { + searchBar.becomeFirstResponder() + } return searchBar } @@ -18,22 +44,48 @@ struct UIKitSearchBar : UIViewRepresentable { uiView.text = text } + func makeCoordinator() -> Cordinator { + return Cordinator(text: $text, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) + } - class Cordinator : NSObject, UISearchBarDelegate { + + class Cordinator : NSObject, UISearchBarDelegate, UITextFieldDelegate { @Binding var text : String - init(text : Binding) { + private var actionOnReturnKeyPress: (() -> Bool)? + + private var textChanged: ((String) -> Void)? + + + init(text: Binding, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { _text = text + + self.actionOnReturnKeyPress = actionOnReturnKeyPress + + self.textChanged = textChanged } + func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { - text = searchText + let newText = searchBar.text ?? "" + let didTextChange = newText != text // e.g. if just the cursor has been placed to another position then textFieldDidChangeSelection() gets called but text didn't change + + DispatchQueue.main.async { // to not update state during view update + self.text = newText + + if didTextChange { + self.textChanged?(newText) + } + } } - } - - func makeCoordinator() -> Cordinator { - return Cordinator(text: $text) + + + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + return actionOnReturnKeyPress?() ?? false + } + + } }