updated MDN Field to use new inputfield
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
239af70710
commit
018fe9a25e
@ -14,7 +14,7 @@ import MVMCore
|
|||||||
/**
|
/**
|
||||||
This class provides the convenience of formatting the MDN entered/displayer for the user.
|
This class provides the convenience of formatting the MDN entered/displayer for the user.
|
||||||
*/
|
*/
|
||||||
@objcMembers open class MdnEntryField: TextEntryField, ABPeoplePickerNavigationControllerDelegate, CNContactPickerDelegate {
|
@objcMembers open class MdnEntryField: InputEntryField, ABPeoplePickerNavigationControllerDelegate, CNContactPickerDelegate {
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Stored Properties
|
// MARK: - Stored Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -47,52 +47,17 @@ import MVMCore
|
|||||||
get { MVMCoreUIUtility.removeMdnFormat(text) }
|
get { MVMCoreUIUtility.removeMdnFormat(text) }
|
||||||
set { text = MVMCoreUIUtility.formatMdn(newValue) }
|
set { text = MVMCoreUIUtility.formatMdn(newValue) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Toggles selected or original (unselected) UI.
|
|
||||||
public override var isSelected: Bool {
|
|
||||||
get { return entryFieldContainer.isSelected }
|
|
||||||
set (selected) {
|
|
||||||
if selected && showError {
|
|
||||||
showError = false
|
|
||||||
}
|
|
||||||
|
|
||||||
super.isSelected = selected
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
|
||||||
// MARK: - Initializers
|
|
||||||
//--------------------------------------------------
|
|
||||||
|
|
||||||
@objc public override init(frame: CGRect) {
|
|
||||||
super.init(frame: .zero)
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc public convenience init() {
|
|
||||||
self.init(frame: .zero)
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc required public init?(coder: NSCoder) {
|
|
||||||
super.init(coder: coder)
|
|
||||||
fatalError("MdnEntryField xib not supported.")
|
|
||||||
}
|
|
||||||
|
|
||||||
required public init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
|
||||||
super.init(model: model, delegateObject, additionalData)
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Setup
|
// MARK: - Setup
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
@objc public override func setupFieldContainerContent(_ container: UIView) {
|
open override func setup() {
|
||||||
super.setupFieldContainerContent(container)
|
super.setup()
|
||||||
|
setupTextFieldToolbar()
|
||||||
textField.keyboardType = .numberPad
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open override func setupTextFieldToolbar() {
|
open func setupTextFieldToolbar() {
|
||||||
|
|
||||||
let toolbar = UIToolbar.createEmptyToolbar()
|
let toolbar = UIToolbar.createEmptyToolbar()
|
||||||
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
|
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
|
||||||
let contacts = UIBarButtonItem(title: MVMCoreUIUtility.hardcodedString(withKey: "textfield_contacts_barbutton"), style: .plain, target: self, action: #selector(getContacts))
|
let contacts = UIBarButtonItem(title: MVMCoreUIUtility.hardcodedString(withKey: "textfield_contacts_barbutton"), style: .plain, target: self, action: #selector(getContacts))
|
||||||
@ -103,40 +68,7 @@ import MVMCore
|
|||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Methods
|
// MARK: - Methods
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
@objc public func hasValidMDN() -> Bool {
|
|
||||||
|
|
||||||
guard let MDN = mdn, !MDN.isEmpty else { return false }
|
|
||||||
|
|
||||||
if isNationalMDN {
|
|
||||||
return MVMCoreUIUtility.validateMDNString(MDN)
|
|
||||||
}
|
|
||||||
|
|
||||||
return MVMCoreUIUtility.validateInternationalMDNString(MDN)
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc public func validateMDNTextField() -> Bool {
|
|
||||||
|
|
||||||
guard !shouldValidateMDN, let MDN = mdn, !MDN.isEmpty else {
|
|
||||||
isValid = true
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
isValid = hasValidMDN()
|
|
||||||
|
|
||||||
if self.isValid {
|
|
||||||
showError = false
|
|
||||||
|
|
||||||
} else {
|
|
||||||
entryFieldModel?.errorMessage = entryFieldModel?.errorMessage ?? MVMCoreUIUtility.hardcodedString(withKey: "textfield_phone_format_error_message")
|
|
||||||
showError = true
|
|
||||||
UIAccessibility.post(notification: .layoutChanged, argument: textField)
|
|
||||||
}
|
|
||||||
|
|
||||||
return isValid
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc public func getContacts(_ sender: Any?) {
|
@objc public func getContacts(_ sender: Any?) {
|
||||||
|
|
||||||
let picker = CNContactPickerViewController()
|
let picker = CNContactPickerViewController()
|
||||||
@ -152,11 +84,12 @@ import MVMCore
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - MoleculeViewProtocol
|
// MARK: - MoleculeViewProtocol
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
public override func viewModelDidUpdate() {
|
||||||
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
viewModel.type = .phone
|
||||||
super.set(with: model, delegateObject, additionalData)
|
super.viewModelDidUpdate()
|
||||||
|
if let phoneNumber = viewModel.text {
|
||||||
textField.keyboardType = .phonePad
|
text = phoneNumber.formatUSNumber()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -179,62 +112,47 @@ import MVMCore
|
|||||||
let startIndex = unformedMDN.index(unformedMDN.startIndex, offsetBy: 1)
|
let startIndex = unformedMDN.index(unformedMDN.startIndex, offsetBy: 1)
|
||||||
unformattedMDN = String(unformedMDN[startIndex...])
|
unformattedMDN = String(unformedMDN[startIndex...])
|
||||||
}
|
}
|
||||||
|
|
||||||
text = unformattedMDN
|
text = unformattedMDN
|
||||||
textFieldShouldReturn(textField)
|
textFieldShouldReturn(textField)
|
||||||
textFieldDidEndEditing(textField)
|
textFieldDidEndEditing(textField)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Implemented TextField Delegate
|
// MARK: - Implemented TextField Delegate
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
@objc public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
@objc public override func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
||||||
|
_ = resignFirstResponder()
|
||||||
textField.resignFirstResponder()
|
let superValue = super.textFieldShouldReturn(textField)
|
||||||
|
return proprietorTextDelegate?.textFieldShouldReturn?(textField) ?? superValue
|
||||||
return proprietorTextDelegate?.textFieldShouldReturn?(textField) ?? true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
@objc public override func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
||||||
|
let superValue = super.textField(textField, shouldChangeCharactersIn: range, replacementString: string)
|
||||||
if !MVMCoreUIUtility.validate(string, withRegularExpression: RegularExpressionDigitOnly) {
|
return proprietorTextDelegate?.textField?(textField, shouldChangeCharactersIn: range, replacementString: string) ?? superValue
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return proprietorTextDelegate?.textField?(textField, shouldChangeCharactersIn: range, replacementString: string) ?? true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func textFieldDidBeginEditing(_ textField: UITextField) {
|
@objc public override func textFieldDidBeginEditing(_ textField: UITextField) {
|
||||||
|
super.textFieldDidBeginEditing(textField)
|
||||||
textField.text = MVMCoreUIUtility.removeMdnFormat(textField.text)
|
|
||||||
proprietorTextDelegate?.textFieldDidBeginEditing?(textField)
|
proprietorTextDelegate?.textFieldDidBeginEditing?(textField)
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func textFieldDidEndEditing(_ textField: UITextField) {
|
@objc public override func textFieldDidEndEditing(_ textField: UITextField) {
|
||||||
|
|
||||||
proprietorTextDelegate?.textFieldDidEndEditing?(textField)
|
proprietorTextDelegate?.textFieldDidEndEditing?(textField)
|
||||||
|
super.textFieldDidEndEditing(textField)
|
||||||
if validateMDNTextField() {
|
|
||||||
if isNationalMDN {
|
|
||||||
textField.text = MVMCoreUIUtility.formatMdn(textField.text)
|
|
||||||
}
|
|
||||||
// Validate the base input field along with triggering form field validation rules.
|
|
||||||
validateText()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
|
@objc public override func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
|
||||||
proprietorTextDelegate?.textFieldShouldBeginEditing?(textField) ?? true
|
proprietorTextDelegate?.textFieldShouldBeginEditing?(textField) ?? true
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
|
@objc public override func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
|
||||||
proprietorTextDelegate?.textFieldShouldEndEditing?(textField) ?? true
|
proprietorTextDelegate?.textFieldShouldEndEditing?(textField) ?? true
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func textFieldShouldClear(_ textField: UITextField) -> Bool {
|
@objc public override func textFieldShouldClear(_ textField: UITextField) -> Bool {
|
||||||
proprietorTextDelegate?.textFieldShouldClear?(textField) ?? true
|
proprietorTextDelegate?.textFieldShouldClear?(textField) ?? true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,4 +12,9 @@
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
public override class var identifier: String { "mdnEntryField" }
|
public override class var identifier: String { "mdnEntryField" }
|
||||||
|
|
||||||
|
open override func formFieldServerValue() -> AnyHashable? {
|
||||||
|
guard let value = formFieldValue() as? String else { return nil }
|
||||||
|
return value.filter { $0.isNumber }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user