having badge indicator object for the buttonIcon

This commit is contained in:
vasavk 2024-02-07 13:12:39 +05:30
parent 1646eb75dc
commit 95d638ebf4

View File

@ -43,6 +43,11 @@ open class ButtonIcon: Control {
case colorFill, media
}
/// Enum used to describe the badge indicator direction of icon button determining the expand direction.
public enum ExpandDirection: String, CaseIterable {
case right, center, left
}
/// Enum used to describe the size of button icon.
public enum Size: String, EnumSubset {
case large
@ -67,6 +72,8 @@ open class ButtonIcon: Control {
private var centerYConstraint: NSLayoutConstraint?
private var layoutGuideWidthConstraint: NSLayoutConstraint?
private var layoutGuideHeightConstraint: NSLayoutConstraint?
private var badgeIndicatorLeadingConstraint: NSLayoutConstraint?
private var badgeIndicatorBottomConstraint: NSLayoutConstraint?
private var currentIconName: Icon.Name? {
if let selectedIconName {
return selectedIconName
@ -75,9 +82,31 @@ open class ButtonIcon: Control {
}
}
private var badgeIndicatorOffset: CGPoint {
switch (size, kind) {
case (.small, .ghost):
return .init(x: 1, y: 0)
case (.large, .ghost):
return .init(x: 1, y: 1)
case (.small, .lowContrast), (.small, .highContrast):
return .init(x: 4, y: 4)
case (.large, .lowContrast), (.large, .highContrast):
return .init(x: 6, y: 6)
}
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
///BadgeIndicator object used to render for the ButtonIcon.
open var badgeIndicator = BadgeIndicator().with {
$0.translatesAutoresizingMaskIntoConstraints = false
// $0.hideBorder = true
// $0.borderWidth = 0
$0.size = .small
}
/// Icon object used to render out the Icon for this ButtonIcon.
open var icon = Icon().with { $0.isUserInteractionEnabled = false }
@ -107,7 +136,13 @@ open class ButtonIcon: Control {
/// If set to true, the button icon will not have a border.
open var hideBorder: Bool = true { didSet { setNeedsUpdate() } }
/// If provided, the badge indicator will present.
open var showBadge: Bool = false { didSet { setNeedsUpdate() } }
/// If provided, the button icon will have selectable Icon.
open var selectable: Bool = false { didSet { setNeedsUpdate() } }
/// Used to move the icon inside the button in both x and y axis.
open var iconOffset: CGPoint = .init(x: 0, y: 0) { didSet { setNeedsUpdate() } }
@ -270,11 +305,16 @@ open class ButtonIcon: Control {
//add the icon
addSubview(icon)
addSubview(badgeIndicator)
badgeIndicator.isHidden = !showBadge
//determines the height/width of the icon
layoutGuideWidthConstraint = iconLayoutGuide.width(constant: size.containerSize)
layoutGuideHeightConstraint = iconLayoutGuide.height(constant: size.containerSize)
badgeIndicatorBottomConstraint = icon.centerYAnchor.constraint(equalTo: badgeIndicator.bottomAnchor)
badgeIndicatorLeadingConstraint = badgeIndicator.leadingAnchor.constraint(equalTo: icon.centerXAnchor)
badgeIndicatorBottomConstraint?.isActive = true
badgeIndicatorLeadingConstraint?.isActive = true
//pin layout guide
iconLayoutGuide
.pinTop()
@ -300,6 +340,9 @@ open class ButtonIcon: Control {
hideBorder = true
iconOffset = .init(x: 0, y: 0)
iconName = nil
// selectableIconName = nil
showBadge = false
selectable = false
shouldUpdateView = true
setNeedsUpdate()
}
@ -318,6 +361,8 @@ open class ButtonIcon: Control {
} else {
icon.reset()
}
badgeIndicatorLeadingConstraint?.constant = badgeIndicatorOffset.x
badgeIndicatorBottomConstraint?.constant = badgeIndicatorOffset.y
setNeedsLayout()
}