Created
September 26, 2019 06:04
-
-
Save nyteshade/bdaa0852e63fe6e0acc7cc3e45e6b2e0 to your computer and use it in GitHub Desktop.
Example of Dictionary for Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment