NCBackgroundImageColor.swift 8.7 KB

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