NCBrand.swift 16 KB

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