NCBackgroundImageColor.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // NCBackgroundImageColor.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/05/21.
  6. // Copyright © 2021 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. import ChromaColorPicker
  25. class NCBackgroundImageColor: UIViewController {
  26. @IBOutlet weak var titleLabel: UILabel!
  27. @IBOutlet weak var chromaColorPickerView: UIView!
  28. @IBOutlet weak var whiteButton: UIButton!
  29. @IBOutlet weak var orangeButton: UIButton!
  30. @IBOutlet weak var redButton: UIButton!
  31. @IBOutlet weak var greenButton: UIButton!
  32. @IBOutlet weak var blackButton: UIButton!
  33. @IBOutlet weak var darkmodeLabel: UILabel!
  34. @IBOutlet weak var darkmodeSwitch: UISwitch!
  35. @IBOutlet weak var useForAllLabel: UILabel!
  36. @IBOutlet weak var useForAllSwitch: UISwitch!
  37. @IBOutlet weak var defaultButton: UIButton!
  38. @IBOutlet weak var cancelButton: UIButton!
  39. @IBOutlet weak var okButton: UIButton!
  40. private let colorPicker = ChromaColorPicker()
  41. private let brightnessSlider = ChromaBrightnessSlider()
  42. private var colorHandle: ChromaColorHandle?
  43. private let defaultColorPickerSize = CGSize(width: 200, height: 200)
  44. private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
  45. private var darkColor = ""
  46. private var lightColor = ""
  47. public var collectionViewCommon: NCCollectionViewCommon?
  48. let width: CGFloat = 300
  49. let height: CGFloat = 500
  50. // MARK: - View Life Cycle
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. setupColorPicker()
  54. setupBrightnessSlider()
  55. setupColorPickerHandles()
  56. titleLabel.text = NSLocalizedString("_background_", comment: "")
  57. darkmodeLabel.text = NSLocalizedString("_dark_mode_", comment: "")
  58. useForAllLabel.text = NSLocalizedString("_as_default_color_", comment: "")
  59. defaultButton.setTitle(NSLocalizedString("_default_color_", comment: ""), for: .normal)
  60. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  61. okButton.setTitle(NSLocalizedString("_ok_", comment: ""), for: .normal)
  62. whiteButton.backgroundColor = .white
  63. whiteButton.layer.cornerRadius = 5
  64. whiteButton.layer.borderWidth = 0.5
  65. whiteButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  66. whiteButton.layer.masksToBounds = true
  67. orangeButton.backgroundColor = .orange
  68. orangeButton.layer.cornerRadius = 5
  69. orangeButton.layer.borderWidth = 0.5
  70. orangeButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  71. orangeButton.layer.masksToBounds = true
  72. redButton.backgroundColor = .red
  73. redButton.layer.cornerRadius = 5
  74. redButton.layer.borderWidth = 0.5
  75. redButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  76. redButton.layer.masksToBounds = true
  77. greenButton.backgroundColor = .green
  78. greenButton.layer.cornerRadius = 5
  79. greenButton.layer.borderWidth = 0.5
  80. greenButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  81. greenButton.layer.masksToBounds = true
  82. blackButton.backgroundColor = .black
  83. blackButton.layer.cornerRadius = 5
  84. blackButton.layer.borderWidth = 0.5
  85. blackButton.layer.borderColor = NCBrandColor.shared.label.cgColor
  86. blackButton.layer.masksToBounds = true
  87. defaultButton.layer.cornerRadius = 15
  88. defaultButton.layer.borderWidth = 0.5
  89. defaultButton.layer.borderColor = UIColor.gray.cgColor
  90. defaultButton.layer.masksToBounds = true
  91. }
  92. override func viewWillAppear(_ animated: Bool) {
  93. super.viewWillAppear(animated)
  94. if traitCollection.userInterfaceStyle == .dark {
  95. darkmodeSwitch.isOn = true
  96. } else {
  97. darkmodeSwitch.isOn = false
  98. }
  99. useForAllSwitch.isOn = false
  100. // Color for this view
  101. if let collectionViewCommon = collectionViewCommon {
  102. let layoutForView = NCUtility.shared.getLayoutForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl)
  103. darkColor = layoutForView.darkColorBackground
  104. lightColor = layoutForView.lightColorBackground
  105. }
  106. // Color for all folders
  107. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  108. if darkColor == "" {
  109. darkColor = activeAccount.darkColorFiles
  110. }
  111. if lightColor == "" {
  112. lightColor = activeAccount.lightColorFiles
  113. }
  114. }
  115. // set color
  116. if darkmodeSwitch.isOn {
  117. if let color = UIColor.init(hex: darkColor) {
  118. changeColor(color)
  119. } else {
  120. changeColor(.black)
  121. }
  122. } else {
  123. if let color = UIColor.init(hex: lightColor) {
  124. changeColor(color)
  125. } else {
  126. changeColor(.white)
  127. }
  128. }
  129. }
  130. override func viewDidAppear(_ animated: Bool) {
  131. super.viewDidAppear(animated)
  132. }
  133. // MARK: - Action
  134. @IBAction func whiteButtonAction(_ sender: UIButton) {
  135. changeColor(.white)
  136. }
  137. @IBAction func orangeButtonAction(_ sender: UIButton) {
  138. changeColor(.orange)
  139. }
  140. @IBAction func redButtonAction(_ sender: UIButton) {
  141. changeColor(.red)
  142. }
  143. @IBAction func greenButtonAction(_ sender: UIButton) {
  144. changeColor(.green)
  145. }
  146. @IBAction func blackButtonAction(_ sender: UIButton) {
  147. changeColor(.black)
  148. }
  149. @IBAction func darkmodeAction(_ sender: UISwitch) {
  150. if sender.isOn {
  151. if darkColor == "" {
  152. changeColor(.black)
  153. } else {
  154. if let color = UIColor.init(hex: darkColor) {
  155. changeColor(color)
  156. }
  157. }
  158. } else {
  159. if lightColor == "" {
  160. changeColor(.white)
  161. } else {
  162. if let color = UIColor.init(hex: lightColor) {
  163. changeColor(color)
  164. }
  165. }
  166. }
  167. }
  168. @IBAction func defaultAction(_ sender: Any) {
  169. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  170. if darkmodeSwitch.isOn {
  171. if useForAllSwitch.isOn {
  172. darkColor = ""
  173. changeColor(.black)
  174. } else {
  175. if let color = UIColor.init(hex: activeAccount.darkColorFiles) {
  176. darkColor = activeAccount.darkColorFiles
  177. changeColor(color)
  178. } else {
  179. darkColor = ""
  180. changeColor(.black)
  181. }
  182. }
  183. } else {
  184. if useForAllSwitch.isOn {
  185. lightColor = "#FFFFFF"
  186. changeColor(.white)
  187. } else {
  188. if let color = UIColor.init(hex: activeAccount.lightColorFiles) {
  189. lightColor = activeAccount.lightColorFiles
  190. changeColor(color)
  191. } else {
  192. lightColor = "#FFFFFF"
  193. changeColor(.white)
  194. }
  195. }
  196. }
  197. }
  198. }
  199. @IBAction func cancelAction(_ sender: Any) {
  200. collectionViewCommon?.setLayout()
  201. dismiss(animated: true)
  202. }
  203. @IBAction func okAction(_ sender: Any) {
  204. var lightColor = self.lightColor
  205. var darkColor = self.darkColor
  206. if lightColor == "#FFFFFF" { lightColor = "" }
  207. if darkColor == "#000000" { darkColor = "" }
  208. if let collectionViewCommon = collectionViewCommon {
  209. if useForAllSwitch.isOn {
  210. NCManageDatabase.shared.setAccountColorFiles(lightColorFiles: lightColor, darkColorFiles: darkColor)
  211. NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, lightColorBackground: "", darkColorBackground: "")
  212. } else {
  213. NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, lightColorBackground: lightColor, darkColorBackground: darkColor)
  214. }
  215. }
  216. collectionViewCommon?.setLayout()
  217. dismiss(animated: true)
  218. }
  219. // MARK: - ChromaColorPicker
  220. private func setupColorPicker() {
  221. colorPicker.delegate = self
  222. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  223. view.addSubview(colorPicker)
  224. NSLayoutConstraint.activate([
  225. colorPicker.leadingAnchor.constraint(equalTo: chromaColorPickerView.leadingAnchor, constant: 20),
  226. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  227. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  228. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  229. ])
  230. }
  231. private func setupBrightnessSlider() {
  232. brightnessSlider.connect(to: colorPicker)
  233. // Style
  234. brightnessSlider.trackColor = UIColor.blue
  235. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  236. // Layout
  237. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  238. view.addSubview(brightnessSlider)
  239. NSLayoutConstraint.activate([
  240. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  241. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  242. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  243. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  244. ])
  245. }
  246. private func setupColorPickerHandles() {
  247. colorHandle = colorPicker.addHandle(at: collectionViewCommon?.collectionView.backgroundColor)
  248. }
  249. private func changeColor(_ color: UIColor) {
  250. colorHandle?.color = color
  251. colorPicker.setNeedsLayout()
  252. brightnessSlider.trackColor = color
  253. if darkmodeSwitch.isOn {
  254. darkColor = color.hexString
  255. } else {
  256. lightColor = color.hexString
  257. }
  258. collectionViewCommon?.collectionView.backgroundColor = color
  259. }
  260. }
  261. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  262. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  263. if darkmodeSwitch.isOn {
  264. darkColor = color.hexString
  265. } else {
  266. lightColor = color.hexString
  267. }
  268. collectionViewCommon?.collectionView.backgroundColor = color
  269. }
  270. }