PresentationTheme.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*****************************************************************************
  2. * PresentationTheme.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <caro # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. extension Notification.Name {
  14. static let VLCThemeDidChangeNotification = Notification.Name("themeDidChangeNotfication")
  15. }
  16. @objcMembers class ColorPalette: NSObject {
  17. let isDark: Bool
  18. let name: String
  19. let statusBarStyle: UIStatusBarStyle
  20. let navigationbarColor: UIColor
  21. let navigationbarTextColor: UIColor
  22. let background: UIColor
  23. let cellBackgroundA: UIColor
  24. let cellBackgroundB: UIColor
  25. let cellDetailTextColor: UIColor
  26. let cellTextColor: UIColor
  27. let lightTextColor: UIColor
  28. let sectionHeaderTextColor: UIColor
  29. let separatorColor: UIColor
  30. let mediaCategorySeparatorColor: UIColor
  31. let tabBarColor: UIColor
  32. let orangeUI: UIColor
  33. let toolBarStyle: UIBarStyle
  34. init(isDark: Bool,
  35. name: String,
  36. statusBarStyle: UIStatusBarStyle,
  37. navigationbarColor: UIColor,
  38. navigationbarTextColor: UIColor,
  39. background: UIColor,
  40. cellBackgroundA: UIColor,
  41. cellBackgroundB: UIColor,
  42. cellDetailTextColor: UIColor,
  43. cellTextColor: UIColor,
  44. lightTextColor: UIColor,
  45. sectionHeaderTextColor: UIColor,
  46. separatorColor: UIColor,
  47. mediaCategorySeparatorColor: UIColor,
  48. tabBarColor: UIColor,
  49. orangeUI: UIColor,
  50. toolBarStyle: UIBarStyle) {
  51. self.isDark = isDark
  52. self.name = name
  53. self.statusBarStyle = statusBarStyle
  54. self.navigationbarColor = navigationbarColor
  55. self.navigationbarTextColor = navigationbarTextColor
  56. self.background = background
  57. self.cellBackgroundA = cellBackgroundA
  58. self.cellBackgroundB = cellBackgroundB
  59. self.cellDetailTextColor = cellDetailTextColor
  60. self.cellTextColor = cellTextColor
  61. self.lightTextColor = lightTextColor
  62. self.sectionHeaderTextColor = sectionHeaderTextColor
  63. self.separatorColor = separatorColor
  64. self.mediaCategorySeparatorColor = mediaCategorySeparatorColor
  65. self.tabBarColor = tabBarColor
  66. self.orangeUI = orangeUI
  67. self.toolBarStyle = toolBarStyle
  68. }
  69. }
  70. @objcMembers class Typography: NSObject {
  71. let tableHeaderFont: UIFont
  72. init(tableHeaderFont: UIFont) {
  73. self.tableHeaderFont = tableHeaderFont
  74. }
  75. }
  76. @objcMembers class PresentationTheme: NSObject {
  77. static let brightTheme = PresentationTheme(colors: brightPalette)
  78. static let darkTheme = PresentationTheme(colors: darkPalette)
  79. static var current: PresentationTheme = {
  80. let isDarkTheme = UserDefaults.standard.bool(forKey: kVLCSettingAppTheme)
  81. return isDarkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme
  82. }() {
  83. didSet {
  84. AppearanceManager.setupAppearance(theme: self.current)
  85. NotificationCenter.default.post(name: .VLCThemeDidChangeNotification, object: self)
  86. }
  87. }
  88. init(colors: ColorPalette) {
  89. self.colors = colors
  90. super.init()
  91. }
  92. let colors: ColorPalette
  93. let font = defaultFont
  94. }
  95. @objc extension UIColor {
  96. convenience init(_ rgbValue: UInt32, _ alpha: CGFloat = 1.0) {
  97. let r = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
  98. let g = CGFloat((rgbValue & 0xFF00) >> 8) / 255.0
  99. let b = CGFloat(rgbValue & 0xFF) / 255.0
  100. self.init(red: r, green: g, blue: b, alpha: 1.0)
  101. }
  102. private func toHex(alpha: Bool = false) -> String? {
  103. guard let components = cgColor.components, components.count >= 3 else {
  104. assertionFailure()
  105. return nil
  106. }
  107. let r = Float(components[0])
  108. let g = Float(components[1])
  109. let b = Float(components[2])
  110. var a = Float(1.0)
  111. if components.count == 4 {
  112. a = Float(components[3])
  113. }
  114. if alpha {
  115. return String(format: "#%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  116. } else {
  117. return String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  118. }
  119. }
  120. var toHex: String? {
  121. return toHex()
  122. }
  123. }
  124. let brightPalette = ColorPalette(isDark: false,
  125. name: "Default",
  126. statusBarStyle: .default,
  127. navigationbarColor: UIColor(0xFFFFFF),
  128. navigationbarTextColor: UIColor(0x000000),
  129. background: UIColor(0xFFFFFF),
  130. cellBackgroundA: UIColor(0xF9F9F7),
  131. cellBackgroundB: UIColor(0xE5E5E3),
  132. cellDetailTextColor: UIColor(0xA9A9A9),
  133. cellTextColor: UIColor(0x000000),
  134. lightTextColor: UIColor(0x888888),
  135. sectionHeaderTextColor: UIColor(0x25292C),
  136. separatorColor: UIColor(0xF0F2F7),
  137. mediaCategorySeparatorColor: UIColor(0xECF2F6),
  138. tabBarColor: UIColor(0xFFFFFF),
  139. orangeUI: UIColor(0xFF8800),
  140. toolBarStyle: UIBarStyle.default)
  141. let darkPalette = ColorPalette(isDark: true,
  142. name: "Dark",
  143. statusBarStyle: .lightContent,
  144. navigationbarColor: UIColor(0x1B1E21),
  145. navigationbarTextColor: UIColor(0xFFFFFF),
  146. background: UIColor(0x1B1E21),
  147. cellBackgroundA: UIColor(0x1B1E21),
  148. cellBackgroundB: UIColor(0x000000),
  149. cellDetailTextColor: UIColor(0xD3D3D3),
  150. cellTextColor: UIColor(0xFFFFFF),
  151. lightTextColor: UIColor(0xB8B8B8),
  152. sectionHeaderTextColor: UIColor(0x828282),
  153. separatorColor: UIColor(0x25292C),
  154. mediaCategorySeparatorColor: UIColor(0x25292C),
  155. tabBarColor: UIColor(0x25292C),
  156. orangeUI: UIColor(0xFF8800),
  157. toolBarStyle: UIBarStyle.black)
  158. let defaultFont = Typography(tableHeaderFont: UIFont.systemFont(ofSize: 24, weight: .semibold))