From e7127aa88b2bf3c23d68e911ae08cb9b35daed2b Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 29 Jul 2020 12:38:15 +0200 Subject: [PATCH] Implemented UIKitTextField to be able to react to Return key presses --- .../BankingiOSApp/ui/SwiftExtensions.swift | 5 ++ .../BankingiOSApp/ui/UIKitTextField.swift | 86 +++++++++++++++++++ .../ui/views/AddAccountDialog.swift | 12 ++- .../ui/views/EnterTanDialog.swift | 6 +- .../ui/views/TransferMoneyDialog.swift | 26 ++++-- 5 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift b/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift index fd61390d..6dbd6f68 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift @@ -15,6 +15,11 @@ extension String { return !isBlank } + + func localize() -> String { + return NSLocalizedString(self, comment: "") + } + } extension Optional where Wrapped == String { diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift new file mode 100644 index 00000000..9c6e2fca --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift @@ -0,0 +1,86 @@ +import SwiftUI + + +struct UIKitTextField: UIViewRepresentable { + + @Binding private var text: String + + private var placeHolder: String + + private var keyboardType: UIKeyboardType = .default + private var isPasswordField: Bool = false + + private var actionOnReturnKeyPress: (() -> Void)? = nil + + init(_ titleKey: String, text: Binding, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false, actionOnReturnKeyPress: (() -> Void)? = nil) { + self.placeHolder = titleKey + _text = text + + self.keyboardType = keyboardType + self.isPasswordField = isPasswordField + + self.actionOnReturnKeyPress = actionOnReturnKeyPress + } + + + func makeUIView(context: UIViewRepresentableContext) -> UITextField { + let textField = UITextField(frame: .zero) + + textField.placeholder = placeHolder.localize() + + textField.keyboardType = keyboardType + textField.isSecureTextEntry = isPasswordField + + textField.delegate = context.coordinator + + return textField + } + + func makeCoordinator() -> UIKitTextField.Coordinator { + return Coordinator(text: $text, actionOnReturnKeyPress: actionOnReturnKeyPress /*, nextResponder: $nextResponder, isResponder: $isResponder */) + } + + func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext) { + uiView.text = text + } + + + class Coordinator: NSObject, UITextFieldDelegate { + + @Binding private var text: String + + private var actionOnReturnKeyPress: (() -> Void)? + + + init(text: Binding, actionOnReturnKeyPress: (() -> Void)? = nil) { + _text = text + + self.actionOnReturnKeyPress = actionOnReturnKeyPress + } + + func textFieldDidChangeSelection(_ textField: UITextField) { + text = textField.text ?? "" + } + + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + actionOnReturnKeyPress?() + + textField.resignFirstResponder() + + return actionOnReturnKeyPress != nil + } + + } + +} + + +struct UIKitTextView_Previews: PreviewProvider { + + @State static private var text = "" + + static var previews: some View { + UIKitTextField("Test label", text: $text) + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift index b45f84d2..f817c2a0 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift @@ -37,9 +37,17 @@ struct AddAccountDialog: View { } Section { - TextField("Customer ID", text: $customerId) + UIKitTextField("Customer ID", text: $customerId) { + if self.isRequiredDataEntered() { + self.addAccount() + } + } - SecureField("Password", text: $password) + UIKitTextField("Password", text: $password, isPasswordField: true) { + if self.isRequiredDataEntered() { + self.addAccount() + } + } } Section { diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift index 4839ba14..6a438f87 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift @@ -128,7 +128,11 @@ struct EnterTanDialog: View { .padding(.vertical, 2) Section { - TextField("Enter TAN:", text: $enteredTan) + UIKitTextField("Enter TAN:", text: $enteredTan) { + if self.enteredTan.isNotBlank { + self.enteringTanDone() + } + } } Section { diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift index fa176b1a..0cad63aa 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift @@ -90,7 +90,11 @@ struct TransferMoneyDialog: View { } Section { - TextField("Remittee Name", text: $remitteeName) + UIKitTextField("Remittee Name", text: $remitteeName) { + if self.isRequiredDataEntered() { + self.transferMoney() + } + } .onReceive(Just(remitteeName)) { newValue in self.isValidRemitteeNameEntered = self.remitteeName.isNotBlank } @@ -105,7 +109,11 @@ struct TransferMoneyDialog: View { } } - TextField("Remittee IBAN", text: $remitteeIban) + UIKitTextField("Remittee IBAN", text: $remitteeIban) { + if self.isRequiredDataEntered() { + self.transferMoney() + } + } .onReceive(Just(remitteeIban)) { newValue in self.isValidRemitteeIbanEntered = newValue.count > 14 // TODO: implement real check if IBAN is valid self.tryToGetBicFromIban(newValue) @@ -113,9 +121,13 @@ struct TransferMoneyDialog: View { } Section { - TextField("Amount", text: $amount) - .keyboardType(.decimalPad) + UIKitTextField("Amount", text: $amount, keyboardType: .decimalPad) { + if self.isRequiredDataEntered() { + self.transferMoney() + } + } .onReceive(Just(amount)) { newValue in + // TODO: implement DecimalTextField / NumericTextField let filtered = newValue.filter { "0123456789,".contains($0) } if filtered != newValue { self.amount = filtered @@ -124,7 +136,11 @@ struct TransferMoneyDialog: View { self.isValidAmountEntered = self.amount.isNotBlank } - TextField("Usage", text: $usage) + UIKitTextField("Usage", text: $usage) { + if self.isRequiredDataEntered() { + self.transferMoney() + } + } .onReceive(Just($usage)) { newValue in self.isValidUsageEntered = true }