NCBrand.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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_bundleId: String = "it.twsweb.Nextcloud"
  31. @objc public let configuration_serverUrl: String = "serverUrl"
  32. @objc public let configuration_username: String = "username"
  33. @objc public let configuration_password: String = "password"
  34. }
  35. // MARK: - Options
  36. @objc class NCBrandOptions: NSObject {
  37. @objc static let shared: NCBrandOptions = {
  38. let instance = NCBrandOptions()
  39. return instance
  40. }()
  41. @objc public var brand: String = "Nextcloud"
  42. // @objc public var mailMe: String = "ios@nextcloud.com" // Deprecated
  43. @objc public var textCopyrightNextcloudiOS: String = "Nextcloud Liquid for iOS %@ © 2022"
  44. @objc public var textCopyrightNextcloudServer: String = "Nextcloud Server %@"
  45. @objc public var loginBaseUrl: String = "https://cloud.nextcloud.com"
  46. @objc public var pushNotificationServerProxy: String = "https://push-notifications.nextcloud.com"
  47. @objc public var linkLoginHost: String = "https://nextcloud.com/install"
  48. @objc public var linkloginPreferredProviders: String = "https://nextcloud.com/signup-ios"
  49. @objc public var webLoginAutenticationProtocol: String = "nc://" // example "abc://"
  50. @objc public var privacy: String = "https://nextcloud.com/privacy"
  51. @objc public var sourceCode: String = "https://github.com/nextcloud/ios"
  52. // Personalized
  53. @objc public var webCloseViewProtocolPersonalized: String = "" // example "abc://change/plan" Don't touch me !!
  54. @objc public var folderBrandAutoUpload: String = "" // example "_auto_upload_folder_" Don't touch me !!
  55. // Auto Upload default folder
  56. @objc public var folderDefaultAutoUpload: String = "Photos"
  57. // Capabilities Group
  58. @objc public var capabilitiesGroups: String = "group.it.twsweb.Crypto-Cloud"
  59. // User Agent
  60. @objc public var userAgent: String = "Nextcloud-iOS" // Don't touch me !!
  61. // Options
  62. @objc public var use_login_web_personalized: Bool = false // Don't touch me !!
  63. @objc public var use_default_auto_upload: Bool = false
  64. @objc public var use_themingColor: Bool = true
  65. //@objc public var use_themingBackground: Bool = true // Deprecated
  66. @objc public var use_themingLogo: Bool = false
  67. @objc public var use_storeLocalAutoUploadAll: Bool = false
  68. @objc public var use_configuration: Bool = false // Don't touch me !!
  69. @objc public var use_loginflowv2: Bool = false // Don't touch me !!
  70. @objc public var disable_intro: Bool = false
  71. @objc public var disable_request_login_url: Bool = false
  72. @objc public var disable_multiaccount: Bool = false
  73. @objc public var disable_manage_account: Bool = false
  74. @objc public var disable_more_external_site: Bool = false
  75. @objc public var disable_openin_file: Bool = false // Don't touch me !!
  76. @objc public var disable_crash_service: Bool = false
  77. @objc public var disable_request_account: Bool = false
  78. @objc public var disable_log: Bool = false
  79. @objc public var disable_background_color: Bool = true
  80. @objc public var disable_background_image: Bool = true
  81. override init() {
  82. if folderBrandAutoUpload != "" {
  83. folderDefaultAutoUpload = folderBrandAutoUpload
  84. }
  85. }
  86. }
  87. // MARK: - Color
  88. class NCBrandColor: NSObject {
  89. @objc static let shared: NCBrandColor = {
  90. let instance = NCBrandColor()
  91. instance.createImagesThemingColor()
  92. instance.createUserColors()
  93. return instance
  94. }()
  95. struct cacheImages {
  96. static var file = UIImage()
  97. static var shared = UIImage()
  98. static var canShare = UIImage()
  99. static var shareByLink = UIImage()
  100. static var favorite = UIImage()
  101. static var comment = UIImage()
  102. static var livePhoto = UIImage()
  103. static var offlineFlag = UIImage()
  104. static var local = UIImage()
  105. static var folderEncrypted = UIImage()
  106. static var folderSharedWithMe = UIImage()
  107. static var folderPublic = UIImage()
  108. static var folderGroup = UIImage()
  109. static var folderExternal = UIImage()
  110. static var folderAutomaticUpload = UIImage()
  111. static var folder = UIImage()
  112. static var checkedYes = UIImage()
  113. static var checkedNo = UIImage()
  114. static var buttonMore = UIImage()
  115. static var buttonStop = UIImage()
  116. static var buttonMoreLock = UIImage()
  117. static var buttonRestore = UIImage()
  118. static var iconContacts = UIImage()
  119. static var iconTalk = UIImage()
  120. static var iconCalendar = UIImage()
  121. static var iconDeck = UIImage()
  122. static var iconMail = UIImage()
  123. static var iconConfirm = UIImage()
  124. static var iconPages = UIImage()
  125. }
  126. // Color
  127. @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
  128. @objc public var customerText: UIColor = .white
  129. @objc public var brand: UIColor // don't touch me
  130. @objc public var brandElement: UIColor // don't touch me
  131. @objc public var brandText: UIColor // don't touch me
  132. @objc public let nextcloud: UIColor = UIColor(red: 0.0/255.0, green: 130.0/255.0, blue: 201.0/255.0, alpha: 1.0)
  133. @objc public let gray: UIColor = UIColor(red: 104.0/255.0, green: 104.0/255.0, blue: 104.0/255.0, alpha: 1.0)
  134. @objc public let lightGray: UIColor = UIColor(red: 229.0/255.0, green: 229.0/229.0, blue: 104.0/255.0, alpha: 1.0)
  135. @objc public let yellowFavorite: UIColor = UIColor(red: 248.0/255.0, green: 205.0/255.0, blue: 70.0/255.0, alpha: 1.0)
  136. public var userColors: [CGColor] = []
  137. @objc public var systemBackground: UIColor {
  138. get {
  139. if #available(iOS 13, *) {
  140. return .systemBackground
  141. } else {
  142. return .white
  143. }
  144. }
  145. }
  146. @objc public var secondarySystemBackground: UIColor {
  147. get {
  148. if #available(iOS 13, *) {
  149. return .secondarySystemBackground
  150. } else {
  151. return UIColor(red: 0.95, green: 0.95, blue: 0.97, alpha: 1.0)
  152. }
  153. }
  154. }
  155. @objc public var tertiarySystemBackground: UIColor {
  156. get {
  157. if #available(iOS 13, *) {
  158. return .tertiarySystemBackground
  159. } else {
  160. return UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
  161. }
  162. }
  163. }
  164. @objc public var systemGroupedBackground: UIColor {
  165. get {
  166. if #available(iOS 13, *) {
  167. return .systemGroupedBackground
  168. } else {
  169. return UIColor(red: 0.95, green: 0.95, blue: 0.97, alpha: 1.0)
  170. }
  171. }
  172. }
  173. @objc public var secondarySystemGroupedBackground: UIColor {
  174. get {
  175. if #available(iOS 13, *) {
  176. return .secondarySystemGroupedBackground
  177. } else {
  178. return .white
  179. }
  180. }
  181. }
  182. @objc public var label: UIColor {
  183. get {
  184. if #available(iOS 13, *) {
  185. return .label
  186. } else {
  187. return .black
  188. }
  189. }
  190. }
  191. @objc public var secondaryLabel: UIColor {
  192. get {
  193. if #available(iOS 13, *) {
  194. return .secondaryLabel
  195. } else {
  196. return UIColor(red: 0.24, green: 0.24, blue: 0.26, alpha: 0.6)
  197. }
  198. }
  199. }
  200. @objc public var separator: UIColor {
  201. get {
  202. if #available(iOS 13, *) {
  203. return .separator
  204. } else {
  205. return UIColor(red: 0.89, green: 0.89, blue: 0.89, alpha: 1.0)
  206. }
  207. }
  208. }
  209. @objc public var opaqueSeparator: UIColor {
  210. get {
  211. if #available(iOS 13, *) {
  212. return .opaqueSeparator
  213. } else {
  214. return UIColor(red: 0.78, green: 0.78, blue: 0.78, alpha: 1.0)
  215. }
  216. }
  217. }
  218. @objc public var systemGray: UIColor {
  219. get {
  220. if #available(iOS 13, *) {
  221. return .systemGray
  222. } else {
  223. return UIColor(red: 0.56, green: 0.56, blue: 0.58, alpha: 1.0)
  224. }
  225. }
  226. }
  227. @objc public var systemGray1: UIColor {
  228. get {
  229. if #available(iOS 13, *) {
  230. return UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha: 1.0)
  231. } else {
  232. return UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha: 1.0)
  233. }
  234. }
  235. }
  236. @objc public var systemGray2: UIColor {
  237. get {
  238. if #available(iOS 13, *) {
  239. return .systemGray2
  240. } else {
  241. return UIColor(red: 0.68, green: 0.68, blue: 0.7, alpha: 1.0)
  242. }
  243. }
  244. }
  245. @objc public var systemGray3: UIColor {
  246. get {
  247. if #available(iOS 13, *) {
  248. return .systemGray3
  249. } else {
  250. return UIColor(red: 0.78, green: 0.78, blue: 0.8, alpha: 1.0)
  251. }
  252. }
  253. }
  254. @objc public var systemGray4: UIColor {
  255. get {
  256. if #available(iOS 13, *) {
  257. return .systemGray4
  258. } else {
  259. return UIColor(red: 0.82, green: 0.82, blue: 0.84, alpha: 1.0)
  260. }
  261. }
  262. }
  263. @objc public var systemGray5: UIColor {
  264. get {
  265. if #available(iOS 13, *) {
  266. return .systemGray5
  267. } else {
  268. return UIColor(red: 0.9, green: 0.9, blue: 0.92, alpha: 1.0)
  269. }
  270. }
  271. }
  272. @objc public var systemGray6: UIColor {
  273. get {
  274. if #available(iOS 13, *) {
  275. return .systemGray6
  276. } else {
  277. return UIColor(red: 0.95, green: 0.95, blue: 0.97, alpha: 1.0)
  278. }
  279. }
  280. }
  281. @objc public var systemFill: UIColor {
  282. get {
  283. if #available(iOS 13, *) {
  284. return .systemFill
  285. } else {
  286. return UIColor(red: 120/255, green: 120/255, blue: 120/255, alpha: 1.0)
  287. }
  288. }
  289. }
  290. override init() {
  291. self.brand = self.customer
  292. self.brandElement = self.customer
  293. self.brandText = self.customerText
  294. }
  295. private func createUserColors() {
  296. self.userColors = generateColors()
  297. }
  298. public func createImagesThemingColor() {
  299. let gray: UIColor = UIColor(red: 162.0/255.0, green: 162.0/255.0, blue: 162.0/255.0, alpha: 0.5)
  300. cacheImages.file = UIImage(named: "file")!
  301. cacheImages.shared = UIImage(named: "share")!.image(color: gray, size: 50)
  302. cacheImages.canShare = UIImage(named: "share")!.image(color: gray, size: 50)
  303. cacheImages.shareByLink = UIImage(named: "sharebylink")!.image(color: gray, size: 50)
  304. cacheImages.favorite = NCUtility.shared.loadImage(named: "star.fill", color: yellowFavorite)
  305. cacheImages.comment = UIImage(named: "comment")!.image(color: gray, size: 50)
  306. cacheImages.livePhoto = NCUtility.shared.loadImage(named: "livephoto", color: label)
  307. cacheImages.offlineFlag = UIImage(named: "offlineFlag")!
  308. cacheImages.local = UIImage(named: "local")!
  309. let folderWidth: CGFloat = UIScreen.main.bounds.width / 3
  310. cacheImages.folderEncrypted = UIImage(named: "folderEncrypted")!.image(color: brandElement, size: folderWidth)
  311. cacheImages.folderSharedWithMe = UIImage(named: "folder_shared_with_me")!.image(color: brandElement, size: folderWidth)
  312. cacheImages.folderPublic = UIImage(named: "folder_public")!.image(color: brandElement, size: folderWidth)
  313. cacheImages.folderGroup = UIImage(named: "folder_group")!.image(color: brandElement, size: folderWidth)
  314. cacheImages.folderExternal = UIImage(named: "folder_external")!.image(color: brandElement, size: folderWidth)
  315. cacheImages.folderAutomaticUpload = UIImage(named: "folderAutomaticUpload")!.image(color: brandElement, size: folderWidth)
  316. cacheImages.folder = UIImage(named: "folder")!.image(color: brandElement, size: folderWidth)
  317. cacheImages.checkedYes = NCUtility.shared.loadImage(named: "checkmark.circle.fill", color: .systemBlue)
  318. cacheImages.checkedNo = NCUtility.shared.loadImage(named: "circle", color: gray)
  319. cacheImages.buttonMore = UIImage(named: "more")!.image(color: gray, size: 50)
  320. cacheImages.buttonStop = UIImage(named: "stop")!.image(color: gray, size: 50)
  321. cacheImages.buttonMoreLock = UIImage(named: "moreLock")!.image(color: gray, size: 50)
  322. cacheImages.buttonRestore = UIImage(named: "restore")!.image(color: gray, size: 50)
  323. cacheImages.iconContacts = UIImage(named: "icon-contacts")!.image(color: brandElement, size: folderWidth)
  324. cacheImages.iconTalk = UIImage(named: "icon-talk")!.image(color: brandElement, size: folderWidth)
  325. cacheImages.iconCalendar = UIImage(named: "icon-calendar")!.image(color: brandElement, size: folderWidth)
  326. cacheImages.iconDeck = UIImage(named: "icon-deck")!.image(color: brandElement, size: folderWidth)
  327. cacheImages.iconMail = UIImage(named: "icon-mail")!.image(color: brandElement, size: folderWidth)
  328. cacheImages.iconConfirm = UIImage(named: "icon-confirm")!.image(color: brandElement, size: folderWidth)
  329. cacheImages.iconPages = UIImage(named: "icon-pages")!.image(color: brandElement, size: folderWidth)
  330. }
  331. #if !EXTENSION
  332. public func settingThemingColor(account: String) {
  333. let darker: CGFloat = 30 // %
  334. let lighter: CGFloat = 30 // %
  335. if NCBrandOptions.shared.use_themingColor {
  336. let themingColor = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColor)
  337. let themingColorElement = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColorElement)
  338. let themingColorText = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesThemingColorText)
  339. settingBrandColor(themingColor, themingColorElement: themingColorElement, themingColorText: themingColorText)
  340. if NCBrandColor.shared.brandElement.isTooLight() {
  341. if let color = NCBrandColor.shared.brandElement.darker(by: darker) {
  342. NCBrandColor.shared.brandElement = color
  343. }
  344. } else if NCBrandColor.shared.brandElement.isTooDark() {
  345. if let color = NCBrandColor.shared.brandElement.lighter(by: lighter) {
  346. NCBrandColor.shared.brandElement = color
  347. }
  348. }
  349. } else {
  350. if NCBrandColor.shared.customer.isTooLight() {
  351. if let color = NCBrandColor.shared.customer.darker(by: darker) {
  352. NCBrandColor.shared.brandElement = color
  353. }
  354. } else if NCBrandColor.shared.customer.isTooDark() {
  355. if let color = NCBrandColor.shared.customer.lighter(by: lighter) {
  356. NCBrandColor.shared.brandElement = color
  357. }
  358. } else {
  359. NCBrandColor.shared.brandElement = NCBrandColor.shared.customer
  360. }
  361. NCBrandColor.shared.brand = NCBrandColor.shared.customer
  362. NCBrandColor.shared.brandText = NCBrandColor.shared.customerText
  363. }
  364. DispatchQueue.main.async {
  365. self.createImagesThemingColor()
  366. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeTheming)
  367. }
  368. }
  369. #endif
  370. @objc func settingBrandColor(_ themingColor: String?, themingColorElement: String?, themingColorText: String?) {
  371. // COLOR
  372. if themingColor?.first == "#" {
  373. if let color = UIColor(hex: themingColor!) {
  374. NCBrandColor.shared.brand = color
  375. } else {
  376. NCBrandColor.shared.brand = NCBrandColor.shared.customer
  377. }
  378. } else {
  379. NCBrandColor.shared.brand = NCBrandColor.shared.customer
  380. }
  381. // COLOR TEXT
  382. if themingColorText?.first == "#" {
  383. if let color = UIColor(hex: themingColorText!) {
  384. NCBrandColor.shared.brandText = color
  385. } else {
  386. NCBrandColor.shared.brandText = NCBrandColor.shared.customerText
  387. }
  388. } else {
  389. NCBrandColor.shared.brandText = NCBrandColor.shared.customerText
  390. }
  391. // COLOR ELEMENT
  392. if themingColorElement?.first == "#" {
  393. if let color = UIColor(hex: themingColorElement!) {
  394. NCBrandColor.shared.brandElement = color
  395. } else {
  396. NCBrandColor.shared.brandElement = NCBrandColor.shared.brand
  397. }
  398. } else {
  399. NCBrandColor.shared.brandElement = NCBrandColor.shared.brand
  400. }
  401. }
  402. private func stepCalc(steps: Int, color1: CGColor, color2: CGColor) -> [CGFloat] {
  403. var step = [CGFloat](repeating: 0, count: 3)
  404. step[0] = (color2.components![0] - color1.components![0]) / CGFloat(steps)
  405. step[1] = (color2.components![1] - color1.components![1]) / CGFloat(steps)
  406. step[2] = (color2.components![2] - color1.components![2]) / CGFloat(steps)
  407. return step
  408. }
  409. private func mixPalette(steps: Int, color1: CGColor, color2: CGColor) -> [CGColor] {
  410. var palette = [color1]
  411. let step = stepCalc(steps: steps, color1: color1, color2: color2)
  412. let c1Components = color1.components!
  413. for i in 1 ..< steps {
  414. let r = c1Components[0] + step[0] * CGFloat(i)
  415. let g = c1Components[1] + step[1] * CGFloat(i)
  416. let b = c1Components[2] + step[2] * CGFloat(i)
  417. palette.append(UIColor(red: r, green: g, blue: b, alpha: 1).cgColor)
  418. }
  419. return palette
  420. }
  421. /**
  422. Generate colors from the official nextcloud color.
  423. You can provide how many colors you want (multiplied by 3).
  424. if `step` = 6,
  425. 3 colors \* 6 will result in 18 generated colors
  426. */
  427. func generateColors(steps: Int = 6) -> [CGColor] {
  428. let red = UIColor(red: 182/255, green: 70/255, blue: 157/255, alpha: 1).cgColor
  429. let yellow = UIColor(red: 221/255, green: 203/255, blue: 85/255, alpha: 1).cgColor
  430. let blue = UIColor(red: 0/255, green: 130/255, blue: 201/255, alpha: 1).cgColor
  431. let palette1 = mixPalette(steps: steps, color1: red, color2: yellow)
  432. let palette2 = mixPalette(steps: steps, color1: yellow, color2: blue)
  433. let palette3 = mixPalette(steps: steps, color1: blue, color2: red)
  434. return palette1 + palette2 + palette3
  435. }
  436. }