NCBackgroundImageColor.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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("_default_color_all_folders_", 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. darkColor = ""
  170. lightColor = ""
  171. if darkmodeSwitch.isOn {
  172. changeColor(.black)
  173. } else {
  174. changeColor(.white)
  175. }
  176. }
  177. @IBAction func cancelAction(_ sender: Any) {
  178. collectionViewCommon?.setLayout()
  179. dismiss(animated: true)
  180. }
  181. @IBAction func okAction(_ sender: Any) {
  182. var lightColor = self.lightColor
  183. var darkColor = self.darkColor
  184. if lightColor == "#FFFFFF" { lightColor = "" }
  185. if darkColor == "#000000" { darkColor = "" }
  186. if let collectionViewCommon = collectionViewCommon {
  187. if useForAllSwitch.isOn {
  188. NCManageDatabase.shared.setAccountColorFiles(lightColorFiles: lightColor, darkColorFiles: darkColor)
  189. NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, lightColorBackground: "", darkColorBackground: "")
  190. } else {
  191. NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, lightColorBackground: lightColor, darkColorBackground: darkColor)
  192. }
  193. }
  194. collectionViewCommon?.setLayout()
  195. dismiss(animated: true)
  196. }
  197. // MARK: - ChromaColorPicker
  198. private func setupColorPicker() {
  199. colorPicker.delegate = self
  200. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  201. view.addSubview(colorPicker)
  202. NSLayoutConstraint.activate([
  203. colorPicker.leadingAnchor.constraint(equalTo: chromaColorPickerView.leadingAnchor, constant: 20),
  204. //colorPicker.centerXAnchor.constraint(equalTo: chromaColorPickerView.centerXAnchor),
  205. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  206. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  207. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  208. ])
  209. }
  210. private func setupBrightnessSlider() {
  211. brightnessSlider.connect(to: colorPicker)
  212. // Style
  213. brightnessSlider.trackColor = UIColor.blue
  214. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  215. // Layout
  216. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  217. view.addSubview(brightnessSlider)
  218. NSLayoutConstraint.activate([
  219. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  220. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  221. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  222. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  223. ])
  224. }
  225. private func setupColorPickerHandles() {
  226. colorHandle = colorPicker.addHandle(at: collectionViewCommon?.collectionView.backgroundColor)
  227. }
  228. private func changeColor(_ color: UIColor) {
  229. colorHandle?.color = color
  230. colorPicker.setNeedsLayout()
  231. brightnessSlider.trackColor = color
  232. if darkmodeSwitch.isOn {
  233. darkColor = color.hexString
  234. } else {
  235. lightColor = color.hexString
  236. }
  237. collectionViewCommon?.collectionView.backgroundColor = color
  238. }
  239. }
  240. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  241. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  242. if darkmodeSwitch.isOn {
  243. darkColor = color.hexString
  244. } else {
  245. lightColor = color.hexString
  246. }
  247. collectionViewCommon?.collectionView.backgroundColor = color
  248. }
  249. }