This commit is contained in:
Kevin G Christiano 2019-10-25 15:17:38 -04:00
parent 5bc78e594e
commit f14614514d

View File

@ -21,8 +21,10 @@ import UIKit
private(set) var feedbackLabel: Label?
private(set) var fieldContainer: UIView?
public var backgroundView: UIView?
public var separatorView: UIView?
private var borderStrokeColor: UIColor = UIColor.mfSilver()
private var borderPath: UIBezierPath?
public var bottomBar: UIView?
public var dashLine: DashLine?
//--------------------------------------------------
@ -32,23 +34,28 @@ import UIKit
public var isValid = false
public var fieldKey: String?
private var borderPath: UIBezierPath?
public var showErrorMessage = false
public var errorMessage: String? {
didSet { feedback = errorMessage }
didSet {
if showErrorMessage {
feedback = errorMessage
}
}
}
/// Toggles the enables state of this component.
public var isEnabled = true {
didSet {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.isUserInteractionEnabled = self.isEnabled
self.feedbackLabel?.text = nil
self.descriptionLabel?.textColor = self.isEnabled ? UIColor.mfBattleshipGrey() : UIColor.mfSilver()
self.feedbackLabel?.textColor = self.isEnabled ? .black : UIColor.mfSilver()
self.separatorView?.backgroundColor = self.showErrorMessage ? UIColor.mfPumpkin() : .black
self.bottomBar?.backgroundColor = self.isEnabled ? (self.showErrorMessage ? UIColor.mfPumpkin() : .black) : UIColor.mfSilver()
self.fieldContainer?.setNeedsDisplay()
self.fieldContainer?.layoutIfNeeded()
}
}
}
@ -57,6 +64,17 @@ import UIKit
public var hideBorder = false {
didSet { setNeedsDisplay() }
}
public var isLocked = false {
didSet {
isUserInteractionEnabled = isLocked
borderStrokeColor = isLocked ? .clear : UIColor.mfSilver()
bottomBar?.backgroundColor = isLocked ? .clear : .black
bottomBarHeightConstraint?.constant = 1
fieldContainer?.setNeedsDisplay()
}
}
public var descriptionText: String? {
get { return descriptionLabel?.text }
@ -69,23 +87,19 @@ import UIKit
/// Override this to conveniently get/set the textfield(s).
public var text: String? {
get { return nil }
set {
fatalError("You're supposed to override FormEntryField's 'text' variable.")
}
set { fatalError("You need to override FormEntryField's 'text' variable in the subclass.") }
}
/// Sets feedback text in the textField.
public var feedback: String? {
get { return feedbackLabel?.text }
set {
guard isEnabled else { return }
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.feedbackLabel?.text = newValue
self.separatorHeightConstraint?.constant = self.showErrorMessage ? 4 : 1
self.separatorView?.backgroundColor = self.showErrorMessage ? UIColor.mfPumpkin() : .black
self.bottomBarHeightConstraint?.constant = self.showErrorMessage ? 4 : 1
self.bottomBar?.backgroundColor = self.showErrorMessage ? UIColor.mfPumpkin() : .black
self.setNeedsDisplay()
self.layoutIfNeeded()
}
@ -118,7 +132,7 @@ import UIKit
public var descriptionLabelLeading: NSLayoutConstraint?
public var descriptionLabelTrailing: NSLayoutConstraint?
public var separatorHeightConstraint: NSLayoutConstraint?
public var bottomBarHeightConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Initializers
@ -215,32 +229,20 @@ import UIKit
/// Configuration for the field container view.
private func setupFieldContainer(_ parentView: UIView) {
let backgroundView = UIView(frame: .zero)
self.backgroundView = backgroundView
backgroundView.translatesAutoresizingMaskIntoConstraints = false
parentView.addSubview(backgroundView)
NSLayoutConstraint.activate([
backgroundView.topAnchor.constraint(equalTo: parentView.topAnchor, constant: 1),
backgroundView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor, constant: 1),
parentView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor, constant: 1),
parentView.bottomAnchor.constraint(equalTo: backgroundView.bottomAnchor, constant: 1)])
setupFieldContainerContent(parentView)
let separatorView = UIView(frame: .zero)
self.separatorView = separatorView
separatorView.translatesAutoresizingMaskIntoConstraints = false
separatorView.backgroundColor = .black
let bottomBar = UIView(frame: .zero)
self.bottomBar = bottomBar
bottomBar.translatesAutoresizingMaskIntoConstraints = false
bottomBar.backgroundColor = .black
parentView.addSubview(separatorView)
parentView.addSubview(bottomBar)
separatorHeightConstraint = separatorView.heightAnchor.constraint(equalToConstant: 1)
separatorHeightConstraint?.isActive = true
separatorView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor).isActive = true
parentView.trailingAnchor.constraint(equalTo: separatorView.trailingAnchor).isActive = true
parentView.bottomAnchor.constraint(equalTo: separatorView.bottomAnchor).isActive = true
bottomBarHeightConstraint = bottomBar.heightAnchor.constraint(equalToConstant: 1)
bottomBarHeightConstraint?.isActive = true
bottomBar.leadingAnchor.constraint(equalTo: parentView.leadingAnchor).isActive = true
parentView.trailingAnchor.constraint(equalTo: bottomBar.trailingAnchor).isActive = true
parentView.bottomAnchor.constraint(equalTo: bottomBar.bottomAnchor).isActive = true
let dashLine = DashLine()
dashLine.translatesAutoresizingMaskIntoConstraints = false
@ -250,10 +252,10 @@ import UIKit
parentView.addSubview(dashLine)
NSLayoutConstraint.activate([
dashLine.centerYAnchor.constraint(equalTo: separatorView.centerYAnchor),
dashLine.centerXAnchor.constraint(equalTo: separatorView.centerXAnchor),
dashLine.topAnchor.constraint(equalTo: separatorView.topAnchor),
dashLine.leadingAnchor.constraint(equalTo: separatorView.leadingAnchor)])
dashLine.centerYAnchor.constraint(equalTo: bottomBar.centerYAnchor),
dashLine.centerXAnchor.constraint(equalTo: bottomBar.centerXAnchor),
dashLine.topAnchor.constraint(equalTo: bottomBar.topAnchor),
dashLine.leadingAnchor.constraint(equalTo: bottomBar.leadingAnchor)])
}
open override func updateView(_ size: CGFloat) {
@ -277,15 +279,17 @@ import UIKit
if !hideBorder, let frame = fieldContainer?.frame {
let insetLean: CGFloat = 0.5
borderPath = UIBezierPath()
borderPath?.move(to: CGPoint(x: frame.origin.x, y: frame.origin.y + frame.size.height))
borderPath?.addLine(to: CGPoint(x: frame.origin.x, y: frame.origin.y))
borderPath?.addLine(to: CGPoint(x: frame.origin.x + frame.size.width, y: frame.origin.y))
borderPath?.addLine(to: CGPoint(x: frame.origin.x + frame.size.width, y: frame.origin.y + frame.size.height))
borderPath?.lineWidth = 1
let strokeColor = showErrorMessage ? UIColor.mfPumpkin() : UIColor.mfSilver()
strokeColor.setStroke()
borderPath?.move(to: CGPoint(x: frame.origin.x + insetLean, y: frame.origin.y + frame.size.height))
borderPath?.addLine(to: CGPoint(x: frame.origin.x + insetLean, y: frame.origin.y + insetLean))
borderPath?.addLine(to: CGPoint(x: frame.origin.x + frame.size.width - insetLean, y: frame.origin.y + insetLean))
borderPath?.addLine(to: CGPoint(x: frame.origin.x + frame.size.width - insetLean, y: frame.origin.y + frame.size.height))
borderStrokeColor.setStroke()
borderPath?.stroke()
}
@ -313,16 +317,27 @@ import UIKit
// MARK: - Methods
//--------------------------------------------------
open func showPlaceholderErrorLabel(_ show: Bool) {
open func showError(_ show: Bool) {
feedbackLabel?.isHidden = !show
showErrorMessage = show
if show {
feedbackLabel?.text = errorMessage
}
}
open func resetEntryAppearance() {
borderStrokeColor = .mfSilver()
bottomBar?.backgroundColor = .black
bottomBarHeightConstraint?.constant = 1
}
open func showDashSeperatorView(_ dash: Bool) {
// Never hide seperator view because it could be possiblely used by other classes for positioning
dashLine?.isHidden = !dash
separatorView?.backgroundColor = dash ? .clear : .black
bottomBar?.backgroundColor = dash ? .clear : .black
}
}
@ -337,7 +352,7 @@ extension FormEntryField {
else { return }
if let formText = dictionary[KeyLabel] as? String {
self.descriptionText = formText
descriptionText = formText
}
if let text = dictionary[KeyDisable] as? String, text.isEqual(StringY) || dictionary.boolForKey(KeyDisable) {
@ -345,7 +360,7 @@ extension FormEntryField {
}
if let errMessage = dictionary[KeyErrorMessage] as? String {
self.errorMessage = errMessage
errorMessage = errMessage
}
if let hideBorder = dictionary["hideBorder"] as? Bool {