Keeping up appearance.

This commit is contained in:
Kevin G Christiano 2019-10-30 16:02:18 -04:00
parent 9d043edb29
commit 5c1d7b4a38
2 changed files with 57 additions and 98 deletions

View File

@ -19,7 +19,7 @@ import UIKit
// MARK: - Outlets // MARK: - Outlets
//-------------------------------------------------- //--------------------------------------------------
private(set) var titleLabel: Label = { public private(set) var titleLabel: Label = {
let label = Label() let label = Label()
label.font = MFStyler.fontB3() label.font = MFStyler.fontB3()
label.textColor = UIColor.mfBattleshipGrey() label.textColor = UIColor.mfBattleshipGrey()
@ -28,7 +28,7 @@ import UIKit
return label return label
}() }()
private(set) var feedbackLabel: Label = { public private(set) var feedbackLabel: Label = {
let label = Label() let label = Label()
label.font = MFStyler.fontForTextFieldUnderLabel() label.font = MFStyler.fontForTextFieldUnderLabel()
label.textColor = .black label.textColor = .black
@ -37,7 +37,7 @@ import UIKit
return label return label
}() }()
private(set) var fieldContainer: UIView = { public private(set) var fieldContainer: UIView = {
let view = UIView(frame: .zero) let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false view.translatesAutoresizingMaskIntoConstraints = false
return view return view
@ -65,13 +65,11 @@ import UIKit
public var fieldKey: String? public var fieldKey: String?
/// Determines if a border should be drawn. /// Determines if a border should be drawn.
public var hideBorder = false private var hideBorder = false
public var showErrorMessage = false { public private(set) var appearance: Appearance = .original
didSet {
feedbackHeightConstraint?.isActive = !showErrorMessage public var showErrorMessage = false
}
}
public var errorMessage: String? public var errorMessage: String?
@ -81,7 +79,7 @@ import UIKit
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.isEnabled ? self.originalAppearance() : self.disabledAppearance() self.updateUI(appearance: self.isEnabled ? .original : .disable)
} }
} }
} }
@ -106,7 +104,7 @@ import UIKit
set { set {
feedbackLabel.text = newValue feedbackLabel.text = newValue
setAccessibilityString(newValue) setAccessibilityString(newValue)
refreshUI() refreshBorderUI()
} }
} }
@ -131,13 +129,10 @@ import UIKit
public var feedbackLabelTrailing: NSLayoutConstraint? public var feedbackLabelTrailing: NSLayoutConstraint?
public var feedbackLabelLeading: NSLayoutConstraint? public var feedbackLabelLeading: NSLayoutConstraint?
var feedbackHeightConstraint: NSLayoutConstraint?
public var titleLabelLeading: NSLayoutConstraint? public var titleLabelLeading: NSLayoutConstraint?
public var titleLabelTrailing: NSLayoutConstraint? public var titleLabelTrailing: NSLayoutConstraint?
public var bottomBarHeightConstraint: NSLayoutConstraint?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
@ -195,7 +190,6 @@ import UIKit
addSubview(feedbackLabel) addSubview(feedbackLabel)
feedbackHeightConstraint = feedbackLabel.heightAnchor.constraint(equalToConstant: 0)
feedbackLabel.topAnchor.constraint(equalTo: fieldContainer.bottomAnchor, constant: PaddingOne).isActive = true feedbackLabel.topAnchor.constraint(equalTo: fieldContainer.bottomAnchor, constant: PaddingOne).isActive = true
feedbackLabelLeading = feedbackLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor) feedbackLabelLeading = feedbackLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
feedbackLabelLeading?.isActive = true feedbackLabelLeading?.isActive = true
@ -222,7 +216,7 @@ import UIKit
titleLabel.updateView(size) titleLabel.updateView(size)
feedbackLabel.font = MFStyler.fontForTextFieldUnderLabel() feedbackLabel.font = MFStyler.fontForTextFieldUnderLabel()
refreshUI() refreshBorderUI()
} }
open override func reset() { open override func reset() {
@ -232,20 +226,19 @@ import UIKit
titleLabel.reset() titleLabel.reset()
feedbackLabel.reset() feedbackLabel.reset()
fieldContainer.subviews.forEach { $0.removeFromSuperview() } fieldContainer.subviews.forEach { $0.removeFromSuperview() }
originalAppearance() updateUI(appearance: .original)
} }
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
refreshUI(bottomBarSize: 1) refreshBorderUI(bottomBarSize: 1)
} }
open func refreshUI(bottomBarSize: CGFloat? = nil) { open func refreshBorderUI(bottomBarSize: CGFloat? = nil) {
if let size = bottomBarSize { let size = CGFloat(appearance == .error ? 4 : 1)
bottomBar.frame = CGRect(x: 0, y: fieldContainer.bounds.height - size, width: fieldContainer.bounds.width, height: size) bottomBar.frame = CGRect(x: 0, y: fieldContainer.bounds.height - size, width: fieldContainer.bounds.width, height: size)
}
self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated?(self) self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated?(self)
setNeedsDisplay() setNeedsDisplay()
@ -313,66 +306,50 @@ import UIKit
case error case error
case lock case lock
case select case select
case disabled case disable
} }
open func originalAppearance() { public func updateUI(appearance: Appearance) {
isUserInteractionEnabled = true
bottomBarHeightConstraint?.constant = 1
hideBorder = false
showErrorMessage = false
borderStrokeColor = .mfSilver()
bottomBar.backgroundColor = UIColor.black.cgColor
titleLabel.textColor = .mfBattleshipGrey()
refreshUI(bottomBarSize: 1)
}
/// - parameter showError: Default = false. Determines if error message should be displayed when setting the visual error appearance.
open func errorAppearance(showError: Bool = false) {
self.appearance = appearance
isUserInteractionEnabled = true isUserInteractionEnabled = true
hideBorder = false hideBorder = false
bottomBarHeightConstraint?.constant = 4
borderStrokeColor = .mfPumpkin()
bottomBar.backgroundColor = UIColor.mfPumpkin().cgColor
if showError { switch appearance {
showErrorMessage = true case .original:
feedback = errorMessage borderStrokeColor = .mfSilver()
feedback = nil
bottomBar.backgroundColor = UIColor.black.cgColor
titleLabel.textColor = .mfBattleshipGrey()
case .error:
borderStrokeColor = .mfPumpkin()
titleLabel.textColor = UIColor.mfBattleshipGrey()
bottomBar.backgroundColor = UIColor.mfPumpkin().cgColor
feedback = showErrorMessage ? errorMessage : nil
case .lock:
isUserInteractionEnabled = false
hideBorder = true
feedback = nil
titleLabel.textColor = UIColor.mfBattleshipGrey()
bottomBar.backgroundColor = UIColor.clear.cgColor
case .select:
borderStrokeColor = .black
feedback = nil
titleLabel.textColor = UIColor.mfBattleshipGrey()
bottomBar.backgroundColor = UIColor.black.cgColor
case .disable:
isUserInteractionEnabled = false
feedback = nil
borderStrokeColor = .mfSilver()
titleLabel.textColor = self.isEnabled ? UIColor.mfBattleshipGrey() : UIColor.mfSilver()
bottomBar.backgroundColor = self.isEnabled ? UIColor.black.cgColor : UIColor.mfSilver().cgColor
} }
refreshUI(bottomBarSize: 4) refreshBorderUI(bottomBarSize: appearance == .error ? 4 : 1)
}
open func lockAppearance() {
isUserInteractionEnabled = false
bottomBarHeightConstraint?.constant = 1
hideBorder = true
bottomBar.backgroundColor = UIColor.clear.cgColor
refreshUI(bottomBarSize: 1)
}
open func selectedAppearance() {
isUserInteractionEnabled = true
bottomBarHeightConstraint?.constant = 1
hideBorder = false
borderStrokeColor = .black
bottomBar.backgroundColor = UIColor.black.cgColor
refreshUI(bottomBarSize: 1)
}
open func disabledAppearance() {
isUserInteractionEnabled = false
bottomBarHeightConstraint?.constant = 1
hideBorder = false
feedback = nil
titleLabel.textColor = self.isEnabled ? UIColor.mfBattleshipGrey() : UIColor.mfSilver()
bottomBar.backgroundColor = self.isEnabled ? UIColor.black.cgColor : UIColor.mfSilver().cgColor
refreshUI(bottomBarSize: 1)
} }
} }
@ -403,24 +380,8 @@ extension FormEntryField {
self.hideBorder = hideBorder self.hideBorder = hideBorder
} }
if let appearance = dictionary["appearance"] as? String { if let appearance = dictionary["appearance"] as? String, let kind = Appearance(rawValue: appearance) {
switch Appearance(rawValue: appearance) { updateUI(appearance: kind)
case .error:
errorAppearance()
case .lock:
lockAppearance()
case .select:
selectedAppearance()
case .disabled:
// This is set by the isEnabled property observer.
isEnabled = false
default:
originalAppearance()
}
} }
// Key used to send text value to server // Key used to send text value to server

