SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Theme.swift
  3. //  FlowrSpot
  4. //
  5. //  Created by Marin Nikolic on 23/07/2019.
  6. //  Copyright © 2019 PovioLabs. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11.  
  12. //MARK - Configuration Handler
  13. extension UIColor {
  14.     private class ThemeService: NSObject {
  15.         public static let instance = ThemeService()
  16.         private var colorDictionary: Dictionary<String, String>?
  17.        
  18.         private override init() {
  19.             super.init()
  20.             loadThemeConfigIntoDictionary()
  21.         }
  22.        
  23.         private func loadThemeConfigIntoDictionary() {
  24.             if let jsonPath = Bundle.main.path(forResource: "ThemeConfig", ofType: "json") {
  25.                 do {
  26.                     let data = try Data(contentsOf: URL(fileURLWithPath: jsonPath), options: .mappedIfSafe)
  27.                     self.colorDictionary = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? Dictionary<String, String>
  28.                 } catch {
  29.                     fatalError("ThemeConfig file is missing, add theme Config file to Resources folder")
  30.                 }
  31.             }
  32.         }
  33.        
  34.         public func colorForKey(key: String) -> UIColor {
  35.             _ = colorDictionary?[key]
  36.             return UIColor(named: "") ?? UIColor.white
  37.         }
  38.     }
  39. }
  40.  
  41. //MARK - Structured UIColor Extension
  42. extension UIColor {
  43.    
  44.     private enum Theme: String {
  45.         case primary = "colorPrimary"
  46.         case accent = "colorAccent"
  47.         case textPrimary = "textPrimaryColor"
  48.         case textAccent = "textAccentColor"
  49.         case textPlaceholder = "textPlaceholderColor"
  50.         case windowBackground = "windowBackgroundColor"
  51.        
  52.         public var color: UIColor? {
  53.             get {
  54.                 return ThemeService.instance.colorForKey(key: self.rawValue)
  55.             }
  56.         }
  57.     }
  58.    
  59.     struct Navigation {
  60.         static let title = Theme.accent.color
  61.     }
  62.    
  63.     struct Home {
  64.         static let background = Theme.windowBackground.color
  65.         static let searchPlaceholder = Theme.textPlaceholder.color
  66.         static let searchText = Theme.textPrimary.color
  67.         static let searchIcon = Theme.accent.color
  68.         static let itemTitle = Theme.textAccent.color
  69.     }
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top