NCBrand.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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: - Configuration
  25. @objc class NCBrandConfiguration: NSObject {
  26. @objc static let shared: NCBrandConfiguration = {
  27. let instance = NCBrandConfiguration()
  28. return instance
  29. }()
  30. @objc public let configuration_serverUrl: String = "serverUrl"
  31. @objc public let configuration_username: String = "username"
  32. @objc public let configuration_password: String = "password"
  33. }
  34. // MARK: - Options
  35. @objc class NCBrandOptions: NSObject {
  36. @objc static let shared: NCBrandOptions = {
  37. let instance = NCBrandOptions()
  38. return instance
  39. }()
  40. @objc public var brand: String = "Nextcloud"
  41. // @objc public var mailMe: String = "ios@nextcloud.com" // Deprecated
  42. @objc public var textCopyrightNextcloudiOS: String = "Nextcloud Liquid for iOS %@ © 2022"
  43. @objc public var textCopyrightNextcloudServer: String = "Nextcloud Server %@"
  44. @objc public var loginBaseUrl: String = "https://cloud.nextcloud.com"
  45. @objc public var pushNotificationServerProxy: String = "https://push-notifications.nextcloud.com"
  46. @objc public var linkLoginHost: String = "https://nextcloud.com/install"
  47. @objc public var linkloginPreferredProviders: String = "https://nextcloud.com/signup-ios"
  48. @objc public var webLoginAutenticationProtocol: String = "nc://" // example "abc://"
  49. @objc public var privacy: String = "https://nextcloud.com/privacy"
  50. @objc public var sourceCode: String = "https://github.com/nextcloud/ios"
  51. // Personalized
  52. @objc public var webCloseViewProtocolPersonalized: String = "" // example "abc://change/plan" Don't touch me !!
  53. @objc public var folderBrandAutoUpload: String = "" // example "_auto_upload_folder_" Don't touch me !!
  54. // Auto Upload default folder
  55. @objc public var folderDefaultAutoUpload: String = "Photos"
  56. // Capabilities Group
  57. @objc public var capabilitiesGroups: String = "group.it.twsweb.Crypto-Cloud"
  58. // User Agent
  59. @objc public var userAgent: String = "Nextcloud-iOS" // Don't touch me !!
  60. // Brand
  61. @objc public var use_login_web_personalized: Bool = false // Don't touch me !!
  62. // Options
  63. @objc public var use_default_auto_upload: Bool = false
  64. @objc public var use_themingColor: Bool = true
  65. @objc public var use_themingLogo: Bool = false
  66. @objc public var use_storeLocalAutoUploadAll: Bool = false
  67. @objc public var use_loginflowv2: Bool = false // Don't touch me !!
  68. @objc public var disable_intro: Bool = false
  69. @objc public var disable_request_login_url: Bool = false
  70. @objc public var disable_multiaccount: Bool = false
  71. @objc public var disable_manage_account: Bool = false
  72. @objc public var disable_more_external_site: Bool = false
  73. @objc public var disable_openin_file: Bool = false // Don't touch me !!
  74. @objc public var disable_crash_service: Bool = false
  75. @objc public var disable_request_account: Bool = false
  76. @objc public var disable_log: Bool = false
  77. override init() {
  78. if folderBrandAutoUpload != "" {
  79. folderDefaultAutoUpload = folderBrandAutoUpload
  80. }
  81. }
  82. }
  83. // MARK: - Color
  84. class NCBrandColor: NSObject {
  85. @objc static let shared: NCBrandColor = {
  86. let instance = NCBrandColor()
  87. return instance
  88. }()
  89. struct cacheImages {
  90. static var file = UIImage()
  91. static var shared = UIImage()
  92. static var canShare = UIImage()
  93. static var shareByLink = UIImage()
  94. static var favorite = UIImage()
  95. static var comment = UIImage()
  96. static var livePhoto = UIImage()
  97. static var offlineFlag = UIImage()
  98. static var local = UIImage()
  99. static var folderEncrypted = UIImage()
  100. static var folderSharedWithMe = UIImage()
  101. static var folderPublic = UIImage()
  102. static var folderGroup = UIImage()
  103. static var folderExternal = UIImage()
  104. static var folderAutomaticUpload = UIImage()
  105. static var folder = UIImage()
  106. static var checkedYes = UIImage()
  107. static var checkedNo = UIImage()
  108. static var buttonMore = UIImage()
  109. static var buttonStop = UIImage()
  110. static var buttonMoreLock = UIImage()
  111. static var buttonRestore = UIImage()
  112. static var buttonTrash = UIImage()
  113. static var iconContacts = UIImage()
  114. static var iconTalk = UIImage()
  115. static var iconCalendar = UIImage()
  116. static var iconDeck = UIImage()
  117. static var iconMail = UIImage()
  118. static var iconConfirm = UIImage()
  119. static var iconPages = UIImage()
  120. }
  121. // Color
  122. @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
  123. @objc public var customerText: UIColor = .white
  124. @objc public var brand: UIColor // don't touch me
  125. @objc public var brandElement: UIColor // don't touch me
  126. @objc public var brandText: UIColor // don't touch me
  127. @objc public let nextcloud: UIColor = UIColor(red: 0.0/255.0, green: 130.0/255.0, blue: 201.0/255.0, alpha: 1.0)
  128. @objc public let gray: UIColor = UIColor(red: 104.0/255.0, green: 104.0/255.0, blue: 104.0/255.0, alpha: 1.0)
  129. @objc public let lightGray: UIColor = UIColor(red: 229.0/255.0, green: 229.0/229.0, blue: 104.0/255.0, alpha: 1.0)
  130. @objc public let yellowFavorite: UIColor = UIColor(red: 248.0/255.0, green: 205.0/255.0, blue: 70.0/255.0, alpha: 1.0)
  131. public var userColors: [CGColor] = []
  132. public var themingColor: String = ""
  133. public var themingColorElement: String = ""
  134. public var themingColorText: String = ""
  135. @objc public var systemMint: UIColor {
  136. get {
  137. return UIColor(red: 0.0 / 255.0, green: 199.0 / 255.0, blue: 190.0 / 255.0, alpha: 1.0)
  138. }
  139. }
  140. @objc public var systemGray1: UIColor {
  141. get {
  142. return UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha: 1.0)
  143. }
  144. }
  145. override init() {
  146. brand = customer
  147. brandElement = customer
  148. brandText = customerText
  149. }
  150. func createUserColors() {
  151. userColors = generateColors()
  152. }
  153. func createImagesThemingColor() {
  154. let gray: UIColor = UIColor(red: 162.0/255.0, green: 162.0/255.0, blue: 162.0/255.0, alpha: 0.5)
  155. cacheImages.file = UIImage(named: "file")!
  156. cacheImages.shared = UIImage(named: "share")!.image(color: gray, size: 50)
  157. cacheImages.canShare = UIImage(named: "share")!.image(color: gray, size: 50)
  158. cacheImages.shareByLink = UIImage(named: "sharebylink")!.image(color: gray, size: 50)
  159. cacheImages.favorite = NCUtility.shared.loadImage(named: "star.fill", color: yellowFavorite)
  160. cacheImages.comment = UIImage(named: "comment")!.image(color: gray, size: 50)
  161. cacheImages.livePhoto = NCUtility.shared.loadImage(named: "livephoto", color: .label)
  162. cacheImages.offlineFlag = UIImage(named: "offlineFlag")!
  163. cacheImages.local = UIImage(named: "local")!
  164. let folderWidth: CGFloat = UIScreen.main.bounds.width / 3
  165. cacheImages.folderEncrypted = UIImage(named: "folderEncrypted")!.image(color: brandElement, size: folderWidth)
  166. cacheImages.folderSharedWithMe = UIImage(named: "folder_shared_with_me")!.image(color: brandElement, size: folderWidth)
  167. cacheImages.folderPublic = UIImage(named: "folder_public")!.image(color: brandElement, size: folderWidth)
  168. cacheImages.folderGroup = UIImage(named: "folder_group")!.image(color: brandElement, size: folderWidth)
  169. cacheImages.folderExternal = UIImage(named: "folder_external")!.image(color: brandElement, size: folderWidth)
  170. cacheImages.folderAutomaticUpload = UIImage(named: "folderAutomaticUpload")!.image(color: brandElement, size: folderWidth)
  171. cacheImages.folder = UIImage(named: "folder")!.image(color: brandElement, size: folderWidth)
  172. cacheImages.checkedYes = NCUtility.shared.loadImage(named: "checkmark.circle.fill", color: .systemBlue)
  173. cacheImages.checkedNo = NCUtility.shared.loadImage(named: "circle", color: gray)
  174. cacheImages.buttonMore = UIImage(named: "more")!.image(color: gray, size: 50)
  175. cacheImages.buttonStop = UIImage(named: "stop")!.image(color: gray, size: 50)
  176. cacheImages.buttonMoreLock = UIImage(named: "moreLock")!.image(color: gray, size: 50)
  177. cacheImages.buttonRestore = UIImage(named: "restore")!.image(color: gray, size: 50)
  178. cacheImages.buttonTrash = UIImage(named: "trash")!.image(color: gray, size: 50)
  179. cacheImages.iconContacts = UIImage(named: "icon-contacts")!.image(color: brandElement, size: folderWidth)
  180. cacheImages.iconTalk = UIImage(named: "icon-talk")!.image(color: brandElement, size: folderWidth)
  181. cacheImages.iconCalendar = UIImage(named: "icon-calendar")!.image(color: brandElement, size: folderWidth)
  182. cacheImages.iconDeck = UIImage(named: "icon-deck")!.image(color: brandElement, size: folderWidth)
  183. cacheImages.iconMail = UIImage(named: "icon-mail")!.image(color: brandElement, size: folderWidth)
  184. cacheImages.iconConfirm = UIImage(named: "icon-confirm")!.image(color: brandElement, size: folderWidth)
  185. cacheImages.iconPages = UIImage(named: "icon-pages")!.image(color: brandElement, size: folderWidth)
  186. }
  187. func settingThemingColor(account: String) {
  188. let darker: CGFloat = 30 // %
  189. let lighter: CGFloat = 30 // %
  190. if NCBrandOptions.shared.use_themingColor {
  191. if let themingColor = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColor),
  192. let themingColorElement = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColorElement),
  193. let themingColorText = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColorText) {
  194. self.themingColor = themingColor
  195. self.themingColorElement = themingColorElement
  196. self.themingColorText = themingColorText
  197. // COLOR
  198. if themingColor.first == "#" {
  199. if let color = UIColor(hex: themingColor) {
  200. brand = color
  201. } else {
  202. brand = customer
  203. }
  204. } else {
  205. brand = customer
  206. }
  207. // COLOR TEXT
  208. if themingColorText.first == "#" {
  209. if let color = UIColor(hex: themingColorText) {
  210. brandText = color
  211. } else {
  212. brandText = customerText
  213. }
  214. } else {
  215. brandText = customerText
  216. }
  217. // COLOR ELEMENT
  218. if themingColorElement.first == "#" {
  219. if let color = UIColor(hex: themingColorElement) {
  220. brandElement = color
  221. } else {
  222. brandElement = brand
  223. }
  224. } else {
  225. brandElement = brand
  226. }
  227. }
  228. if brandElement.isTooLight() {
  229. if let color = brandElement.darker(by: darker) {
  230. brandElement = color
  231. }
  232. } else if brandElement.isTooDark() {
  233. if let color = brandElement.lighter(by: lighter) {
  234. brandElement = color
  235. }
  236. }
  237. } else {
  238. if self.customer.isTooLight() {
  239. if let color = customer.darker(by: darker) {
  240. brandElement = color
  241. }
  242. } else if customer.isTooDark() {
  243. if let color = customer.lighter(by: lighter) {
  244. brandElement = color
  245. }
  246. } else {
  247. brandElement = customer
  248. }
  249. brand = customer
  250. brandText = customerText
  251. }
  252. createImagesThemingColor()
  253. #if !EXTENSION
  254. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeTheming)
  255. #endif
  256. }
  257. private func stepCalc(steps: Int, color1: CGColor, color2: CGColor) -> [CGFloat] {
  258. var step = [CGFloat](repeating: 0, count: 3)
  259. step[0] = (color2.components![0] - color1.components![0]) / CGFloat(steps)
  260. step[1] = (color2.components![1] - color1.components![1]) / CGFloat(steps)
  261. step[2] = (color2.components![2] - color1.components![2]) / CGFloat(steps)
  262. return step
  263. }
  264. private func mixPalette(steps: Int, color1: CGColor, color2: CGColor) -> [CGColor] {
  265. var palette = [color1]
  266. let step = stepCalc(steps: steps, color1: color1, color2: color2)
  267. let c1Components = color1.components!
  268. for i in 1 ..< steps {
  269. let r = c1Components[0] + step[0] * CGFloat(i)
  270. let g = c1Components[1] + step[1] * CGFloat(i)
  271. let b = c1Components[2] + step[2] * CGFloat(i)
  272. palette.append(UIColor(red: r, green: g, blue: b, alpha: 1).cgColor)
  273. }
  274. return palette
  275. }
  276. /**
  277. Generate colors from the official nextcloud color.
  278. You can provide how many colors you want (multiplied by 3).
  279. if `step` = 6,
  280. 3 colors \* 6 will result in 18 generated colors
  281. */
  282. func generateColors(steps: Int = 6) -> [CGColor] {
  283. let red = UIColor(red: 182/255, green: 70/255, blue: 157/255, alpha: 1).cgColor
  284. let yellow = UIColor(red: 221/255, green: 203/255, blue: 85/255, alpha: 1).cgColor
  285. let blue = UIColor(red: 0/255, green: 130/255, blue: 201/255, alpha: 1).cgColor
  286. let palette1 = mixPalette(steps: steps, color1: red, color2: yellow)
  287. let palette2 = mixPalette(steps: steps, color1: yellow, color2: blue)
  288. let palette3 = mixPalette(steps: steps, color1: blue, color2: red)
  289. return palette1 + palette2 + palette3
  290. }
  291. }