155 lines
4.6 KiB
Swift
155 lines
4.6 KiB
Swift
//
|
|
// Icon.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 1/9/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import Combine
|
|
|
|
public enum IconSize: String, CaseIterable {
|
|
case xsmall
|
|
case small
|
|
case medium
|
|
case large
|
|
case XLarge
|
|
|
|
public var dimensions: CGSize {
|
|
switch self {
|
|
|
|
case .xsmall:
|
|
return .init(width: 12, height: 12)
|
|
|
|
case .small:
|
|
return .init(width: 16, height: 16)
|
|
|
|
case .medium:
|
|
return .init(width: 20, height: 20)
|
|
|
|
case .large:
|
|
return .init(width: 24, height: 24)
|
|
|
|
case .XLarge:
|
|
return .init(width: 28, height: 28)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc(VDSIcon)
|
|
public class Icon: View {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Properties
|
|
//--------------------------------------------------
|
|
private var widthConstraint: NSLayoutConstraint?
|
|
private var heightConstraint: NSLayoutConstraint?
|
|
|
|
private var imageView = UIImageView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.contentMode = .scaleAspectFill
|
|
$0.clipsToBounds = true
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
open var color: IconColor = .black { didSet { didChange() }}
|
|
open var size: IconSize = .medium { didSet { didChange() }}
|
|
open var name: IconName? { didSet { didChange() }}
|
|
|
|
//reserverd for internal components only
|
|
internal var restrictedName: RestrictedIconName? { didSet { didChange() }}
|
|
internal var isBold: Bool = false { didSet { didChange() }}
|
|
|
|
//functions
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
|
|
addSubview(imageView)
|
|
imageView.pinToSuperView()
|
|
|
|
heightConstraint = imageView.heightAnchor.constraint(equalToConstant: size.dimensions.height)
|
|
heightConstraint?.isActive = true
|
|
widthConstraint = imageView.widthAnchor.constraint(equalToConstant: size.dimensions.width)
|
|
widthConstraint?.isActive = true
|
|
|
|
backgroundColor = .clear
|
|
}
|
|
|
|
public override func reset() {
|
|
super.reset()
|
|
color = .black
|
|
imageView.image = nil
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - State
|
|
//--------------------------------------------------
|
|
open override func updateView() {
|
|
super.updateView()
|
|
|
|
//get the color for the image
|
|
var imageColor = color.value
|
|
|
|
//ensure the correct color for white/black colors
|
|
if surface == .dark && color == IconColor.black {
|
|
imageColor = VDSColor.elementsPrimaryOndark
|
|
} else if surface == .light && color == IconColor.black {
|
|
imageColor = VDSColor.elementsPrimaryOnlight
|
|
}
|
|
|
|
//set the icon dimensions
|
|
let dimensions = size.dimensions
|
|
heightConstraint?.constant = dimensions.height
|
|
widthConstraint?.constant = dimensions.width
|
|
|
|
//get the image name
|
|
//set the image
|
|
if let restrictedName, let image = getImage(for: restrictedName.rawValue) {
|
|
setImage(image: image, imageColor: imageColor)
|
|
|
|
} else if let name, let image = getImage(for: name.rawValue) {
|
|
setImage(image: image, imageColor: imageColor)
|
|
} else {
|
|
imageView.image = nil
|
|
}
|
|
}
|
|
|
|
private func getImage(for imageName: String) -> UIImage? {
|
|
if !imageName.hasPrefix("pagination"), isBold {
|
|
let boldIconName = "\(imageName)-bold"
|
|
if let image = UIImage(named: boldIconName, in: Bundle(for: Self.self), with: nil) {
|
|
return image
|
|
|
|
} else if let image = UIImage(named: boldIconName) {
|
|
return image
|
|
|
|
} else if let image = UIImage(named: imageName) {
|
|
return image
|
|
}
|
|
} else {
|
|
if let image = UIImage(named: imageName, in: Bundle(for: Self.self), with: nil) {
|
|
return image
|
|
|
|
} else if let image = UIImage(named: imageName) {
|
|
return image
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
private func setImage(image: UIImage, imageColor: UIColor) {
|
|
imageView.image = image.withTintColor(imageColor)
|
|
}
|
|
}
|
|
|