Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NCColorPicker.swift « Color « iOSClient - github.com/nextcloud/ios.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ace49d09a82f9ac20f00ffd7dd3b969e0581522a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
//  NCColorPicker.swift
//  Nextcloud
//
//  Created by Marino Faggiana on 24/07/22.
//  Copyright © 2022 Marino Faggiana. All rights reserved.
//

import Foundation
import UIKit

class NCColorPicker: UIViewController {

    @IBOutlet weak var closeButton: UIButton!
    @IBOutlet weak var titleLabel: UILabel!

    @IBOutlet weak var orangeButton: UIButton!
    @IBOutlet weak var redButton: UIButton!
    @IBOutlet weak var purpleButton: UIButton!
    @IBOutlet weak var blueButton: UIButton!
    @IBOutlet weak var greenButton: UIButton!
    @IBOutlet weak var cyanButton: UIButton!
    @IBOutlet weak var yellowButton: UIButton!
    @IBOutlet weak var grayButton: UIButton!
    @IBOutlet weak var brownButton: UIButton!

    @IBOutlet weak var systemBlueButton: UIButton!
    @IBOutlet weak var systemIndigoButton: UIButton!
    @IBOutlet weak var systemMintButton: UIButton!
    @IBOutlet weak var systemPinkButton: UIButton!

    @IBOutlet weak var defaultButton: UIButton!
    @IBOutlet weak var customButton: UIButton!

    var metadata: tableMetadata?
    var tapAction: UITapGestureRecognizer?
    var selectedColor: UIColor?

    // MARK: - View Life Cycle

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .secondarySystemBackground

        if let metadata = metadata {
            let serverUrl = metadata.serverUrl + "/" + metadata.fileName
            if let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, serverUrl)), let hex = tableDirectory.colorFolder, let color = UIColor(hex: hex) {
                selectedColor = color
            }
        }

        closeButton.setImage(NCUtility.shared.loadImage(named: "xmark", color: .label), for: .normal)
        titleLabel.text = NSLocalizedString("_select_color_", comment: "")

        orangeButton.backgroundColor = .orange
        orangeButton.layer.cornerRadius = 5
        orangeButton.layer.masksToBounds = true

        redButton.backgroundColor = .red
        redButton.layer.cornerRadius = 5
        redButton.layer.masksToBounds = true

        purpleButton.backgroundColor = .purple
        purpleButton.layer.cornerRadius = 5
        purpleButton.layer.masksToBounds = true

        blueButton.backgroundColor = .blue
        blueButton.layer.cornerRadius = 5
        blueButton.layer.masksToBounds = true

        brownButton.backgroundColor = .brown
        brownButton.layer.cornerRadius = 5
        brownButton.layer.masksToBounds = true

        greenButton.backgroundColor = .green
        greenButton.layer.cornerRadius = 5
        greenButton.layer.masksToBounds = true

        grayButton.backgroundColor = .gray
        grayButton.layer.cornerRadius = 5
        grayButton.layer.masksToBounds = true

        cyanButton.backgroundColor = .cyan
        cyanButton.layer.cornerRadius = 5
        cyanButton.layer.masksToBounds = true

        yellowButton.backgroundColor = .yellow
        yellowButton.layer.cornerRadius = 5
        yellowButton.layer.masksToBounds = true

        systemBlueButton.backgroundColor = .systemBlue
        systemBlueButton.layer.cornerRadius = 5
        systemBlueButton.layer.masksToBounds = true

        systemMintButton.backgroundColor = NCBrandColor.shared.systemMint
        systemMintButton.layer.cornerRadius = 5
        systemMintButton.layer.masksToBounds = true

        systemPinkButton.backgroundColor = .systemPink
        systemPinkButton.layer.cornerRadius = 5
        systemPinkButton.layer.masksToBounds = true

        customButton.setImage(UIImage(named: "rgb"), for: .normal)
        if let selectedColor = selectedColor {
            customButton.backgroundColor = selectedColor
        } else {
            customButton.backgroundColor = .secondarySystemBackground
        }
        customButton.layer.cornerRadius = 5
        customButton.layer.masksToBounds = true

        systemIndigoButton.backgroundColor = .systemIndigo
        systemIndigoButton.layer.cornerRadius = 5
        systemIndigoButton.layer.masksToBounds = true

        defaultButton.backgroundColor = NCBrandColor.shared.brandElement
        defaultButton.layer.cornerRadius = 5
        defaultButton.layer.masksToBounds = true
    }

    // MARK: - Action

    @IBAction func closeAction(_ sender: UIButton) {
        dismiss(animated: true)
    }

    @IBAction func orangeButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.orange.hexString)
    }

    @IBAction func redButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.red.hexString)
    }

    @IBAction func purpleButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.purple.hexString)
    }

    @IBAction func blueButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.blue.hexString)
    }

    @IBAction func greenButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.green.hexString)
    }

    @IBAction func cyanButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.cyan.hexString)
    }

    @IBAction func yellowButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.yellow.hexString)
    }

    @IBAction func grayButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.gray.hexString)
    }

    @IBAction func brownButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.brown.hexString)
    }

    @IBAction func systemBlueButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.systemBlue.hexString)
    }

    @IBAction func systemIndigoButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.systemIndigo.hexString)
    }

    @IBAction func systemMintButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: NCBrandColor.shared.systemMint.hexString)
    }

    @IBAction func systemPinkButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: UIColor.systemPink.hexString)
    }

    @IBAction func defaultButtonAction(_ sender: AnyObject) {
        updateColor(hexColor: nil)
    }

    @IBAction func customButtonAction(_ sender: AnyObject) {

        let picker = UIColorPickerViewController()
        picker.delegate = self
        picker.supportsAlpha = false
        if let selectedColor = selectedColor {
            picker.selectedColor = selectedColor
        }
        self.present(picker, animated: true, completion: nil)
    }

    // MARK: -

    func updateColor(hexColor: String?) {
        if let metadata = metadata {
            let serverUrl = metadata.serverUrl + "/" + metadata.fileName
            if NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, colorFolder: hexColor, account: metadata.account) != nil {
                self.dismiss(animated: true)
                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": metadata.serverUrl])
            }
        }
        self.dismiss(animated: true)
    }
}

extension NCColorPicker: UIColorPickerViewControllerDelegate {

    func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
        let hexColor = viewController.selectedColor.hexString
        updateColor(hexColor: hexColor)
    }
}