NCBackgroundImageColor.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 darkmodeLabel: UILabel!
  29. @IBOutlet weak var darkmodeSwitch: UISwitch!
  30. @IBOutlet weak var useForAllLabel: UILabel!
  31. @IBOutlet weak var useForAllSwitch: UISwitch!
  32. @IBOutlet weak var defaultButton: UIButton!
  33. @IBOutlet weak var cancelButton: UIButton!
  34. @IBOutlet weak var okButton: UIButton!
  35. private let colorPicker = ChromaColorPicker()
  36. private let brightnessSlider = ChromaBrightnessSlider()
  37. private var colorHandle: ChromaColorHandle?
  38. private let defaultColorPickerSize = CGSize(width: 200, height: 200)
  39. private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
  40. private var darkColor = "" // "#000000"
  41. private var lightColor = "" // #FFFFFF
  42. public var collectionViewCommon: NCCollectionViewCommon?
  43. let width: CGFloat = 300
  44. let height: CGFloat = 500
  45. // MARK: - View Life Cycle
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. setupColorPicker()
  49. setupBrightnessSlider()
  50. setupColorPickerHandles()
  51. titleLabel.text = NSLocalizedString("_background_", comment: "")
  52. darkmodeLabel.text = NSLocalizedString("_dark_mode_", comment: "")
  53. useForAllLabel.text = NSLocalizedString("_default_color_all_folders_", comment: "")
  54. defaultButton.setTitle(NSLocalizedString("_default_color_", comment: ""), for: .normal)
  55. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  56. okButton.setTitle(NSLocalizedString("_ok_", comment: ""), for: .normal)
  57. defaultButton.layer.cornerRadius = 10
  58. defaultButton.layer.borderWidth = 0.5
  59. defaultButton.layer.borderColor = UIColor.gray.cgColor
  60. defaultButton.layer.masksToBounds = true
  61. }
  62. override func viewWillAppear(_ animated: Bool) {
  63. super.viewWillAppear(animated)
  64. if traitCollection.userInterfaceStyle == .dark {
  65. darkmodeSwitch.isOn = true
  66. } else {
  67. darkmodeSwitch.isOn = false
  68. }
  69. // Color for this view
  70. if let collectionViewCommon = collectionViewCommon {
  71. let layoutForView = NCUtility.shared.getLayoutForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl)
  72. darkColor = layoutForView.darkColorBackground
  73. lightColor = layoutForView.lightColorBackground
  74. }
  75. // Color all
  76. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  77. if darkColor == "" {
  78. darkColor = activeAccount.darkColorFiles
  79. }
  80. if lightColor == "" {
  81. lightColor = activeAccount.lightColorFiles
  82. }
  83. }
  84. if darkmodeSwitch.isOn {
  85. if let color = UIColor.init(hex: darkColor) {
  86. changeColor(color)
  87. } else {
  88. changeColor(.black)
  89. }
  90. } else {
  91. if let color = UIColor.init(hex: lightColor) {
  92. changeColor(color)
  93. } else {
  94. changeColor(.white)
  95. }
  96. }
  97. }
  98. override func viewDidAppear(_ animated: Bool) {
  99. super.viewDidAppear(animated)
  100. }
  101. // MARK: - Action
  102. @IBAction func darkmodeAction(_ sender: UISwitch) {
  103. if sender.isOn {
  104. if darkColor == "" {
  105. changeColor(.black)
  106. } else {
  107. if let color = UIColor.init(hex: darkColor) {
  108. changeColor(color)
  109. }
  110. }
  111. } else {
  112. if lightColor == "" {
  113. changeColor(.white)
  114. } else {
  115. if let color = UIColor.init(hex: lightColor) {
  116. changeColor(color)
  117. }
  118. }
  119. }
  120. }
  121. @IBAction func defaultAction(_ sender: Any) {
  122. darkColor = ""
  123. lightColor = ""
  124. if darkmodeSwitch.isOn {
  125. changeColor(.black)
  126. } else {
  127. changeColor(.white)
  128. }
  129. }
  130. @IBAction func cancelAction(_ sender: Any) {
  131. collectionViewCommon?.setLayout()
  132. dismiss(animated: true)
  133. }
  134. @IBAction func okAction(_ sender: Any) {
  135. if useForAllSwitch.isOn {
  136. NCManageDatabase.shared.setAccountColorFiles(lightColorFiles: lightColor, darkColorFiles: darkColor)
  137. } else {
  138. if let collectionViewCommon = collectionViewCommon {
  139. NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, lightColorBackground: lightColor, darkColorBackground: darkColor)
  140. }
  141. }
  142. collectionViewCommon?.setLayout()
  143. dismiss(animated: true)
  144. }
  145. // MARK: - ChromaColorPicker
  146. private func setupColorPicker() {
  147. colorPicker.delegate = self
  148. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  149. view.addSubview(colorPicker)
  150. NSLayoutConstraint.activate([
  151. colorPicker.centerXAnchor.constraint(equalTo: chromaColorPickerView.centerXAnchor),
  152. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  153. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  154. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  155. ])
  156. }
  157. private func setupBrightnessSlider() {
  158. brightnessSlider.connect(to: colorPicker)
  159. // Style
  160. brightnessSlider.trackColor = UIColor.blue
  161. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  162. // Layout
  163. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  164. view.addSubview(brightnessSlider)
  165. NSLayoutConstraint.activate([
  166. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  167. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  168. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  169. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  170. ])
  171. }
  172. private func setupColorPickerHandles() {
  173. colorHandle = colorPicker.addHandle(at: collectionViewCommon?.collectionView.backgroundColor)
  174. }
  175. private func changeColor(_ color: UIColor) {
  176. colorHandle?.color = color
  177. colorPicker.setNeedsLayout()
  178. brightnessSlider.trackColor = color
  179. collectionViewCommon?.collectionView.backgroundColor = color
  180. }
  181. }
  182. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  183. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  184. if darkmodeSwitch.isOn {
  185. darkColor = color.hexString
  186. } else {
  187. lightColor = color.hexString
  188. }
  189. collectionViewCommon?.collectionView.backgroundColor = color
  190. }
  191. }