diff --git a/MVMCoreUI/Atoms/TextFields/DigitTextBox.swift b/MVMCoreUI/Atoms/TextFields/DigitTextBox.swift index 48d6734d..f87d2189 100644 --- a/MVMCoreUI/Atoms/TextFields/DigitTextBox.swift +++ b/MVMCoreUI/Atoms/TextFields/DigitTextBox.swift @@ -8,14 +8,130 @@ import UIKit -class DigitTextBox: UITextField { - - /* - // Only override draw() if you perform custom drawing. - // An empty implementation adversely affects performance during animation. - override func draw(_ rect: CGRect) { - // Drawing code - } - */ - +@objc protocol MFDigitTextBoxDelegate: NSObjectProtocol { + @objc optional func textFieldDidDelete(_ textField: UITextField?) +} + +class DigitTextBox: UITextField, MVMCoreViewProtocol { + //-------------------------------------------------- + // MARK: - Outlets + //-------------------------------------------------- + + private weak var bottomBar: SeparatorView? + + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + + weak var mfTextBoxDelegate: MFDigitTextBoxDelegate? + + private var previousSize: CGFloat = 0.0 + + //-------------------------------------------------- + // MARK: - Constraints + //-------------------------------------------------- + + private weak var widthConstraint: NSLayoutConstraint? + private weak var heightConstraint: NSLayoutConstraint? + + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- + + required init?(coder: NSCoder) { + super.init(coder: coder) + fatalError("init(coder:) has not been implemented") + } + + public override init(frame: CGRect) { + super.init(frame: frame) + setup() + } + + public convenience init() { + self.init(frame: .zero) + } + + //-------------------------------------------------- + // MARK: - Lifecycle + //-------------------------------------------------- + + func setup() { + + if constraints.count == 0 { + + translatesAutoresizingMaskIntoConstraints = false + backgroundColor = .white + textAlignment = .center + keyboardType = .numberPad + layer.borderWidth = 1 + hideError() + + widthConstraint = widthAnchor.constraint(equalToConstant: 40) + widthConstraint?.isActive = true + + heightConstraint = heightAnchor.constraint(equalToConstant: 60) + heightConstraint?.isActive = true + + bottomBar = SeparatorView.separatorAdd(to: self, position: SeparatorPositionBot) + bottomBar?.setAsMedium() + + updateView(MVMCoreUISplitViewController.getDetailViewWidth()) + } + } + + //-------------------------------------------------- + // MARK: - Methods + //-------------------------------------------------- + + override func deleteBackward() { + super.deleteBackward() + + if let delegate = mfTextBoxDelegate { + delegate.textFieldDidDelete?(self) + } + } + + func updateView(_ size: CGFloat) { + + DispatchQueue.main.async { + if !MVMCoreGetterUtility.fequal(a: Float(size), b: Float(self.previousSize)) { + MFStyler.styleTextField(self) + self.font = MFFonts.mfFont55Rg(28) + + var digitWidth: CGFloat = 0 + var digitHeight: CGFloat = 0 + + let sizeObject = MFSizeObject(standardBlock: { + digitWidth = 39 + digitHeight = 44 + + }, smalliPhone: { + digitWidth = 35 + digitHeight = 38 + + }, standardiPadPortraitBlock: { + digitWidth = 59 + digitHeight = 74 + }) + + sizeObject?.performBlockBase(onSize: size) + self.widthConstraint?.constant = digitWidth + self.heightConstraint?.constant = digitHeight + self.previousSize = size + } + } + } + + func setAsError() { + + layer.borderColor = UIColor.mfPumpkin().cgColor + bottomBar?.backgroundColor = UIColor.mfPumpkin() + bottomBar?.height?.constant = 4 + } + + func hideError() { + layer.borderColor = UIColor.mfSilver().cgColor + bottomBar?.setAsMedium() + } } diff --git a/MVMCoreUI/Atoms/TextFields/MFDigitTextField.m b/MVMCoreUI/Atoms/TextFields/MFDigitTextField.m index 5f76e88b..dcc08512 100644 --- a/MVMCoreUI/Atoms/TextFields/MFDigitTextField.m +++ b/MVMCoreUI/Atoms/TextFields/MFDigitTextField.m @@ -118,6 +118,8 @@ }]; } +// CONTINUE + - (void)updateView:(CGFloat)size { [super updateView:size]; [MVMCoreDispatchUtility performBlockOnMainThread:^{ diff --git a/MVMCoreUI/Atoms/TextFields/TextField.swift b/MVMCoreUI/Atoms/TextFields/TextField.swift index 20df71c5..d5bf2c72 100644 --- a/MVMCoreUI/Atoms/TextFields/TextField.swift +++ b/MVMCoreUI/Atoms/TextFields/TextField.swift @@ -11,11 +11,11 @@ import UIKit @objc public protocol TextFieldDelegate: NSObjectProtocol { /// Called when the entered text becomes valid based on the validation block - @objc optional func entryIsValid(_ textfield: TextField?) + @objc optional func isValid(_ textfield: TextField?) /// Called when the entered text becomes invalid based on the validation block - @objc optional func entryIsInvalid(_ textfield: TextField?) + @objc optional func isInvalid(_ textfield: TextField?) /// Dismisses the keyboard. - @objc optional func dismissFieldInput(_ sender: Any?) + @objc optional func dismissField(_ sender: Any?) } @objcMembers open class TextField: ViewConstrainingView { @@ -650,13 +650,13 @@ import UIKit } if let mfTextFieldDelegate = mfTextFieldDelegate { - mfTextFieldDelegate.entryIsInvalid?(self) + mfTextFieldDelegate.isInvalid?(self) } } else if !previousValidity && isValid { hideError() if let mfTextFieldDelegate = mfTextFieldDelegate { - mfTextFieldDelegate.entryIsValid?(self) + mfTextFieldDelegate.isValid?(self) } } } diff --git a/MVMCoreUI/FormUIHelpers/FormValidator+TextFields.swift b/MVMCoreUI/FormUIHelpers/FormValidator+TextFields.swift index 27d6c18a..e8d79245 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidator+TextFields.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidator+TextFields.swift @@ -37,3 +37,34 @@ import Foundation }) } } + +// Temporary: Looking to either combine or separate entirely with MFTextFieldDelegate. +extension FormValidator: TextFieldDelegate { + + public func dismissField(_ sender: Any?) { + + if let delegate = delegate as? MFTextFieldDelegate { + delegate.dismissFieldInput?(sender) + } + } + + @nonobjc public func isValid(_ textfield: MFTextField?) { + + MVMCoreDispatchUtility.performBlock(onMainThread: { + self.enableByValidation() + if let delegate = self.delegate as? MFTextFieldDelegate { + delegate.entryIsValid?(textfield) + } + }) + } + + public func isInvalid(_ textfield: MFTextField?) { + + MVMCoreDispatchUtility.performBlock(onMainThread: { + self.enableByValidation() + if let delegate = self.delegate as? MFTextFieldDelegate { + delegate.entryIsInvalid?(textfield) + } + }) + } +}