Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import UIKit
  2.  
  3. enum ConfigType {
  4. case primaryFont(UIFont)
  5. case secondaryFont(UIFont)
  6. case tertiaryFont(UIFont)
  7.  
  8. case primaryLabelColor(UIColor)
  9. case secondaryLabelColor(UIColor)
  10. case tertiaryLabelColor(UIColor)
  11.  
  12. var name: ConfigName {
  13. switch self {
  14. case .primaryFont(_): return .primaryFont
  15. case .secondaryFont(_): return .secondaryFont
  16. case .tertiaryFont(_): return .tertiaryFont
  17. case .primaryLabelColor(_): return .primaryLabelColor
  18. case .secondaryLabelColor(_): return .secondaryLabelColor
  19. case .tertiaryLabelColor(_): return .tertiaryLabelColor
  20. }
  21. }
  22. }
  23.  
  24. enum ConfigName: String, CaseIterable, Hashable {
  25. case primaryFont
  26. case secondaryFont
  27. case tertiaryFont
  28.  
  29. case primaryLabelColor
  30. case secondaryLabelColor
  31. case tertiaryLabelColor
  32. }
  33.  
  34. extension Dictionary where Key == ConfigName, Value == ConfigType {
  35. mutating func set(_ type: ConfigType) {
  36. self[type.name] = type
  37. }
  38.  
  39. func getColor(_ name: ConfigName, _ defColor: UIColor = .clear) -> UIColor {
  40. switch name {
  41. case .primaryLabelColor, .secondaryLabelColor, .tertiaryLabelColor:
  42. if let type: ConfigType = self[name] {
  43. switch type {
  44. case let .primaryLabelColor(color): return color
  45. case let .secondaryLabelColor(color): return color
  46. case let .tertiaryLabelColor(color): return color
  47. default: return defColor
  48. }
  49. }
  50. default:
  51. return defColor
  52. }
  53. return defColor
  54. }
  55.  
  56. func getFont(
  57. _ name: ConfigName,
  58. _ defFont: UIFont = .systemFont(ofSize: 10)
  59. ) -> UIFont {
  60. switch name {
  61. case .primaryFont, .secondaryFont, .tertiaryFont:
  62. if let type: ConfigType = self[name] {
  63. switch type {
  64. case let .primaryFont(font): return font
  65. case let .secondaryFont(font): return font
  66. case let .tertiaryFont(font): return font
  67. default: return defFont
  68. }
  69. }
  70. default:
  71. return defFont
  72. }
  73.  
  74. return defFont
  75. }
  76.  
  77. mutating func apply(dict: Dictionary<ConfigName,ConfigType>) {
  78. for value in dict.values {
  79. self[value.name] = value
  80. }
  81. }
  82. }
  83.  
  84. var config = [ConfigName:ConfigType]()
  85. var config2 = [ConfigName:ConfigType]()
  86.  
  87. config[.primaryFont] = .primaryFont(.systemFont(ofSize: 12))
  88. config.set(.secondaryFont(.systemFont(ofSize: 18)))
  89. config.set(.primaryLabelColor(.red))
  90.  
  91. config2.set(.primaryLabelColor(.brown))
  92. config.apply(dict: config2)
  93.  
  94. config.getFont(.primaryFont) // UIFont.systemFont(ofSize: 12)
  95. config.getFont(.secondaryFont) // UIFont.systemFont(ofSize: 18)
  96. config.getColor(.primaryLabelColor) // UIColor.red
  97. config.getColor(.tertiaryLabelColor) // UIColor.clear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement