PresentationTheme.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. init(isDark: Bool,
  34. name: String,
  35. statusBarStyle: UIStatusBarStyle,
  36. navigationbarColor: UIColor,
  37. navigationbarTextColor: UIColor,
  38. background: UIColor,
  39. cellBackgroundA: UIColor,
  40. cellBackgroundB: UIColor,
  41. cellDetailTextColor: UIColor,
  42. cellTextColor: UIColor,
  43. lightTextColor: UIColor,
  44. sectionHeaderTextColor: UIColor,
  45. separatorColor: UIColor,
  46. mediaCategorySeparatorColor: UIColor,
  47. tabBarColor: UIColor,
  48. orangeUI: UIColor) {
  49. self.isDark = isDark
  50. self.name = name
  51. self.statusBarStyle = statusBarStyle
  52. self.navigationbarColor = navigationbarColor
  53. self.navigationbarTextColor = navigationbarTextColor
  54. self.background = background
  55. self.cellBackgroundA = cellBackgroundA
  56. self.cellBackgroundB = cellBackgroundB
  57. self.cellDetailTextColor = cellDetailTextColor
  58. self.cellTextColor = cellTextColor
  59. self.lightTextColor = lightTextColor
  60. self.sectionHeaderTextColor = sectionHeaderTextColor
  61. self.separatorColor = separatorColor
  62. self.mediaCategorySeparatorColor = mediaCategorySeparatorColor
  63. self.tabBarColor = tabBarColor
  64. self.orangeUI = orangeUI
  65. }
  66. }
  67. @objcMembers class Typography: NSObject {
  68. let tableHeaderFont: UIFont
  69. init(tableHeaderFont: UIFont) {
  70. self.tableHeaderFont = tableHeaderFont
  71. }
  72. }
  73. @objcMembers class PresentationTheme: NSObject {
  74. static let brightTheme = PresentationTheme(colors: brightPalette)
  75. static let darkTheme = PresentationTheme(colors: darkPalette)
  76. static var current: PresentationTheme = {
  77. let isDarkTheme = UserDefaults.standard.bool(forKey: kVLCSettingAppTheme)
  78. return isDarkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme
  79. }() {
  80. didSet {
  81. AppearanceManager.setupAppearance(theme: self.current)
  82. NotificationCenter.default.post(name: .VLCThemeDidChangeNotification, object: self)
  83. }
  84. }
  85. init(colors: ColorPalette) {
  86. self.colors = colors
  87. super.init()
  88. }
  89. let colors: ColorPalette
  90. let font = defaultFont
  91. }
  92. @objc extension UIColor {
  93. convenience init(_ rgbValue: UInt32, _ alpha: CGFloat = 1.0) {
  94. let r = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
  95. let g = CGFloat((rgbValue & 0xFF00) >> 8) / 255.0
  96. let b = CGFloat(rgbValue & 0xFF) / 255.0
  97. self.init(red: r, green: g, blue: b, alpha: 1.0)
  98. }
  99. private func toHex(alpha: Bool = false) -> String? {
  100. guard let components = cgColor.components, components.count >= 3 else {
  101. assertionFailure()
  102. return nil
  103. }
  104. let r = Float(components[0])
  105. let g = Float(components[1])
  106. let b = Float(components[2])
  107. var a = Float(1.0)
  108. if components.count == 4 {
  109. a = Float(components[3])
  110. }
  111. if alpha {
  112. return String(format: "#%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  113. } else {
  114. return String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  115. }
  116. }
  117. var toHex: String? {
  118. return toHex()
  119. }
  120. }
  121. let brightPalette = ColorPalette(isDark: false,
  122. name: "Default",
  123. statusBarStyle: .default,
  124. navigationbarColor: UIColor(0xFFFFFF),
  125. navigationbarTextColor: UIColor(0x000000),
  126. background: UIColor(0xF9F9F7),
  127. cellBackgroundA: UIColor(0xF9F9F7),
  128. cellBackgroundB: UIColor(0xE5E5E3),
  129. cellDetailTextColor: UIColor(0xA9A9A9),
  130. cellTextColor: UIColor(0x000000),
  131. lightTextColor: UIColor(0x888888),
  132. sectionHeaderTextColor: UIColor(0x25292C),
  133. separatorColor: UIColor(0xF0F2F7),
  134. mediaCategorySeparatorColor: UIColor(0xECF2F6),
  135. tabBarColor: UIColor(0xFFFFFF),
  136. orangeUI: UIColor(0xFF8800))
  137. let darkPalette = ColorPalette(isDark: true,
  138. name: "Dark",
  139. statusBarStyle: .lightContent,
  140. navigationbarColor: UIColor(0x292B36),
  141. navigationbarTextColor: UIColor(0xD3D3D3),
  142. background: UIColor(0x1B1E21),
  143. cellBackgroundA: UIColor(0x292B36),
  144. cellBackgroundB: UIColor(0x000000),
  145. cellDetailTextColor: UIColor(0xD3D3D3),
  146. cellTextColor: UIColor(0xFFFFFF),
  147. lightTextColor: UIColor(0xB8B8B8),
  148. sectionHeaderTextColor: UIColor(0xFFFFFF),
  149. separatorColor: UIColor(0x25292C),
  150. mediaCategorySeparatorColor: UIColor(0x25292C),
  151. tabBarColor: UIColor(0x292B36),
  152. orangeUI: UIColor(0xFF8800))
  153. let defaultFont = Typography(tableHeaderFont: UIFont.systemFont(ofSize: 24, weight: .semibold))