Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- enum ConfigType {
- case primaryFont(UIFont)
- case secondaryFont(UIFont)
- case tertiaryFont(UIFont)
- case primaryLabelColor(UIColor)
- case secondaryLabelColor(UIColor)
- case tertiaryLabelColor(UIColor)
- var name: ConfigName {
- switch self {
- case .primaryFont(_): return .primaryFont
- case .secondaryFont(_): return .secondaryFont
- case .tertiaryFont(_): return .tertiaryFont
- case .primaryLabelColor(_): return .primaryLabelColor
- case .secondaryLabelColor(_): return .secondaryLabelColor
- case .tertiaryLabelColor(_): return .tertiaryLabelColor
- }
- }
- }
- enum ConfigName: String, CaseIterable, Hashable {
- case primaryFont
- case secondaryFont
- case tertiaryFont
- case primaryLabelColor
- case secondaryLabelColor
- case tertiaryLabelColor
- }
- extension Dictionary where Key == ConfigName, Value == ConfigType {
- mutating func set(_ type: ConfigType) {
- self[type.name] = type
- }
- func getColor(_ name: ConfigName, _ defColor: UIColor = .clear) -> UIColor {
- switch name {
- case .primaryLabelColor, .secondaryLabelColor, .tertiaryLabelColor:
- if let type: ConfigType = self[name] {
- switch type {
- case let .primaryLabelColor(color): return color
- case let .secondaryLabelColor(color): return color
- case let .tertiaryLabelColor(color): return color
- default: return defColor
- }
- }
- default:
- return defColor
- }
- return defColor
- }
- func getFont(
- _ name: ConfigName,
- _ defFont: UIFont = .systemFont(ofSize: 10)
- ) -> UIFont {
- switch name {
- case .primaryFont, .secondaryFont, .tertiaryFont:
- if let type: ConfigType = self[name] {
- switch type {
- case let .primaryFont(font): return font
- case let .secondaryFont(font): return font
- case let .tertiaryFont(font): return font
- default: return defFont
- }
- }
- default:
- return defFont
- }
- return defFont
- }
- mutating func apply(dict: Dictionary<ConfigName,ConfigType>) {
- for value in dict.values {
- self[value.name] = value
- }
- }
- }
- var config = [ConfigName:ConfigType]()
- var config2 = [ConfigName:ConfigType]()
- config[.primaryFont] = .primaryFont(.systemFont(ofSize: 12))
- config.set(.secondaryFont(.systemFont(ofSize: 18)))
- config.set(.primaryLabelColor(.red))
- config2.set(.primaryLabelColor(.brown))
- config.apply(dict: config2)
- config.getFont(.primaryFont) // UIFont.systemFont(ofSize: 12)
- config.getFont(.secondaryFont) // UIFont.systemFont(ofSize: 18)
- config.getColor(.primaryLabelColor) // UIColor.red
- config.getColor(.tertiaryLabelColor) // UIColor.clear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement