NCBackgroundImageColor.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 defaultButton: UIButton!
  31. @IBOutlet weak var cancelButton: UIButton!
  32. @IBOutlet weak var okButton: UIButton!
  33. private let colorPicker = ChromaColorPicker()
  34. private let brightnessSlider = ChromaBrightnessSlider()
  35. private var colorHandle: ChromaColorHandle?
  36. private let defaultColorPickerSize = CGSize(width: 200, height: 200)
  37. private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
  38. private var darkColorHexString = ""
  39. private var lightColorHexString = ""
  40. public var collectionViewCommon: NCCollectionViewCommon?
  41. public var defaultDarkColor: UIColor = .black
  42. public var defaultLightColor: UIColor = .white
  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. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  54. okButton.setTitle(NSLocalizedString("_ok_", comment: ""), for: .normal)
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. super.viewWillAppear(animated)
  58. if traitCollection.userInterfaceStyle == .dark {
  59. darkmodeSwitch.isOn = true
  60. } else {
  61. darkmodeSwitch.isOn = false
  62. }
  63. }
  64. override func viewDidAppear(_ animated: Bool) {
  65. super.viewDidAppear(animated)
  66. }
  67. // MARK: - Action
  68. @IBAction func darkmodeAction(_ sender: UISwitch) {
  69. var colorHexString = ""
  70. if sender.isOn {
  71. colorHexString = darkColorHexString
  72. } else {
  73. colorHexString = lightColorHexString
  74. }
  75. if let color = UIColor.init(hex: colorHexString) {
  76. changeColor(color)
  77. } else {
  78. }
  79. }
  80. @IBAction func defaultAction(_ sender: Any) {
  81. //changeColor(defaultColor)
  82. darkColorHexString = ""
  83. lightColorHexString = ""
  84. }
  85. @IBAction func cancelAction(_ sender: Any) {
  86. collectionViewCommon?.setLayout()
  87. dismiss(animated: true)
  88. }
  89. @IBAction func okAction(_ sender: Any) {
  90. if let collectionViewCommon = collectionViewCommon {
  91. //NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, colorBackground: "", colorDarkBackground: darkColorHexString)
  92. }
  93. collectionViewCommon?.setLayout()
  94. dismiss(animated: true)
  95. }
  96. // MARK: - ChromaColorPicker
  97. private func setupColorPicker() {
  98. colorPicker.delegate = self
  99. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  100. view.addSubview(colorPicker)
  101. NSLayoutConstraint.activate([
  102. colorPicker.centerXAnchor.constraint(equalTo: chromaColorPickerView.centerXAnchor),
  103. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  104. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  105. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  106. ])
  107. }
  108. private func setupBrightnessSlider() {
  109. brightnessSlider.connect(to: colorPicker)
  110. // Style
  111. brightnessSlider.trackColor = UIColor.blue
  112. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  113. // Layout
  114. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  115. view.addSubview(brightnessSlider)
  116. NSLayoutConstraint.activate([
  117. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  118. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  119. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  120. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  121. ])
  122. }
  123. private func setupColorPickerHandles() {
  124. colorHandle = colorPicker.addHandle(at: collectionViewCommon?.collectionView.backgroundColor)
  125. }
  126. private func changeColor(_ color: UIColor) {
  127. colorHandle?.color = color
  128. colorPicker.setNeedsLayout()
  129. brightnessSlider.trackColor = color
  130. collectionViewCommon?.collectionView.backgroundColor = color
  131. }
  132. }
  133. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  134. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  135. if darkmodeSwitch.isOn {
  136. darkColorHexString = color.hexString
  137. } else {
  138. lightColorHexString = color.hexString
  139. }
  140. collectionViewCommon?.collectionView.backgroundColor = color
  141. }
  142. }