NCBrand.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 %@ © 2023"
  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. @objc public var mobileconfig: String = "/remote.php/dav/provisioning/apple-provisioning.mobileconfig"
  41. // Personalized
  42. @objc public var webCloseViewProtocolPersonalized: String = "" // example "abc://change/plan" Don't touch me !!
  43. @objc public var folderBrandAutoUpload: String = "" // example "_auto_upload_folder_" Don't touch me !!
  44. // Auto Upload default folder
  45. @objc public var folderDefaultAutoUpload: String = "Photos"
  46. // Capabilities Group
  47. @objc public var capabilitiesGroups: String = "group.it.twsweb.Crypto-Cloud"
  48. @objc public var capabilitiesGroupApps: String = "group.com.nextcloud.apps"
  49. // User Agent
  50. @objc public var userAgent: String = "Nextcloud-iOS" // Don't touch me !!
  51. // BRAND ONLY
  52. @objc public var use_login_web_personalized: Bool = false // Don't touch me !!
  53. @objc public var use_AppConfig: Bool = false // Don't touch me !!
  54. @objc public var use_GroupApps: Bool = true // Don't touch me !!
  55. // Options
  56. @objc public var use_default_auto_upload: Bool = false
  57. @objc public var use_themingColor: Bool = true
  58. @objc public var use_themingLogo: Bool = false
  59. @objc public var use_storeLocalAutoUploadAll: Bool = false
  60. @objc public var use_loginflowv2: Bool = false // Don't touch me !!
  61. @objc public var disable_intro: Bool = false
  62. @objc public var disable_request_login_url: Bool = false
  63. @objc public var disable_multiaccount: Bool = false
  64. @objc public var disable_manage_account: Bool = false
  65. @objc public var disable_more_external_site: Bool = false
  66. @objc public var disable_openin_file: Bool = false // Don't touch me !!
  67. @objc public var disable_crash_service: Bool = false
  68. @objc public var disable_log: Bool = false
  69. @objc public var disable_mobileconfig: Bool = false
  70. @objc public var disable_show_more_nextcloud_apps_in_settings: Bool = false
  71. // Internal option behaviour
  72. @objc public var cleanUpDay: Int = 0 // Set default "Delete, in the cache, all files older than" possible days value are: 0, 1, 7, 30, 90, 180, 365
  73. // Info Paging
  74. enum NCInfoPagingTab: Int, CaseIterable {
  75. case activity, sharing
  76. }
  77. override init() {
  78. if folderBrandAutoUpload != "" {
  79. folderDefaultAutoUpload = folderBrandAutoUpload
  80. }
  81. // wrapper AppConfig
  82. if let configurationManaged = UserDefaults.standard.dictionary(forKey: "com.apple.configuration.managed"), use_AppConfig {
  83. if let str = configurationManaged[NCGlobal.shared.configuration_brand] as? String {
  84. brand = str
  85. }
  86. if let str = configurationManaged[NCGlobal.shared.configuration_disable_intro] as? String {
  87. disable_intro = (str as NSString).boolValue
  88. }
  89. if let str = configurationManaged[NCGlobal.shared.configuration_disable_multiaccount] as? String {
  90. disable_multiaccount = (str as NSString).boolValue
  91. }
  92. if let str = configurationManaged[NCGlobal.shared.configuration_disable_crash_service] as? String {
  93. disable_crash_service = (str as NSString).boolValue
  94. }
  95. if let str = configurationManaged[NCGlobal.shared.configuration_disable_log] as? String {
  96. disable_log = (str as NSString).boolValue
  97. }
  98. if let str = configurationManaged[NCGlobal.shared.configuration_disable_manage_account] as? String {
  99. disable_manage_account = (str as NSString).boolValue
  100. }
  101. if let str = configurationManaged[NCGlobal.shared.configuration_disable_more_external_site] as? String {
  102. disable_more_external_site = (str as NSString).boolValue
  103. }
  104. if let str = configurationManaged[NCGlobal.shared.configuration_disable_openin_file] as? String {
  105. disable_openin_file = (str as NSString).boolValue
  106. }
  107. }
  108. }
  109. }
  110. // MARK: - Color
  111. class NCBrandColor: NSObject {
  112. @objc static let shared: NCBrandColor = {
  113. let instance = NCBrandColor()
  114. return instance
  115. }()
  116. struct cacheImages {
  117. static var file = UIImage()
  118. static var shared = UIImage()
  119. static var canShare = UIImage()
  120. static var shareByLink = UIImage()
  121. static var favorite = UIImage()
  122. static var comment = UIImage()
  123. static var livePhoto = UIImage()
  124. static var offlineFlag = UIImage()
  125. static var local = UIImage()
  126. static var folderEncrypted = UIImage()
  127. static var folderSharedWithMe = UIImage()
  128. static var folderPublic = UIImage()
  129. static var folderGroup = UIImage()
  130. static var folderExternal = UIImage()
  131. static var folderAutomaticUpload = UIImage()
  132. static var folder = UIImage()
  133. static var checkedYes = UIImage()
  134. static var checkedNo = UIImage()
  135. static var buttonMore = UIImage()
  136. static var buttonStop = UIImage()
  137. static var buttonMoreLock = UIImage()
  138. static var buttonRestore = UIImage()
  139. static var buttonTrash = UIImage()
  140. static var iconContacts = UIImage()
  141. static var iconTalk = UIImage()
  142. static var iconCalendar = UIImage()
  143. static var iconDeck = UIImage()
  144. static var iconMail = UIImage()
  145. static var iconConfirm = UIImage()
  146. static var iconPages = UIImage()
  147. }
  148. // Color
  149. @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
  150. @objc public var customerText: UIColor = .white
  151. @objc public var brand: UIColor // don't touch me
  152. @objc public var brandElement: UIColor // don't touch me
  153. @objc public var brandText: UIColor // don't touch me
  154. @objc public let nextcloud: UIColor = UIColor(red: 0.0/255.0, green: 130.0/255.0, blue: 201.0/255.0, alpha: 1.0)
  155. @objc public let yellowFavorite: UIColor = UIColor(red: 248.0/255.0, green: 205.0/255.0, blue: 70.0/255.0, alpha: 1.0)
  156. public var userColors: [CGColor] = []
  157. public var themingColor: String = ""
  158. public var themingColorElement: String = ""
  159. public var themingColorText: String = ""
  160. @objc public var systemMint: UIColor {
  161. get {
  162. return UIColor(red: 0.0 / 255.0, green: 199.0 / 255.0, blue: 190.0 / 255.0, alpha: 1.0)
  163. }
  164. }
  165. override init() {
  166. brand = customer
  167. brandElement = customer
  168. brandText = customerText
  169. }
  170. func createUserColors() {
  171. userColors = generateColors()
  172. }
  173. func createImagesThemingColor() {
  174. cacheImages.file = UIImage(named: "file")!
  175. cacheImages.shared = UIImage(named: "share")!.image(color: .systemGray, size: 50)
  176. cacheImages.canShare = UIImage(named: "share")!.image(color: .systemGray, size: 50)
  177. cacheImages.shareByLink = UIImage(named: "sharebylink")!.image(color: .systemGray, size: 50)
  178. cacheImages.favorite = NCUtility.shared.loadImage(named: "star.fill", color: yellowFavorite)
  179. cacheImages.comment = UIImage(named: "comment")!.image(color: .systemGray, size: 50)
  180. cacheImages.livePhoto = NCUtility.shared.loadImage(named: "livephoto", color: .label)
  181. cacheImages.offlineFlag = UIImage(named: "offlineFlag")!
  182. cacheImages.local = UIImage(named: "local")!
  183. let folderWidth: CGFloat = UIScreen.main.bounds.width / 3
  184. cacheImages.folderEncrypted = UIImage(named: "folderEncrypted")!.image(color: brandElement, size: folderWidth)
  185. cacheImages.folderSharedWithMe = UIImage(named: "folder_shared_with_me")!.image(color: brandElement, size: folderWidth)
  186. cacheImages.folderPublic = UIImage(named: "folder_public")!.image(color: brandElement, size: folderWidth)
  187. cacheImages.folderGroup = UIImage(named: "folder_group")!.image(color: brandElement, size: folderWidth)
  188. cacheImages.folderExternal = UIImage(named: "folder_external")!.image(color: brandElement, size: folderWidth)
  189. cacheImages.folderAutomaticUpload = UIImage(named: "folderAutomaticUpload")!.image(color: brandElement, size: folderWidth)
  190. cacheImages.folder = UIImage(named: "folder")!.image(color: brandElement, size: folderWidth)
  191. cacheImages.checkedYes = NCUtility.shared.loadImage(named: "checkmark.circle.fill", color: .systemBlue)
  192. cacheImages.checkedNo = NCUtility.shared.loadImage(named: "circle", color: .systemGray)
  193. cacheImages.buttonMore = UIImage(named: "more")!.image(color: .systemGray, size: 50)
  194. cacheImages.buttonStop = UIImage(named: "stop")!.image(color: .systemGray, size: 50)
  195. cacheImages.buttonMoreLock = UIImage(named: "moreLock")!.image(color: .systemGray, size: 50)
  196. cacheImages.buttonRestore = UIImage(named: "restore")!.image(color: .systemGray, size: 50)
  197. cacheImages.buttonTrash = UIImage(named: "trash")!.image(color: .systemGray, size: 50)
  198. cacheImages.iconContacts = UIImage(named: "icon-contacts")!.image(color: brandElement, size: folderWidth)
  199. cacheImages.iconTalk = UIImage(named: "icon-talk")!.image(color: brandElement, size: folderWidth)
  200. cacheImages.iconCalendar = UIImage(named: "icon-calendar")!.image(color: brandElement, size: folderWidth)
  201. cacheImages.iconDeck = UIImage(named: "icon-deck")!.image(color: brandElement, size: folderWidth)
  202. cacheImages.iconMail = UIImage(named: "icon-mail")!.image(color: brandElement, size: folderWidth)
  203. cacheImages.iconConfirm = UIImage(named: "icon-confirm")!.image(color: brandElement, size: folderWidth)
  204. cacheImages.iconPages = UIImage(named: "icon-pages")!.image(color: brandElement, size: folderWidth)
  205. }
  206. func settingThemingColor(account: String) {
  207. let darker: CGFloat = 30 // %
  208. let lighter: CGFloat = 30 // %
  209. if NCBrandOptions.shared.use_themingColor {
  210. self.themingColor = NCGlobal.shared.capabilityThemingColor
  211. self.themingColorElement = NCGlobal.shared.capabilityThemingColorElement
  212. self.themingColorText = NCGlobal.shared.capabilityThemingColorText
  213. // COLOR
  214. if themingColor.first == "#" {
  215. if let color = UIColor(hex: themingColor) {
  216. brand = color
  217. } else {
  218. brand = customer
  219. }
  220. } else {
  221. brand = customer
  222. }
  223. // COLOR TEXT
  224. if themingColorText.first == "#" {
  225. if let color = UIColor(hex: themingColorText) {
  226. brandText = color
  227. } else {
  228. brandText = customerText
  229. }
  230. } else {
  231. brandText = customerText
  232. }
  233. // COLOR ELEMENT
  234. if themingColorElement.first == "#" {
  235. if let color = UIColor(hex: themingColorElement) {
  236. brandElement = color
  237. } else {
  238. brandElement = brand
  239. }
  240. } else {
  241. brandElement = brand
  242. }
  243. if brandElement.isTooLight() {
  244. if let color = brandElement.darker(by: darker) {
  245. brandElement = color
  246. }
  247. } else if brandElement.isTooDark() {
  248. if let color = brandElement.lighter(by: lighter) {
  249. brandElement = color
  250. }
  251. }
  252. } else {
  253. if self.customer.isTooLight() {
  254. if let color = customer.darker(by: darker) {
  255. brandElement = color
  256. }
  257. } else if customer.isTooDark() {
  258. if let color = customer.lighter(by: lighter) {
  259. brandElement = color
  260. }
  261. } else {
  262. brandElement = customer
  263. }
  264. brand = customer
  265. brandText = customerText
  266. }
  267. createImagesThemingColor()
  268. #if !EXTENSION
  269. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeTheming)
  270. #endif
  271. }
  272. private func stepCalc(steps: Int, color1: CGColor, color2: CGColor) -> [CGFloat] {
  273. var step = [CGFloat](repeating: 0, count: 3)
  274. step[0] = (color2.components![0] - color1.components![0]) / CGFloat(steps)
  275. step[1] = (color2.components![1] - color1.components![1]) / CGFloat(steps)
  276. step[2] = (color2.components![2] - color1.components![2]) / CGFloat(steps)
  277. return step
  278. }
  279. private func mixPalette(steps: Int, color1: CGColor, color2: CGColor) -> [CGColor] {
  280. var palette = [color1]
  281. let step = stepCalc(steps: steps, color1: color1, color2: color2)
  282. let c1Components = color1.components!
  283. for i in 1 ..< steps {
  284. let r = c1Components[0] + step[0] * CGFloat(i)
  285. let g = c1Components[1] + step[1] * CGFloat(i)
  286. let b = c1Components[2] + step[2] * CGFloat(i)
  287. palette.append(UIColor(red: r, green: g, blue: b, alpha: 1).cgColor)
  288. }
  289. return palette
  290. }
  291. /**
  292. Generate colors from the official nextcloud color.
  293. You can provide how many colors you want (multiplied by 3).
  294. if `step` = 6,
  295. 3 colors \* 6 will result in 18 generated colors
  296. */
  297. func generateColors(steps: Int = 6) -> [CGColor] {
  298. let red = UIColor(red: 182/255, green: 70/255, blue: 157/255, alpha: 1).cgColor
  299. let yellow = UIColor(red: 221/255, green: 203/255, blue: 85/255, alpha: 1).cgColor
  300. let blue = UIColor(red: 0/255, green: 130/255, blue: 201/255, alpha: 1).cgColor
  301. let palette1 = mixPalette(steps: steps, color1: red, color2: yellow)
  302. let palette2 = mixPalette(steps: steps, color1: yellow, color2: blue)
  303. let palette3 = mixPalette(steps: steps, color1: blue, color2: red)
  304. return palette1 + palette2 + palette3
  305. }
  306. }