NCBackgroundImageColor.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. useForAllSwitch.isOn = false
  70. // Color for this view
  71. if let collectionViewCommon = collectionViewCommon {
  72. let layoutForView = NCUtility.shared.getLayoutForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl)
  73. darkColor = layoutForView.darkColorBackground
  74. lightColor = layoutForView.lightColorBackground
  75. }
  76. // Color for all folders
  77. if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
  78. if darkColor == "" {
  79. darkColor = activeAccount.darkColorFiles
  80. }
  81. if lightColor == "" {
  82. lightColor = activeAccount.lightColorFiles
  83. }
  84. }
  85. if lightColor != "" || darkColor != "" {
  86. useForAllSwitch.isOn = true
  87. }
  88. // set color
  89. if darkmodeSwitch.isOn {
  90. if let color = UIColor.init(hex: darkColor) {
  91. changeColor(color)
  92. } else {
  93. changeColor(.black)
  94. }
  95. } else {
  96. if let color = UIColor.init(hex: lightColor) {
  97. changeColor(color)
  98. } else {
  99. changeColor(.white)
  100. }
  101. }
  102. }
  103. override func viewDidAppear(_ animated: Bool) {
  104. super.viewDidAppear(animated)
  105. }
  106. // MARK: - Action
  107. @IBAction func darkmodeAction(_ sender: UISwitch) {
  108. if sender.isOn {
  109. if darkColor == "" {
  110. changeColor(.black)
  111. } else {
  112. if let color = UIColor.init(hex: darkColor) {
  113. changeColor(color)
  114. }
  115. }
  116. } else {
  117. if lightColor == "" {
  118. changeColor(.white)
  119. } else {
  120. if let color = UIColor.init(hex: lightColor) {
  121. changeColor(color)
  122. }
  123. }
  124. }
  125. }
  126. @IBAction func defaultAction(_ sender: Any) {
  127. darkColor = ""
  128. lightColor = ""
  129. if darkmodeSwitch.isOn {
  130. changeColor(.black)
  131. } else {
  132. changeColor(.white)
  133. }
  134. }
  135. @IBAction func cancelAction(_ sender: Any) {
  136. collectionViewCommon?.setLayout()
  137. dismiss(animated: true)
  138. }
  139. @IBAction func okAction(_ sender: Any) {
  140. if useForAllSwitch.isOn {
  141. NCManageDatabase.shared.setAccountColorFiles(lightColorFiles: lightColor, darkColorFiles: darkColor)
  142. } else {
  143. if let collectionViewCommon = collectionViewCommon {
  144. NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, lightColorBackground: lightColor, darkColorBackground: darkColor)
  145. }
  146. }
  147. collectionViewCommon?.setLayout()
  148. dismiss(animated: true)
  149. }
  150. // MARK: - ChromaColorPicker
  151. private func setupColorPicker() {
  152. colorPicker.delegate = self
  153. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  154. view.addSubview(colorPicker)
  155. NSLayoutConstraint.activate([
  156. colorPicker.centerXAnchor.constraint(equalTo: chromaColorPickerView.centerXAnchor),
  157. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  158. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  159. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  160. ])
  161. }
  162. private func setupBrightnessSlider() {
  163. brightnessSlider.connect(to: colorPicker)
  164. // Style
  165. brightnessSlider.trackColor = UIColor.blue
  166. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  167. // Layout
  168. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  169. view.addSubview(brightnessSlider)
  170. NSLayoutConstraint.activate([
  171. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  172. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  173. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  174. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  175. ])
  176. }
  177. private func setupColorPickerHandles() {
  178. colorHandle = colorPicker.addHandle(at: collectionViewCommon?.collectionView.backgroundColor)
  179. }
  180. private func changeColor(_ color: UIColor) {
  181. colorHandle?.color = color
  182. colorPicker.setNeedsLayout()
  183. brightnessSlider.trackColor = color
  184. collectionViewCommon?.collectionView.backgroundColor = color
  185. }
  186. }
  187. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  188. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  189. if darkmodeSwitch.isOn {
  190. darkColor = color.hexString
  191. } else {
  192. lightColor = color.hexString
  193. }
  194. collectionViewCommon?.collectionView.backgroundColor = color
  195. }
  196. }