NCRenameFile.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // NCRenameFile.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 26/02/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 NCCommunication
  25. public protocol NCRenameFileDelegate {
  26. func rename(fileName: String, fileNameNew: String)
  27. }
  28. // optional func
  29. public extension NCRenameFileDelegate {
  30. func rename(fileName: String, fileNameNew: String) {}
  31. }
  32. class NCRenameFile: UIViewController, UITextFieldDelegate {
  33. @IBOutlet weak var titleLabel: UILabel!
  34. @IBOutlet weak var previewFile: UIImageView!
  35. @IBOutlet weak var fileNameWithoutExt: UITextField!
  36. @IBOutlet weak var point: UILabel!
  37. @IBOutlet weak var ext: UITextField!
  38. @IBOutlet weak var fileNameWithoutExtTrailingContraint: NSLayoutConstraint!
  39. @IBOutlet weak var cancelButton: UIButton!
  40. @IBOutlet weak var renameButton: UIButton!
  41. let width: CGFloat = 300
  42. let height: CGFloat = 310
  43. var metadata: tableMetadata?
  44. var fileName: String?
  45. var imagePreview: UIImage?
  46. var disableChangeExt: Bool = false
  47. var delegate: NCRenameFileDelegate?
  48. // MARK: - View Life Cycle
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. if let metadata = self.metadata {
  52. if metadata.directory {
  53. titleLabel.text = NSLocalizedString("_rename_folder_", comment: "")
  54. } else {
  55. titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
  56. }
  57. fileNameWithoutExt.text = metadata.fileNameWithoutExt
  58. fileNameWithoutExt.delegate = self
  59. fileNameWithoutExt.becomeFirstResponder()
  60. ext.text = metadata.ext
  61. ext.delegate = self
  62. if disableChangeExt {
  63. ext.isEnabled = false
  64. ext.textColor = .lightGray
  65. }
  66. previewFile.image = imagePreview
  67. previewFile.layer.cornerRadius = 10
  68. previewFile.layer.masksToBounds = true
  69. if metadata.directory {
  70. if imagePreview == nil {
  71. previewFile.image = NCBrandColor.cacheImages.folder
  72. }
  73. ext.isHidden = true
  74. point.isHidden = true
  75. fileNameWithoutExtTrailingContraint.constant = 20
  76. } else {
  77. if imagePreview == nil {
  78. previewFile.image = NCBrandColor.cacheImages.file
  79. }
  80. fileNameWithoutExtTrailingContraint.constant = 90
  81. }
  82. } else if let fileName = self.fileName {
  83. titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
  84. fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
  85. fileNameWithoutExt.delegate = self
  86. fileNameWithoutExt.becomeFirstResponder()
  87. fileNameWithoutExtTrailingContraint.constant = 90
  88. ext.text = (fileName as NSString).pathExtension
  89. ext.delegate = self
  90. if imagePreview == nil {
  91. previewFile.image = NCBrandColor.cacheImages.file
  92. } else {
  93. previewFile.image = imagePreview
  94. }
  95. previewFile.layer.cornerRadius = 10
  96. previewFile.layer.masksToBounds = true
  97. }
  98. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  99. renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
  100. }
  101. override func viewWillAppear(_ animated: Bool) {
  102. super.viewWillAppear(animated)
  103. }
  104. override func viewDidAppear(_ animated: Bool) {
  105. super.viewDidAppear(animated)
  106. if metadata == nil && fileName == nil {
  107. dismiss(animated: true)
  108. }
  109. fileNameWithoutExt.selectAll(nil)
  110. }
  111. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  112. textField.resignFirstResponder()
  113. rename(textField)
  114. return true
  115. }
  116. // MARK: - Action
  117. @IBAction func cancel(_ sender: Any) {
  118. dismiss(animated: true)
  119. }
  120. @IBAction func rename(_ sender: Any) {
  121. var fileNameWithoutExtNew = ""
  122. var extNew = ""
  123. var fileNameNew = ""
  124. if let metadata = self.metadata {
  125. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  126. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  127. return
  128. } else {
  129. fileNameWithoutExtNew = fileNameWithoutExt.text!
  130. }
  131. if metadata.directory {
  132. fileNameNew = fileNameWithoutExtNew
  133. renameMetadata(metadata, fileNameNew: fileNameNew)
  134. } else {
  135. if ext.text == nil || ext.text?.count == 0 {
  136. self.ext.text = metadata.ext
  137. return
  138. } else {
  139. extNew = ext.text!
  140. }
  141. if extNew != metadata.ext {
  142. let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
  143. let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
  144. var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
  145. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  146. fileNameNew = fileNameWithoutExtNew + "." + extNew
  147. self.renameMetadata(metadata, fileNameNew: fileNameNew)
  148. }))
  149. title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
  150. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  151. self.ext.text = metadata.ext
  152. }))
  153. self.present(alertController, animated: true)
  154. } else {
  155. fileNameNew = fileNameWithoutExtNew + "." + extNew
  156. renameMetadata(metadata, fileNameNew: fileNameNew)
  157. }
  158. }
  159. } else if let fileName = self.fileName {
  160. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  161. fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
  162. return
  163. } else if ext.text == nil || ext.text?.count == 0 {
  164. ext.text = (fileName as NSString).pathExtension
  165. return
  166. }
  167. fileNameNew = (fileNameWithoutExt.text ?? "") + "." + (ext.text ?? "")
  168. self.delegate?.rename(fileName: fileName, fileNameNew: fileNameNew)
  169. self.dismiss(animated: true)
  170. }
  171. }
  172. // MARK: - Networking
  173. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  174. NCUtility.shared.startActivityIndicator(backgroundView: nil, blurEffect: true)
  175. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, viewController: self) { (errorCode, errorDescription) in
  176. NCUtility.shared.stopActivityIndicator()
  177. if errorCode == 0 {
  178. self.dismiss(animated: true)
  179. } else {
  180. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  181. }
  182. }
  183. }
  184. }