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