NCColorPicker.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // NCColorPicker.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/07/22.
  6. // Copyright © 2022 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 Foundation
  24. import UIKit
  25. class NCColorPicker: UIViewController {
  26. @IBOutlet weak var closeButton: UIButton!
  27. @IBOutlet weak var titleLabel: UILabel!
  28. @IBOutlet weak var orangeButton: UIButton!
  29. @IBOutlet weak var redButton: UIButton!
  30. @IBOutlet weak var purpleButton: UIButton!
  31. @IBOutlet weak var blueButton: UIButton!
  32. @IBOutlet weak var greenButton: UIButton!
  33. @IBOutlet weak var cyanButton: UIButton!
  34. @IBOutlet weak var yellowButton: UIButton!
  35. @IBOutlet weak var grayButton: UIButton!
  36. @IBOutlet weak var brownButton: UIButton!
  37. @IBOutlet weak var systemBlueButton: UIButton!
  38. @IBOutlet weak var systemIndigoButton: UIButton!
  39. @IBOutlet weak var systemMintButton: UIButton!
  40. @IBOutlet weak var systemPinkButton: UIButton!
  41. @IBOutlet weak var defaultButton: UIButton!
  42. @IBOutlet weak var customButton: UIButton!
  43. var metadata: tableMetadata?
  44. var tapAction: UITapGestureRecognizer?
  45. var selectedColor: UIColor?
  46. // MARK: - View Life Cycle
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. view.backgroundColor = .secondarySystemBackground
  50. if let metadata = metadata {
  51. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  52. if let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, serverUrl)), let hex = tableDirectory.colorFolder, let color = UIColor(hex: hex) {
  53. selectedColor = color
  54. }
  55. }
  56. closeButton.setImage(NCUtility().loadImage(named: "xmark", colors: [NCBrandColor.shared.iconImageColor]), for: .normal)
  57. titleLabel.text = NSLocalizedString("_select_color_", comment: "")
  58. orangeButton.backgroundColor = .orange
  59. orangeButton.layer.cornerRadius = 5
  60. orangeButton.layer.masksToBounds = true
  61. redButton.backgroundColor = .red
  62. redButton.layer.cornerRadius = 5
  63. redButton.layer.masksToBounds = true
  64. purpleButton.backgroundColor = .purple
  65. purpleButton.layer.cornerRadius = 5
  66. purpleButton.layer.masksToBounds = true
  67. blueButton.backgroundColor = .blue
  68. blueButton.layer.cornerRadius = 5
  69. blueButton.layer.masksToBounds = true
  70. brownButton.backgroundColor = .brown
  71. brownButton.layer.cornerRadius = 5
  72. brownButton.layer.masksToBounds = true
  73. greenButton.backgroundColor = .green
  74. greenButton.layer.cornerRadius = 5
  75. greenButton.layer.masksToBounds = true
  76. grayButton.backgroundColor = .gray
  77. grayButton.layer.cornerRadius = 5
  78. grayButton.layer.masksToBounds = true
  79. cyanButton.backgroundColor = .cyan
  80. cyanButton.layer.cornerRadius = 5
  81. cyanButton.layer.masksToBounds = true
  82. yellowButton.backgroundColor = .yellow
  83. yellowButton.layer.cornerRadius = 5
  84. yellowButton.layer.masksToBounds = true
  85. systemBlueButton.backgroundColor = .systemBlue
  86. systemBlueButton.layer.cornerRadius = 5
  87. systemBlueButton.layer.masksToBounds = true
  88. systemMintButton.backgroundColor = NCBrandColor.shared.systemMint
  89. systemMintButton.layer.cornerRadius = 5
  90. systemMintButton.layer.masksToBounds = true
  91. systemPinkButton.backgroundColor = .systemPink
  92. systemPinkButton.layer.cornerRadius = 5
  93. systemPinkButton.layer.masksToBounds = true
  94. customButton.setImage(UIImage(named: "rgb"), for: .normal)
  95. if let selectedColor = selectedColor {
  96. customButton.backgroundColor = selectedColor
  97. } else {
  98. customButton.backgroundColor = .secondarySystemBackground
  99. }
  100. customButton.layer.cornerRadius = 5
  101. customButton.layer.masksToBounds = true
  102. systemIndigoButton.backgroundColor = .systemIndigo
  103. systemIndigoButton.layer.cornerRadius = 5
  104. systemIndigoButton.layer.masksToBounds = true
  105. defaultButton.backgroundColor = NCBrandColor.shared.customer
  106. defaultButton.layer.cornerRadius = 5
  107. defaultButton.layer.masksToBounds = true
  108. }
  109. // MARK: - Action
  110. @IBAction func closeAction(_ sender: UIButton) {
  111. dismiss(animated: true)
  112. }
  113. @IBAction func orangeButtonAction(_ sender: AnyObject) {
  114. updateColor(hexColor: UIColor.orange.hexString)
  115. }
  116. @IBAction func redButtonAction(_ sender: AnyObject) {
  117. updateColor(hexColor: UIColor.red.hexString)
  118. }
  119. @IBAction func purpleButtonAction(_ sender: AnyObject) {
  120. updateColor(hexColor: UIColor.purple.hexString)
  121. }
  122. @IBAction func blueButtonAction(_ sender: AnyObject) {
  123. updateColor(hexColor: UIColor.blue.hexString)
  124. }
  125. @IBAction func greenButtonAction(_ sender: AnyObject) {
  126. updateColor(hexColor: UIColor.green.hexString)
  127. }
  128. @IBAction func cyanButtonAction(_ sender: AnyObject) {
  129. updateColor(hexColor: UIColor.cyan.hexString)
  130. }
  131. @IBAction func yellowButtonAction(_ sender: AnyObject) {
  132. updateColor(hexColor: UIColor.yellow.hexString)
  133. }
  134. @IBAction func grayButtonAction(_ sender: AnyObject) {
  135. updateColor(hexColor: UIColor.gray.hexString)
  136. }
  137. @IBAction func brownButtonAction(_ sender: AnyObject) {
  138. updateColor(hexColor: UIColor.brown.hexString)
  139. }
  140. @IBAction func systemBlueButtonAction(_ sender: AnyObject) {
  141. updateColor(hexColor: UIColor.systemBlue.hexString)
  142. }
  143. @IBAction func systemIndigoButtonAction(_ sender: AnyObject) {
  144. updateColor(hexColor: UIColor.systemIndigo.hexString)
  145. }
  146. @IBAction func systemMintButtonAction(_ sender: AnyObject) {
  147. updateColor(hexColor: NCBrandColor.shared.systemMint.hexString)
  148. }
  149. @IBAction func systemPinkButtonAction(_ sender: AnyObject) {
  150. updateColor(hexColor: UIColor.systemPink.hexString)
  151. }
  152. @IBAction func defaultButtonAction(_ sender: AnyObject) {
  153. updateColor(hexColor: nil)
  154. }
  155. @IBAction func customButtonAction(_ sender: AnyObject) {
  156. let picker = UIColorPickerViewController()
  157. picker.delegate = self
  158. picker.supportsAlpha = false
  159. if let selectedColor = selectedColor {
  160. picker.selectedColor = selectedColor
  161. }
  162. self.present(picker, animated: true, completion: nil)
  163. }
  164. // MARK: -
  165. func updateColor(hexColor: String?) {
  166. if let metadata = metadata {
  167. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  168. NCManageDatabase.shared.updateDirectoryColorFolder(hexColor, metadata: metadata, serverUrl: serverUrl)
  169. self.dismiss(animated: true)
  170. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": metadata.serverUrl, "clearDataSource": true])
  171. }
  172. self.dismiss(animated: true)
  173. }
  174. }
  175. extension NCColorPicker: UIColorPickerViewControllerDelegate {
  176. func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
  177. let hexColor = viewController.selectedColor.hexString
  178. updateColor(hexColor: hexColor)
  179. }
  180. }