View File

@ -50,7 +50,7 @@ import UIKit
guard let self = self else { return } guard let self = self else { return }
self.textField.isEnabled = self.isEnabled self.textField.isEnabled = self.isEnabled
self.textField.textColor = self.isEnabled ? self.enabledTextColor : self.enabledTextColor self.textField.textColor = self.isEnabled ? self.enabledTextColor : self.disabledTextColor
} }
} }
} }
@ -115,7 +115,7 @@ import UIKit
public override init(frame: CGRect) { public override init(frame: CGRect) {
super.init(frame: frame) super.init(frame: frame)
setupView() // setupView()
} }
public convenience init() { public convenience init() {
@ -126,7 +126,7 @@ import UIKit
public init(bothDelegates: (UITextFieldDelegate & TextFieldDelegate)?) { public init(bothDelegates: (UITextFieldDelegate & TextFieldDelegate)?) {
super.init(frame: .zero) super.init(frame: .zero)
setupView() // setupView()
setBothTextDelegates(bothDelegates) setBothTextDelegates(bothDelegates)
} }
@ -158,10 +158,8 @@ import UIKit
open override func updateView(_ size: CGFloat) { open override func updateView(_ size: CGFloat) {
super.updateView(size) super.updateView(size)
MFStyler.styleTextField(textField) MFStyler.styleTextField(textField)
layoutIfNeeded() layoutIfNeeded()
} }