diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift index 5bd10352..67068ade 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift @@ -24,6 +24,8 @@ struct LabelledUIKitTextField: View { ) } + var isFocussedChanged: ((Bool) -> Void)? = nil + var isUserInputEnabled: Bool = true @@ -45,7 +47,7 @@ struct LabelledUIKitTextField: View { UIKitTextField(placeholder, text: $text, keyboardType: keyboardType, isPasswordField: isPasswordField, focusOnStart: focusOnStart, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, focusTextField: focusTextFieldBinding, - textAlignment: .right, isUserInputEnabled: isUserInputEnabled, + isFocussedChanged: isFocussedChanged, textAlignment: .right, isUserInputEnabled: isUserInputEnabled, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) } } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift index 86eb5cdf..e59f5fcb 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift @@ -17,6 +17,8 @@ struct UIKitTextField: UIViewRepresentable { private var focusNextTextFieldOnReturnKeyPress = false @Binding private var focusTextField: Bool + private var isFocussedChanged: ((Bool) -> Void)? = nil + private var textAlignment: NSTextAlignment = .natural private var isUserInputEnabled: Bool = true @@ -27,6 +29,7 @@ struct UIKitTextField: UIViewRepresentable { init(_ titleKey: String, text: Binding, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false, focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, focusTextField: Binding = .constant(false), + isFocussedChanged: ((Bool) -> Void)? = nil, textAlignment: NSTextAlignment = .natural, isUserInputEnabled: Bool = true, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { self.placeholder = titleKey @@ -38,6 +41,7 @@ struct UIKitTextField: UIViewRepresentable { self.focusOnStart = focusOnStart self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress self._focusTextField = focusTextField + self.isFocussedChanged = isFocussedChanged self.textAlignment = textAlignment self.isUserInputEnabled = isUserInputEnabled @@ -84,8 +88,8 @@ struct UIKitTextField: UIViewRepresentable { func makeCoordinator() -> UIKitTextField.Coordinator { - return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, isUserInputEnabled: isUserInputEnabled, - actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) + return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, isFocussedChanged: isFocussedChanged, + isUserInputEnabled: isUserInputEnabled, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) } @@ -95,6 +99,8 @@ struct UIKitTextField: UIViewRepresentable { private var focusNextTextFieldOnReturnKeyPress: Bool + private var isFocussedChanged: ((Bool) -> Void)? = nil + private var isUserInputEnabled: Bool private var actionOnReturnKeyPress: (() -> Bool)? @@ -102,11 +108,12 @@ struct UIKitTextField: UIViewRepresentable { private var textChanged: ((String) -> Void)? - init(text: Binding, focusNextTextFieldOnReturnKeyPress: Bool, isUserInputEnabled: Bool, + init(text: Binding, focusNextTextFieldOnReturnKeyPress: Bool, isFocussedChanged: ((Bool) -> Void)? = nil, isUserInputEnabled: Bool, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { _text = text self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress + self.isFocussedChanged = isFocussedChanged self.isUserInputEnabled = isUserInputEnabled self.actionOnReturnKeyPress = actionOnReturnKeyPress @@ -116,8 +123,16 @@ struct UIKitTextField: UIViewRepresentable { func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { + if isUserInputEnabled { + isFocussedChanged?(true) + } + return isUserInputEnabled } + + func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) { + isFocussedChanged?(false) + } func textFieldDidChangeSelection(_ textField: UITextField) { let newText = textField.text ?? ""