Skip to content

Commit

Permalink
[Refacto-557] Button: Remove clear button - Improve State
Browse files Browse the repository at this point in the history
  • Loading branch information
robergro committed Nov 7, 2023
1 parent f64a107 commit 096dc80
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class ControlPropertyStates<PropertyType> {

/// Get the value for the status of the control.
/// - Parameters:
/// - state: the status of the control
/// - status: the status of the control
func value(forStatus status: ControlStatus) -> PropertyType? {
// isHighlighted has the highest priority,
// then isDisabled,
Expand Down
18 changes: 15 additions & 3 deletions core/Sources/Common/Control/UIView/UIControlStateImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ final class UIControlStateImageView: UIImageView {

private let imageStates = ControlPropertyStates<UIImage>()

private var storedImage: UIImage? {
didSet {
self.isImage = self.storedImage != nil
self.image = self.storedImage
}
}

// MARK: - Published

@Published var isImage: Bool = false

// MARK: - Override Properties

override var image: UIImage? {
didSet {
self.isImage = self.image != nil
get {
return super.image
}
set {
if newValue == self.storedImage {
super.image = newValue
}
}
}

Expand Down Expand Up @@ -62,6 +74,6 @@ final class UIControlStateImageView: UIImageView {
)

// Set the image from states
self.image = self.imageStates.value(forStatus: status)
self.storedImage = self.imageStates.value(forStatus: status)
}
}
73 changes: 52 additions & 21 deletions core/Sources/Common/Control/UIView/UIControlStateLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ final class UIControlStateLabel: UILabel {
private let attributedTextStates = ControlPropertyStates<NSAttributedString>()
private let textTypesStates = ControlPropertyStates<DisplayedTextType>()

private var storedText: String? {
didSet {
self.isText = self.storedText != nil
self.text = self.storedText

// Reset styles
if let storedTextFont = self.storedTextFont {
self.font = storedTextFont
}
if let storedTextColor = self.storedTextColor {
self.textColor = storedTextColor
}
}
}
private var storedAttributedText: NSAttributedString? {
didSet {
self.isText = self.storedAttributedText != nil
self.attributedText = self.storedAttributedText
}
}

private var storedTextFont: UIFont?
private var storedTextColor: UIColor?

Expand All @@ -28,14 +49,26 @@ final class UIControlStateLabel: UILabel {
// MARK: - Override Properties

override var text: String? {
didSet {
self.isText = self.text != nil
get {
return super.text
}
set {
// Set the attributedText only if the current come from setText
if newValue == self.storedText {
super.text = newValue
}
}
}

override var attributedText: NSAttributedString? {
didSet {
self.isText = self.attributedText != nil
get {
return super.attributedText
}
set {
// Set the attributedText only if the current come from setAttributedText
if newValue == self.storedAttributedText {
super.attributedText = newValue
}
}
}

Expand Down Expand Up @@ -129,26 +162,24 @@ final class UIControlStateLabel: UILabel {
)

// Get the current textType from status
let textType = textTypesStates.value(forStatus: status)
let textType = self.textTypesStates.value(forStatus: status)
let textTypeContainsText = textType?.containsText ?? false

// Reset attributedText & text
self.attributedText = nil
self.text = nil
self.storedAttributedText = nil
self.storedText = nil

// Set the text or the attributedText from textType and states
switch textType {
case .text:
self.text = self.textStates.value(forStatus: status)
if let storedTextFont = self.storedTextFont {
self.font = storedTextFont
}
if let storedTextColor = self.storedTextColor {
self.textColor = storedTextColor
}
case .attributedText:
self.attributedText = self.attributedTextStates.value(forStatus: status)
default:
break
if let text = self.textStates.value(forStatus: status),
textType == .text || !textTypeContainsText {
self.storedText = text

} else if let attributedText = self.attributedTextStates.value(forStatus: status),
textType == .attributedText || !textTypeContainsText {
self.storedAttributedText = attributedText

} else { // No text to displayed
self.text = nil
}
}

Expand All @@ -160,7 +191,7 @@ final class UIControlStateLabel: UILabel {
guard let attributedText = self.attributedText else {
return false
}

// The attributedText contains attributes ?
return !attributedText.attributes(at: 0, effectiveRange: nil).isEmpty
}
Expand Down
Loading

0 comments on commit 096dc80

Please sign in to comment.