NCRenameFile.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 Foundation
  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 separatorHeightContraint: NSLayoutConstraint!
  35. @IBOutlet weak var previewFile: UIImageView!
  36. @IBOutlet weak var fileNameWithoutExt: UITextField!
  37. @IBOutlet weak var point: UILabel!
  38. @IBOutlet weak var ext: UITextField!
  39. @IBOutlet weak var fileNameWithoutExtTrailingContraint: NSLayoutConstraint!
  40. @IBOutlet weak var cancelButton: UIButton!
  41. @IBOutlet weak var renameButton: UIButton!
  42. var metadata: tableMetadata?
  43. var fileName: String?
  44. var imagePreview: UIImage?
  45. var disableChangeExt: Bool = false
  46. var delegate: NCRenameFileDelegate?
  47. // MARK: - Life Cycle
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. view.backgroundColor = NCBrandColor.shared.secondarySystemBackground
  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. separatorHeightContraint.constant = 0.3
  58. fileNameWithoutExt.text = metadata.fileNameWithoutExt
  59. fileNameWithoutExt.delegate = self
  60. fileNameWithoutExt.becomeFirstResponder()
  61. ext.text = metadata.ext
  62. ext.delegate = self
  63. if disableChangeExt {
  64. ext.isEnabled = false
  65. ext.textColor = .lightGray
  66. }
  67. previewFile.image = imagePreview
  68. previewFile.layer.cornerRadius = 10
  69. previewFile.layer.masksToBounds = true
  70. if metadata.directory {
  71. if imagePreview == nil {
  72. previewFile.image = NCBrandColor.cacheImages.folder
  73. }
  74. ext.isHidden = true
  75. point.isHidden = true
  76. fileNameWithoutExtTrailingContraint.constant = 20
  77. } else {
  78. if imagePreview == nil {
  79. previewFile.image = NCBrandColor.cacheImages.file
  80. }
  81. fileNameWithoutExtTrailingContraint.constant = 90
  82. }
  83. } else if let fileName = self.fileName {
  84. titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
  85. fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
  86. fileNameWithoutExt.delegate = self
  87. fileNameWithoutExt.becomeFirstResponder()
  88. fileNameWithoutExtTrailingContraint.constant = 90
  89. ext.text = (fileName as NSString).pathExtension
  90. ext.delegate = self
  91. if imagePreview == nil {
  92. previewFile.image = NCBrandColor.cacheImages.file
  93. } else {
  94. previewFile.image = imagePreview
  95. }
  96. previewFile.layer.cornerRadius = 10
  97. previewFile.layer.masksToBounds = true
  98. }
  99. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  100. cancelButton.setTitleColor(.gray, for: .normal)
  101. cancelButton.layer.cornerRadius = 15
  102. cancelButton.layer.masksToBounds = true
  103. cancelButton.layer.backgroundColor = NCBrandColor.shared.graySoft.withAlphaComponent(0.2).cgColor
  104. cancelButton.layer.borderWidth = 0.3
  105. cancelButton.layer.borderColor = UIColor.gray.cgColor
  106. renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
  107. renameButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  108. renameButton.layer.cornerRadius = 15
  109. renameButton.layer.masksToBounds = true
  110. renameButton.layer.backgroundColor = NCBrandColor.shared.brand.cgColor
  111. }
  112. override func viewWillAppear(_ animated: Bool) {
  113. super.viewWillAppear(animated)
  114. }
  115. override func viewDidAppear(_ animated: Bool) {
  116. super.viewDidAppear(animated)
  117. if metadata == nil && fileName == nil {
  118. dismiss(animated: true)
  119. }
  120. fileNameWithoutExt.selectAll(nil)
  121. }
  122. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  123. textField.resignFirstResponder()
  124. rename(textField)
  125. return true
  126. }
  127. // MARK: - Action
  128. @IBAction func cancel(_ sender: Any) {
  129. dismiss(animated: true)
  130. }
  131. @IBAction func rename(_ sender: Any) {
  132. var fileNameWithoutExtNew = ""
  133. var extNew = ""
  134. var fileNameNew = ""
  135. if let metadata = self.metadata {
  136. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  137. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  138. return
  139. } else {
  140. fileNameWithoutExtNew = fileNameWithoutExt.text!
  141. }
  142. if metadata.directory {
  143. fileNameNew = fileNameWithoutExtNew
  144. renameMetadata(metadata, fileNameNew: fileNameNew)
  145. } else {
  146. if ext.text == nil || ext.text?.count == 0 {
  147. self.ext.text = metadata.ext
  148. return
  149. } else {
  150. extNew = ext.text!
  151. }
  152. if extNew != metadata.ext {
  153. let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
  154. let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
  155. var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
  156. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  157. fileNameNew = fileNameWithoutExtNew + "." + extNew
  158. self.renameMetadata(metadata, fileNameNew: fileNameNew)
  159. }))
  160. title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
  161. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  162. self.ext.text = metadata.ext
  163. }))
  164. self.present(alertController, animated: true)
  165. } else {
  166. fileNameNew = fileNameWithoutExtNew + "." + extNew
  167. renameMetadata(metadata, fileNameNew: fileNameNew)
  168. }
  169. }
  170. } else if let fileName = self.fileName {
  171. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  172. fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
  173. return
  174. } else if ext.text == nil || ext.text?.count == 0 {
  175. ext.text = (fileName as NSString).pathExtension
  176. return
  177. }
  178. fileNameNew = (fileNameWithoutExt.text ?? "") + "." + (ext.text ?? "")
  179. self.delegate?.rename(fileName: fileName, fileNameNew: fileNameNew)
  180. self.dismiss(animated: true)
  181. }
  182. }
  183. // MARK: - Networking
  184. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  185. NCUtility.shared.startActivityIndicator(backgroundView: nil, blurEffect: true)
  186. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  187. NCUtility.shared.stopActivityIndicator()
  188. if errorCode == 0 {
  189. self.dismiss(animated: true)
  190. } else {
  191. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  192. }
  193. }
  194. }
  195. }