NCBrand.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // NCBrandColor.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/04/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. // MARK: - Options
  25. @objc class NCBrandOptions: NSObject {
  26. @objc static let shared: NCBrandOptions = {
  27. let instance = NCBrandOptions()
  28. return instance
  29. }()
  30. @objc public var brand: String = "Nextcloud"
  31. @objc public var textCopyrightNextcloudiOS: String = "Nextcloud Liquid for iOS %@ © 2022"
  32. @objc public var textCopyrightNextcloudServer: String = "Nextcloud Server %@"
  33. @objc public var loginBaseUrl: String = "https://cloud.nextcloud.com"
  34. @objc public var pushNotificationServerProxy: String = "https://push-notifications.nextcloud.com"
  35. @objc public var linkLoginHost: String = "https://nextcloud.com/install"
  36. @objc public var linkloginPreferredProviders: String = "https://nextcloud.com/signup-ios"
  37. @objc public var webLoginAutenticationProtocol: String = "nc://" // example "abc://"
  38. @objc public var privacy: String = "https://nextcloud.com/privacy"
  39. @objc public var sourceCode: String = "https://github.com/nextcloud/ios"
  40. // Personalized
  41. @objc public var webCloseViewProtocolPersonalized: String = "" // example "abc://change/plan" Don't touch me !!
  42. @objc public var folderBrandAutoUpload: String = "" // example "_auto_upload_folder_" Don't touch me !!
  43. // Auto Upload default folder
  44. @objc public var folderDefaultAutoUpload: String = "Photos"
  45. // Capabilities Group
  46. @objc public var capabilitiesGroups: String = "group.it.twsweb.Crypto-Cloud"
  47. // User Agent
  48. @objc public var userAgent: String = "Nextcloud-iOS" // Don't touch me !!
  49. // BRAND ONLY
  50. @objc public var use_login_web_personalized: Bool = false // Don't touch me !!
  51. @objc public var use_AppConfig: Bool = false // Don't touch me !!
  52. // Options
  53. @objc public var use_default_auto_upload: Bool = false
  54. @objc public var use_themingColor: Bool = true
  55. @objc public var use_themingLogo: Bool = false
  56. @objc public var use_storeLocalAutoUploadAll: Bool = false
  57. @objc public var use_loginflowv2: Bool = false // Don't touch me !!
  58. @objc public var disable_intro: Bool = false
  59. @objc public var disable_request_login_url: Bool = false
  60. @objc public var disable_multiaccount: Bool = false
  61. @objc public var disable_manage_account: Bool = false
  62. @objc public var disable_more_external_site: Bool = false
  63. @objc public var disable_openin_file: Bool = false // Don't touch me !!
  64. @objc public var disable_crash_service: Bool = false
  65. @objc public var disable_log: Bool = false
  66. override init() {
  67. if folderBrandAutoUpload != "" {
  68. folderDefaultAutoUpload = folderBrandAutoUpload
  69. }
  70. // wrapper AppConfig
  71. if let configurationManaged = UserDefaults.standard.dictionary(forKey: "com.apple.configuration.managed"), use_AppConfig {
  72. if let str = configurationManaged[NCGlobal.shared.configuration_brand] as? String {
  73. brand = str
  74. }
  75. if let str = configurationManaged[NCGlobal.shared.configuration_disable_intro] as? String {
  76. disable_intro = (str as NSString).boolValue
  77. }
  78. if let str = configurationManaged[NCGlobal.shared.configuration_disable_multiaccount] as? String {
  79. disable_multiaccount = (str as NSString).boolValue
  80. }
  81. if let str = configurationManaged[NCGlobal.shared.configuration_disable_crash_service] as? String {
  82. disable_crash_service = (str as NSString).boolValue
  83. }
  84. if let str = configurationManaged[NCGlobal.shared.configuration_disable_log] as? String {
  85. disable_log = (str as NSString).boolValue
  86. }
  87. }
  88. }
  89. }
  90. // MARK: - Color
  91. class NCBrandColor: NSObject {
  92. @objc static let shared: NCBrandColor = {
  93. let instance = NCBrandColor()
  94. return instance
  95. }()
  96. struct cacheImages {
  97. static var file = UIImage()
  98. static var shared = UIImage()
  99. static var canShare = UIImage()
  100. static var shareByLink = UIImage()
  101. static var favorite = UIImage()
  102. static var comment = UIImage()
  103. static var livePhoto = UIImage()
  104. static var offlineFlag = UIImage()
  105. static var local = UIImage()
  106. static var folderEncrypted = UIImage()
  107. static var folderSharedWithMe = UIImage()
  108. static var folderPublic = UIImage()
  109. static var folderGroup = UIImage()
  110. static var folderExternal = UIImage()
  111. static var folderAutomaticUpload = UIImage()
  112. static var folder = UIImage()
  113. static var checkedYes = UIImage()
  114. static var checkedNo = UIImage()
  115. static var buttonMore = UIImage()
  116. static var buttonStop = UIImage()
  117. static var buttonMoreLock = UIImage()
  118. static var buttonRestore = UIImage()
  119. static var buttonTrash = UIImage()
  120. static var iconContacts = UIImage()
  121. static var iconTalk = UIImage()
  122. static var iconCalendar = UIImage()
  123. static var iconDeck = UIImage()
  124. static var iconMail = UIImage()
  125. static var iconConfirm = UIImage()
  126. static var iconPages = UIImage()
  127. }
  128. // Color
  129. @objc public let customer: UIColor = UIColor(red: 0.0/255.0, green: 130.0/255.0, blue: 201.0/255.0, alpha: 1.0) // BLU NC : #0082c9
  130. @objc public var customerText: UIColor = .white
  131. @objc public var brand: UIColor // don't touch me
  132. @objc public var brandElement: UIColor // don't touch me
  133. @objc public var brandText: UIColor // don't touch me
  134. @objc public let nextcloud: UIColor = UIColor(red: 0.0/255.0, green: 130.0/255.0, blue: 201.0/255.0, alpha: 1.0)
  135. @objc public let gray: UIColor = UIColor(red: 104.0/255.0, green: 104.0/255.0, blue: 104.0/255.0, alpha: 1.0)
  136. @objc public let lightGray: UIColor = UIColor(red: 229.0/255.0, green: 229.0/229.0, blue: 104.0/255.0, alpha: 1.0)
  137. @objc public let yellowFavorite: UIColor = UIColor(red: 248.0/255.0, green: 205.0/255.0, blue: 70.0/255.0, alpha: 1.0)
  138. public var userColors: [CGColor] = []
  139. public var themingColor: String = ""
  140. public var themingColorElement: String = ""
  141. public var themingColorText: String = ""
  142. @objc public var systemMint: UIColor {
  143. get {
  144. return UIColor(red: 0.0 / 255.0, green: 199.0 / 255.0, blue: 190.0 / 255.0, alpha: 1.0)
  145. }
  146. }
  147. @objc public var systemGray1: UIColor {
  148. get {
  149. return UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha: 1.0)
  150. }
  151. }
  152. override init() {
  153. brand = customer
  154. brandElement = customer
  155. brandText = customerText
  156. }
  157. func createUserColors() {
  158. userColors = generateColors()
  159. }
  160. func createImagesThemingColor() {
  161. let gray: UIColor = UIColor(red: 162.0/255.0, green: 162.0/255.0, blue: 162.0/255.0, alpha: 0.5)
  162. cacheImages.file = UIImage(named: "file")!
  163. cacheImages.shared = UIImage(named: "share")!.image(color: gray, size: 50)
  164. cacheImages.canShare = UIImage(named: "share")!.image(color: gray, size: 50)
  165. cacheImages.shareByLink = UIImage(named: "sharebylink")!.image(color: gray, size: 50)
  166. cacheImages.favorite = NCUtility.shared.loadImage(named: "star.fill", color: yellowFavorite)
  167. cacheImages.comment = UIImage(named: "comment")!.image(color: gray, size: 50)
  168. cacheImages.livePhoto = NCUtility.shared.loadImage(named: "livephoto", color: .label)
  169. cacheImages.offlineFlag = UIImage(named: "offlineFlag")!
  170. cacheImages.local = UIImage(named: "local")!
  171. let folderWidth: CGFloat = UIScreen.main.bounds.width / 3
  172. cacheImages.folderEncrypted = UIImage(named: "folderEncrypted")!.image(color: brandElement, size: folderWidth)
  173. cacheImages.folderSharedWithMe = UIImage(named: "folder_shared_with_me")!.image(color: brandElement, size: folderWidth)
  174. cacheImages.folderPublic = UIImage(named: "folder_public")!.image(color: brandElement, size: folderWidth)
  175. cacheImages.folderGroup = UIImage(named: "folder_group")!.image(color: brandElement, size: folderWidth)
  176. cacheImages.folderExternal = UIImage(named: "folder_external")!.image(color: brandElement, size: folderWidth)
  177. cacheImages.folderAutomaticUpload = UIImage(named: "folderAutomaticUpload")!.image(color: brandElement, size: folderWidth)
  178. cacheImages.folder = UIImage(named: "folder")!.image(color: brandElement, size: folderWidth)
  179. cacheImages.checkedYes = NCUtility.shared.loadImage(named: "checkmark.circle.fill", color: .systemBlue)
  180. cacheImages.checkedNo = NCUtility.shared.loadImage(named: "circle", color: gray)
  181. cacheImages.buttonMore = UIImage(named: "more")!.image(color: gray, size: 50)
  182. cacheImages.buttonStop = UIImage(named: "stop")!.image(color: gray, size: 50)
  183. cacheImages.buttonMoreLock = UIImage(named: "moreLock")!.image(color: gray, size: 50)
  184. cacheImages.buttonRestore = UIImage(named: "restore")!.image(color: gray, size: 50)
  185. cacheImages.buttonTrash = UIImage(named: "trash")!.image(color: gray, size: 50)
  186. cacheImages.iconContacts = UIImage(named: "icon-contacts")!.image(color: brandElement, size: folderWidth)
  187. cacheImages.iconTalk = UIImage(named: "icon-talk")!.image(color: brandElement, size: folderWidth)
  188. cacheImages.iconCalendar = UIImage(named: "icon-calendar")!.image(color: brandElement, size: folderWidth)
  189. cacheImages.iconDeck = UIImage(named: "icon-deck")!.image(color: brandElement, size: folderWidth)
  190. cacheImages.iconMail = UIImage(named: "icon-mail")!.image(color: brandElement, size: folderWidth)
  191. cacheImages.iconConfirm = UIImage(named: "icon-confirm")!.image(color: brandElement, size: folderWidth)
  192. cacheImages.iconPages = UIImage(named: "icon-pages")!.image(color: brandElement, size: folderWidth)
  193. }
  194. func settingThemingColor(account: String) {
  195. let darker: CGFloat = 30 // %
  196. let lighter: CGFloat = 30 // %
  197. if NCBrandOptions.shared.use_themingColor {
  198. if let themingColor = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColor),
  199. let themingColorElement = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColorElement),
  200. let themingColorText = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColorText) {
  201. self.themingColor = themingColor
  202. self.themingColorElement = themingColorElement
  203. self.themingColorText = themingColorText
  204. // COLOR
  205. if themingColor.first == "#" {
  206. if let color = UIColor(hex: themingColor) {
  207. brand = color
  208. } else {
  209. brand = customer
  210. }
  211. } else {
  212. brand = customer
  213. }
  214. // COLOR TEXT
  215. if themingColorText.first == "#" {
  216. if let color = UIColor(hex: themingColorText) {
  217. brandText = color
  218. } else {
  219. brandText = customerText
  220. }
  221. } else {
  222. brandText = customerText
  223. }
  224. // COLOR ELEMENT
  225. if themingColorElement.first == "#" {
  226. if let color = UIColor(hex: themingColorElement) {
  227. brandElement = color
  228. } else {
  229. brandElement = brand
  230. }
  231. } else {
  232. brandElement = brand
  233. }
  234. }
  235. if brandElement.isTooLight() {
  236. if let color = brandElement.darker(by: darker) {
  237. brandElement = color
  238. }
  239. } else if brandElement.isTooDark() {
  240. if let color = brandElement.lighter(by: lighter) {
  241. brandElement = color
  242. }
  243. }
  244. } else {
  245. if self.customer.isTooLight() {
  246. if let color = customer.darker(by: darker) {
  247. brandElement = color
  248. }
  249. } else if customer.isTooDark() {
  250. if let color = customer.lighter(by: lighter) {
  251. brandElement = color
  252. }
  253. } else {
  254. brandElement = customer
  255. }
  256. brand = customer
  257. brandText = customerText
  258. }
  259. createImagesThemingColor()
  260. #if !EXTENSION
  261. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeTheming)
  262. #endif
  263. }
  264. private func stepCalc(steps: Int, color1: CGColor, color2: CGColor) -> [CGFloat] {
  265. var step = [CGFloat](repeating: 0, count: 3)
  266. step[0] = (color2.components![0] - color1.components![0]) / CGFloat(steps)
  267. step[1] = (color2.components![1] - color1.components![1]) / CGFloat(steps)
  268. step[2] = (color2.components![2] - color1.components![2]) / CGFloat(steps)
  269. return step
  270. }
  271. private func mixPalette(steps: Int, color1: CGColor, color2: CGColor) -> [CGColor] {
  272. var palette = [color1]
  273. let step = stepCalc(steps: steps, color1: color1, color2: color2)
  274. let c1Components = color1.components!
  275. for i in 1 ..< steps {
  276. let r = c1Components[0] + step[0] * CGFloat(i)
  277. let g = c1Components[1] + step[1] * CGFloat(i)
  278. let b = c1Components[2] + step[2] * CGFloat(i)
  279. palette.append(UIColor(red: r, green: g, blue: b, alpha: 1).cgColor)
  280. }
  281. return palette
  282. }
  283. /**
  284. Generate colors from the official nextcloud color.
  285. You can provide how many colors you want (multiplied by 3).
  286. if `step` = 6,
  287. 3 colors \* 6 will result in 18 generated colors
  288. */
  289. func generateColors(steps: Int = 6) -> [CGColor] {
  290. let red = UIColor(red: 182/255, green: 70/255, blue: 157/255, alpha: 1).cgColor
  291. let yellow = UIColor(red: 221/255, green: 203/255, blue: 85/255, alpha: 1).cgColor
  292. let blue = UIColor(red: 0/255, green: 130/255, blue: 201/255, alpha: 1).cgColor
  293. let palette1 = mixPalette(steps: steps, color1: red, color2: yellow)
  294. let palette2 = mixPalette(steps: steps, color1: yellow, color2: blue)
  295. let palette3 = mixPalette(steps: steps, color1: blue, color2: red)
  296. return palette1 + palette2 + palette3
  297. }
  298. }