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

NCRenameFile.swift « Rename file « iOSClient - github.com/nextcloud/ios.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a3627ddf0819483b3192c5210f10b364644a29f (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
//  NCRenameFile.swift
//  Nextcloud
//
//  Created by Marino Faggiana on 26/02/21.
//  Copyright © 2021 Marino Faggiana. All rights reserved.
//
//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import UIKit
import NextcloudKit

public protocol NCRenameFileDelegate: AnyObject {
    func rename(fileName: String, fileNameNew: String)
}

// optional func
public extension NCRenameFileDelegate {
    func rename(fileName: String, fileNameNew: String) {}
}

class NCRenameFile: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var previewFile: UIImageView!
    @IBOutlet weak var fileNameWithoutExt: UITextField!
    @IBOutlet weak var point: UILabel!
    @IBOutlet weak var ext: UITextField!
    @IBOutlet weak var fileNameWithoutExtTrailingContraint: NSLayoutConstraint!
    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var renameButton: UIButton!

    let width: CGFloat = 300
    let height: CGFloat = 310

    var metadata: tableMetadata?
    var fileName: String?
    var imagePreview: UIImage?
    var disableChangeExt: Bool = false
    weak var delegate: NCRenameFileDelegate?

    // MARK: - View Life Cycle

    override func viewDidLoad() {
        super.viewDidLoad()

        if let metadata = self.metadata {

            if metadata.directory {
                titleLabel.text = NSLocalizedString("_rename_folder_", comment: "")
            } else {
                titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
            }

            fileNameWithoutExt.text = (metadata.fileNameView as NSString).deletingPathExtension
            fileNameWithoutExt.delegate = self
            fileNameWithoutExt.becomeFirstResponder()

            ext.text = metadata.fileExtension
            ext.delegate = self
            if disableChangeExt {
                ext.isEnabled = false
                ext.textColor = .lightGray
            }

            previewFile.image = imagePreview
            previewFile.layer.cornerRadius = 10
            previewFile.layer.masksToBounds = true

            if metadata.directory {

                if imagePreview == nil {
                    previewFile.image = NCBrandColor.cacheImages.folder
                }

                ext.isHidden = true
                point.isHidden = true
                fileNameWithoutExtTrailingContraint.constant = 20

            } else {

                if imagePreview == nil {
                    previewFile.image = NCBrandColor.cacheImages.file
                }

                fileNameWithoutExtTrailingContraint.constant = 90
            }

        } else if let fileName = self.fileName {

            titleLabel.text = NSLocalizedString("_rename_file_", comment: "")

            fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
            fileNameWithoutExt.delegate = self
            fileNameWithoutExt.becomeFirstResponder()
            fileNameWithoutExtTrailingContraint.constant = 90

            ext.text = (fileName as NSString).pathExtension
            ext.delegate = self

            if imagePreview == nil {
                previewFile.image = NCBrandColor.cacheImages.file
            } else {
                previewFile.image = imagePreview
            }
            previewFile.layer.cornerRadius = 10
            previewFile.layer.masksToBounds = true
        }

        cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
        renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if metadata == nil && fileName == nil {
            dismiss(animated: true)
        }

        fileNameWithoutExt.selectAll(nil)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        textField.resignFirstResponder()
        renameFile(textField)
        return true
    }

    // MARK: - Action

    @IBAction func cancel(_ sender: Any) {

        dismiss(animated: true)
    }

    @IBAction func renameFile(_ sender: Any) {

        var fileNameWithoutExtNew = ""
        var extNew = ""
        var fileNameNew = ""

        if let metadata = self.metadata {

            let extCurrent = (metadata.fileNameView as NSString).pathExtension

            if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
                self.fileNameWithoutExt.text = (metadata.fileNameView as NSString).deletingPathExtension
                return
            } else {
                fileNameWithoutExtNew = fileNameWithoutExt.text!
            }

            if metadata.directory {

                fileNameNew = fileNameWithoutExtNew
                renameMetadata(metadata, fileNameNew: fileNameNew)

            } else {

                if ext.text == nil || ext.text?.count == 0 {
                    self.ext.text = metadata.fileExtension
                    return
                } else {
                    extNew = ext.text!
                }

                if extNew != extCurrent {

                    let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, extCurrent)
                    let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)

                    var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
                    alertController.addAction(UIAlertAction(title: title, style: .default, handler: { _ in

                        fileNameNew = fileNameWithoutExtNew + "." + extNew
                        self.renameMetadata(metadata, fileNameNew: fileNameNew)
                    }))

                    title = NSLocalizedString("_keep_", comment: "") + " ." + extCurrent
                    alertController.addAction(UIAlertAction(title: title, style: .default, handler: { _ in
                        self.ext.text = metadata.fileExtension
                    }))

                    self.present(alertController, animated: true)

                } else {

                    fileNameNew = fileNameWithoutExtNew + "." + extNew
                    renameMetadata(metadata, fileNameNew: fileNameNew)
                }
            }

        } else if let fileName = self.fileName {

            if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
                fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
                return
            } else if ext.text == nil || ext.text?.count == 0 {
                ext.text = (fileName as NSString).pathExtension
                return
            }

            fileNameNew = (fileNameWithoutExt.text ?? "") + "." + (ext.text ?? "")
            self.dismiss(animated: true) {
                self.delegate?.rename(fileName: fileName, fileNameNew: fileNameNew)
            }
        }
    }

    // MARK: - Networking

    func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {

        NCActivityIndicator.shared.start()

        NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, viewController: self) { error in

            NCActivityIndicator.shared.stop()

            if error == .success {

                self.dismiss(animated: true)

            } else {

                NCContentPresenter.shared.showError(error: error)
            }
        }
    }
}