NCRenameFile.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if let metadata = self.metadata {
  51. if metadata.directory {
  52. titleLabel.text = NSLocalizedString("_rename_folder_", comment: "")
  53. } else {
  54. titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
  55. }
  56. separatorHeightContraint.constant = 0.3
  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. cancelButton.setTitleColor(.gray, for: .normal)
  100. cancelButton.layer.cornerRadius = 15
  101. cancelButton.layer.masksToBounds = true
  102. cancelButton.layer.backgroundColor = NCBrandColor.shared.graySoft.withAlphaComponent(0.2).cgColor
  103. cancelButton.layer.borderWidth = 0.3
  104. cancelButton.layer.borderColor = UIColor.gray.cgColor
  105. renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
  106. renameButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  107. renameButton.layer.cornerRadius = 15
  108. renameButton.layer.masksToBounds = true
  109. renameButton.layer.backgroundColor = NCBrandColor.shared.brand.cgColor
  110. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  111. changeTheming()
  112. }
  113. override func viewWillAppear(_ animated: Bool) {
  114. super.viewWillAppear(animated)
  115. }
  116. override func viewDidAppear(_ animated: Bool) {
  117. super.viewDidAppear(animated)
  118. if metadata == nil && fileName == nil {
  119. dismiss(animated: true)
  120. }
  121. fileNameWithoutExt.selectAll(nil)
  122. }
  123. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  124. textField.resignFirstResponder()
  125. rename(textField)
  126. return true
  127. }
  128. // MARK: - NotificationCenter
  129. @objc func changeTheming() {
  130. view.backgroundColor = NCBrandColor.shared.backgroundViewForm
  131. }
  132. // MARK: - Action
  133. @IBAction func cancel(_ sender: Any) {
  134. dismiss(animated: true)
  135. }
  136. @IBAction func rename(_ sender: Any) {
  137. var fileNameWithoutExtNew = ""
  138. var extNew = ""
  139. var fileNameNew = ""
  140. if let metadata = self.metadata {
  141. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  142. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  143. return
  144. } else {
  145. fileNameWithoutExtNew = fileNameWithoutExt.text!
  146. }
  147. if metadata.directory {
  148. fileNameNew = fileNameWithoutExtNew
  149. renameMetadata(metadata, fileNameNew: fileNameNew)
  150. } else {
  151. if ext.text == nil || ext.text?.count == 0 {
  152. self.ext.text = metadata.ext
  153. return
  154. } else {
  155. extNew = ext.text!
  156. }
  157. if extNew != metadata.ext {
  158. let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
  159. let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
  160. var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
  161. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  162. fileNameNew = fileNameWithoutExtNew + "." + extNew
  163. self.renameMetadata(metadata, fileNameNew: fileNameNew)
  164. }))
  165. title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
  166. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  167. self.ext.text = metadata.ext
  168. }))
  169. self.present(alertController, animated: true)
  170. } else {
  171. fileNameNew = fileNameWithoutExtNew + "." + extNew
  172. renameMetadata(metadata, fileNameNew: fileNameNew)
  173. }
  174. }
  175. } else if let fileName = self.fileName {
  176. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  177. fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
  178. return
  179. } else if ext.text == nil || ext.text?.count == 0 {
  180. ext.text = (fileName as NSString).pathExtension
  181. return
  182. }
  183. fileNameNew = (fileNameWithoutExt.text ?? "") + "." + (ext.text ?? "")
  184. self.delegate?.rename(fileName: fileName, fileNameNew: fileNameNew)
  185. self.dismiss(animated: true)
  186. }
  187. }
  188. // MARK: - Networking
  189. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  190. NCUtility.shared.startActivityIndicator(backgroundView: nil, blurEffect: true)
  191. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  192. NCUtility.shared.stopActivityIndicator()
  193. if errorCode == 0 {
  194. self.dismiss(animated: true)
  195. } else {
  196. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  197. }
  198. }
  199. }
  200. }