NCBrand.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. var brand: String = "SX Space"
  35. var textCopyrightNextcloudiOS: String = "SX Space for iOS %@ © 2024"
  36. var textCopyrightNextcloudServer: String = "SX Space %@"
  37. var loginBaseUrl: String = "https://nc.sharix-app.org"
  38. @objc var pushNotificationServerProxy: String = "https://push-notifications.nextcloud.com"
  39. var linkLoginHost: String = "https://nextcloud.com/install"
  40. var linkloginPreferredProviders: String = "https://nextcloud.com/signup-ios"
  41. var webLoginAutenticationProtocol: String = "nc://" // example "abc://"
  42. var privacy: String = "https://wiki.sharix-app.org/doku.php/open/doc/policy_conf"
  43. var sourceCode: String = "https://github.com/nextcloud/ios"
  44. var mobileconfig: String = "/remote.php/dav/provisioning/apple-provisioning.mobileconfig"
  45. var appStoreUrl: String = "https://apps.apple.com/in/app/nextcloud/id1125420102"
  46. // Auto Upload default folder
  47. var folderDefaultAutoUpload: String = "Photos"
  48. // Capabilities Group
  49. var capabilitiesGroup: String = "group.com.sharix.keychain"
  50. var capabilitiesGroupApps: String = "group.com.sharix.apps"
  51. // BRAND ONLY
  52. @objc public var use_AppConfig: Bool = false // Don't touch me !!
  53. // Use server theming color
  54. @objc public var use_themingColor: Bool = true
  55. var disable_intro: Bool = false
  56. var disable_request_login_url: Bool = false
  57. var disable_multiaccount: Bool = false
  58. var disable_more_external_site: Bool = false
  59. var disable_openin_file: Bool = false // Don't touch me !!
  60. var disable_crash_service: Bool = false
  61. var disable_log: Bool = false
  62. var disable_mobileconfig: Bool = false
  63. var disable_show_more_nextcloud_apps_in_settings: Bool = false
  64. var doNotAskPasscodeAtStartup: Bool = false
  65. var disable_source_code_in_settings: Bool = false
  66. // (name: "Name 1", url: "https://cloud.nextcloud.com"),(name: "Name 2", url: "https://cloud.nextcloud.com")
  67. var enforce_servers: [(name: String, url: String)] = []
  68. // Internal option behaviour
  69. 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
  70. // Max download/upload concurrent
  71. let maxConcurrentOperationDownload: Int = 5
  72. let maxConcurrentOperationUpload: Int = 5
  73. // Number of failed attempts after reset app
  74. let resetAppPasscodeAttempts: Int = 10
  75. let passcodeSecondsFail: Int = 60
  76. // Info Paging
  77. enum NCInfoPagingTab: Int, CaseIterable {
  78. case activity, sharing
  79. }
  80. override init() {
  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_more_external_site] as? String {
  99. disable_more_external_site = (str as NSString).boolValue
  100. }
  101. if let str = configurationManaged[NCGlobal.shared.configuration_disable_openin_file] as? String {
  102. disable_openin_file = (str as NSString).boolValue
  103. }
  104. }
  105. }
  106. @objc func getUserAgent() -> String {
  107. return userAgent
  108. }
  109. }
  110. class NCBrandColor: NSObject {
  111. static let shared: NCBrandColor = {
  112. let instance = NCBrandColor()
  113. return instance
  114. }()
  115. /// This is rewrited from customet theme, default is Nextcloud color
  116. ///
  117. let customer: UIColor = UIColor(red: 48.1 / 255.0, green: 45.8 / 255.0, blue: 49.9 / 255.0, alpha: 1.0) // BLU NC : #0082c9 | new: 662d91
  118. var customerText: UIColor = .white
  119. // INTERNAL DEFINE COLORS
  120. private var themingColor = ThreadSafeDictionary<String, UIColor>()
  121. private var themingColorElement = ThreadSafeDictionary<String, UIColor>()
  122. private var themingColorText = ThreadSafeDictionary<String, UIColor>()
  123. var userColors: [CGColor] = []
  124. let nextcloud: UIColor = UIColor(red: 48.1 / 255.0, green: 45.8 / 255.0, blue: 49.9 / 255.0, alpha: 1.0)
  125. //let nextcloud: UIColor = UIColor(red: 0.0 / 255.0, green: 130.0 / 255.0, blue: 201.0 / 255.0, alpha: 1.0)
  126. let yellowFavorite: UIColor = UIColor(red: 248.0 / 255.0, green: 205.0 / 255.0, blue: 70.0 / 255.0, alpha: 1.0)
  127. let iconImageColor: UIColor = .label
  128. let iconImageColor2: UIColor = .secondaryLabel
  129. let iconImageMultiColors: [UIColor] = [.secondaryLabel, .label]
  130. let textColor: UIColor = .label
  131. let textColor2: UIColor = .secondaryLabel
  132. var systemMint: UIColor {
  133. get {
  134. return UIColor(red: 0.0 / 255.0, green: 199.0 / 255.0, blue: 190.0 / 255.0, alpha: 1.0)
  135. }
  136. }
  137. var documentIconColor: UIColor {
  138. get {
  139. return UIColor(hex: "#49abe9")!
  140. }
  141. }
  142. var spreadsheetIconColor: UIColor {
  143. get {
  144. return UIColor(hex: "#9abd4e")!
  145. }
  146. }
  147. var presentationIconColor: UIColor {
  148. get {
  149. return UIColor(hex: "#f0965f")!
  150. }
  151. }
  152. override init() { }
  153. /**
  154. Generate colors from the official nextcloud color.
  155. You can provide how many colors you want (multiplied by 3).
  156. if `step` = 6,
  157. 3 colors \* 6 will result in 18 generated colors
  158. */
  159. func createUserColors() {
  160. func generateColors(steps: Int = 6) -> [CGColor] {
  161. func stepCalc(steps: Int, color1: CGColor, color2: CGColor) -> [CGFloat] {
  162. var step = [CGFloat](repeating: 0, count: 3)
  163. step[0] = (color2.components![0] - color1.components![0]) / CGFloat(steps)
  164. step[1] = (color2.components![1] - color1.components![1]) / CGFloat(steps)
  165. step[2] = (color2.components![2] - color1.components![2]) / CGFloat(steps)
  166. return step
  167. }
  168. func mixPalette(steps: Int, color1: CGColor, color2: CGColor) -> [CGColor] {
  169. var palette = [color1]
  170. let step = stepCalc(steps: steps, color1: color1, color2: color2)
  171. let c1Components = color1.components!
  172. for i in 1 ..< steps {
  173. let r = c1Components[0] + step[0] * CGFloat(i)
  174. let g = c1Components[1] + step[1] * CGFloat(i)
  175. let b = c1Components[2] + step[2] * CGFloat(i)
  176. palette.append(UIColor(red: r, green: g, blue: b, alpha: 1).cgColor)
  177. }
  178. return palette
  179. }
  180. let red = UIColor(red: 182 / 255, green: 70 / 255, blue: 157 / 255, alpha: 1).cgColor
  181. let yellow = UIColor(red: 221 / 255, green: 203 / 255, blue: 85 / 255, alpha: 1).cgColor
  182. let blue = UIColor(red: 0 / 255, green: 130 / 255, blue: 201 / 255, alpha: 1).cgColor
  183. let palette1 = mixPalette(steps: steps, color1: red, color2: yellow)
  184. let palette2 = mixPalette(steps: steps, color1: yellow, color2: blue)
  185. let palette3 = mixPalette(steps: steps, color1: blue, color2: red)
  186. return palette1 + palette2 + palette3
  187. }
  188. userColors = generateColors()
  189. }
  190. @discardableResult
  191. func settingThemingColor(account: String) -> Bool {
  192. let darker: CGFloat = 30 // %
  193. let lighter: CGFloat = 30 // %
  194. var colorThemingColor: UIColor?
  195. var colorThemingColorElement: UIColor?
  196. var colorThemingColorText: UIColor?
  197. if NCBrandOptions.shared.use_themingColor {
  198. let themingColor = NCCapabilities.shared.getCapabilities(account: account).capabilityThemingColor
  199. let themingColorElement = NCCapabilities.shared.getCapabilities(account: account).capabilityThemingColorElement
  200. let themingColorText = NCCapabilities.shared.getCapabilities(account: account).capabilityThemingColorText
  201. // THEMING COLOR
  202. if themingColor.first == "#" {
  203. if let color = UIColor(hex: themingColor) {
  204. colorThemingColor = color
  205. } else {
  206. colorThemingColor = customer
  207. }
  208. } else {
  209. colorThemingColor = customer
  210. }
  211. // THEMING COLOR ELEMENT (control isTooLight / isTooDark)
  212. if themingColorElement.first == "#" {
  213. if let color = UIColor(hex: themingColorElement) {
  214. if color.isTooLight() {
  215. if let color = color.darker(by: darker) {
  216. colorThemingColorElement = color
  217. }
  218. } else if color.isTooDark() {
  219. if let color = color.lighter(by: lighter) {
  220. colorThemingColorElement = color
  221. }
  222. } else {
  223. colorThemingColorElement = color
  224. }
  225. } else {
  226. colorThemingColorElement = customer
  227. }
  228. } else {
  229. colorThemingColorElement = customer
  230. }
  231. // THEMING COLOR TEXT
  232. if themingColorText.first == "#" {
  233. if let color = UIColor(hex: themingColorText) {
  234. colorThemingColorText = color
  235. } else {
  236. colorThemingColorText = .white
  237. }
  238. } else {
  239. colorThemingColorText = .white
  240. }
  241. } else {
  242. // THEMING COLOR
  243. colorThemingColor = customer
  244. // THEMING COLOR ELEMENT (control isTooLight / isTooDark)
  245. if self.customer.isTooLight() {
  246. if let color = customer.darker(by: darker) {
  247. colorThemingColorElement = color
  248. }
  249. } else if customer.isTooDark() {
  250. if let color = customer.lighter(by: lighter) {
  251. colorThemingColorElement = color
  252. }
  253. } else {
  254. colorThemingColorElement = customer
  255. }
  256. // THEMING COLOR TEXT
  257. colorThemingColorText = customerText
  258. }
  259. if self.themingColor[account] != colorThemingColor || self.themingColorElement[account] != colorThemingColorElement || self.themingColorText[account] != colorThemingColorText {
  260. self.themingColor[account] = colorThemingColor
  261. self.themingColorElement[account] = colorThemingColorElement
  262. self.themingColorText[account] = colorThemingColorText
  263. return true
  264. }
  265. return false
  266. }
  267. public func getTheming(account: String?) -> UIColor {
  268. if let account, let color = self.themingColor[account] {
  269. return color
  270. }
  271. return customer
  272. }
  273. public func getElement(account: String?) -> UIColor {
  274. if let account, let color = self.themingColorElement[account] {
  275. return color
  276. }
  277. return customer
  278. }
  279. public func getText(account: String?) -> UIColor {
  280. if let account, let color = self.themingColorText[account] {
  281. return color
  282. }
  283. return .white
  284. }
  285. }