NCBackgroundImageColor.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 var isDarkMode: Bool = false
  37. private let defaultColorPickerSize = CGSize(width: 200, height: 200)
  38. private let brightnessSliderWidthHeightRatio: CGFloat = 0.1
  39. private var darkColorHexString = ""
  40. private var lightColorHexString = ""
  41. public var collectionViewCommon: NCCollectionViewCommon?
  42. public var defaultColor: UIColor = NCBrandColor.shared.systemBackground
  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. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  68. super.traitCollectionDidChange(previousTraitCollection)
  69. }
  70. // MARK: - Action
  71. @IBAction func darkmodeAction(_ sender: UISwitch) {
  72. isDarkMode = sender.isOn
  73. }
  74. @IBAction func defaultAction(_ sender: Any) {
  75. colorHandle?.color = defaultColor
  76. colorPicker.setNeedsLayout()
  77. brightnessSlider.trackColor = defaultColor
  78. collectionViewCommon?.collectionView.backgroundColor = defaultColor
  79. }
  80. @IBAction func cancelAction(_ sender: Any) {
  81. collectionViewCommon?.setLayout()
  82. dismiss(animated: true)
  83. }
  84. @IBAction func okAction(_ sender: Any) {
  85. if let collectionViewCommon = collectionViewCommon {
  86. if colorHandle?.color == defaultColor {
  87. NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, colorBackground: "", colorDarkBackground: "")
  88. } else {
  89. //NCUtility.shared.setBackgroundColorForView(key: collectionViewCommon.layoutKey, serverUrl: collectionViewCommon.serverUrl, colorBackground: colorHexString)
  90. }
  91. }
  92. collectionViewCommon?.setLayout()
  93. dismiss(animated: true)
  94. }
  95. // MARK: - ChromaColorPicker
  96. private func setupColorPicker() {
  97. colorPicker.delegate = self
  98. colorPicker.translatesAutoresizingMaskIntoConstraints = false
  99. view.addSubview(colorPicker)
  100. NSLayoutConstraint.activate([
  101. colorPicker.centerXAnchor.constraint(equalTo: chromaColorPickerView.centerXAnchor),
  102. colorPicker.topAnchor.constraint(equalTo: chromaColorPickerView.topAnchor),
  103. colorPicker.widthAnchor.constraint(equalToConstant: defaultColorPickerSize.width),
  104. colorPicker.heightAnchor.constraint(equalToConstant: defaultColorPickerSize.height)
  105. ])
  106. }
  107. private func setupBrightnessSlider() {
  108. brightnessSlider.connect(to: colorPicker)
  109. // Style
  110. brightnessSlider.trackColor = UIColor.blue
  111. brightnessSlider.handle.borderWidth = 3.0 // Example of customizing the handle's properties.
  112. // Layout
  113. brightnessSlider.translatesAutoresizingMaskIntoConstraints = false
  114. view.addSubview(brightnessSlider)
  115. NSLayoutConstraint.activate([
  116. brightnessSlider.centerXAnchor.constraint(equalTo: colorPicker.centerXAnchor),
  117. brightnessSlider.topAnchor.constraint(equalTo: colorPicker.bottomAnchor, constant: 20),
  118. brightnessSlider.widthAnchor.constraint(equalTo: colorPicker.widthAnchor, multiplier: 1),
  119. brightnessSlider.heightAnchor.constraint(equalTo: brightnessSlider.widthAnchor, multiplier: brightnessSliderWidthHeightRatio)
  120. ])
  121. }
  122. private func setupColorPickerHandles() {
  123. colorHandle = colorPicker.addHandle(at: collectionViewCommon?.collectionView.backgroundColor)
  124. }
  125. }
  126. extension NCBackgroundImageColor: ChromaColorPickerDelegate {
  127. func colorPickerHandleDidChange(_ colorPicker: ChromaColorPicker, handle: ChromaColorHandle, to color: UIColor) {
  128. if darkmodeSwitch.isOn {
  129. darkColorHexString = color.hexString
  130. } else {
  131. lightColorHexString = color.hexString
  132. }
  133. collectionViewCommon?.collectionView.backgroundColor = color
  134. }
  135. }