NCRenameFile.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. class NCRenameFile: UIViewController, UITextFieldDelegate {
  26. @IBOutlet weak var titleLabel: UILabel!
  27. @IBOutlet weak var separatorHeightContraint: NSLayoutConstraint!
  28. @IBOutlet weak var previewFile: UIImageView!
  29. @IBOutlet weak var fileNameWithoutExt: UITextField!
  30. @IBOutlet weak var point: UILabel!
  31. @IBOutlet weak var ext: UITextField!
  32. @IBOutlet weak var fileNameWithoutExtTrailingContraint: NSLayoutConstraint!
  33. @IBOutlet weak var cancelButton: UIButton!
  34. @IBOutlet weak var renameButton: UIButton!
  35. var metadata: tableMetadata?
  36. var imagePreview: UIImage?
  37. var disableChangeExt: Bool = false
  38. // MARK: - Life Cycle
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. if let metadata = self.metadata {
  42. if metadata.directory {
  43. titleLabel.text = NSLocalizedString("_rename_folder_", comment: "")
  44. } else {
  45. titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
  46. }
  47. separatorHeightContraint.constant = 0.3
  48. fileNameWithoutExt.text = metadata.fileNameWithoutExt
  49. fileNameWithoutExt.delegate = self
  50. fileNameWithoutExt.becomeFirstResponder()
  51. ext.text = metadata.ext
  52. ext.delegate = self
  53. if disableChangeExt {
  54. ext.isEnabled = false
  55. ext.textColor = .lightGray
  56. }
  57. previewFile.image = imagePreview
  58. previewFile.layer.cornerRadius = 10
  59. previewFile.layer.masksToBounds = true
  60. if metadata.directory {
  61. if imagePreview == nil {
  62. previewFile.image = NCCollectionCommon.images.cellFolderImage
  63. }
  64. ext.isHidden = true
  65. point.isHidden = true
  66. fileNameWithoutExtTrailingContraint.constant = 20
  67. } else {
  68. if imagePreview == nil {
  69. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  70. previewFile.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  71. } else {
  72. if metadata.iconName.count > 0 {
  73. previewFile.image = UIImage.init(named: metadata.iconName)
  74. } else {
  75. previewFile.image = NCCollectionCommon.images.cellFileImage
  76. }
  77. }
  78. }
  79. fileNameWithoutExtTrailingContraint.constant = 90
  80. }
  81. }
  82. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  83. cancelButton.setTitleColor(.gray, for: .normal)
  84. cancelButton.layer.cornerRadius = 15
  85. cancelButton.layer.masksToBounds = true
  86. cancelButton.layer.backgroundColor = NCBrandColor.shared.graySoft.withAlphaComponent(0.2).cgColor
  87. cancelButton.layer.borderWidth = 0.3
  88. cancelButton.layer.borderColor = UIColor.gray.cgColor
  89. renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
  90. renameButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  91. renameButton.layer.cornerRadius = 15
  92. renameButton.layer.masksToBounds = true
  93. renameButton.layer.backgroundColor = NCBrandColor.shared.brand.cgColor
  94. }
  95. override func viewWillAppear(_ animated: Bool) {
  96. super.viewWillAppear(animated)
  97. }
  98. override func viewDidAppear(_ animated: Bool) {
  99. super.viewDidAppear(animated)
  100. if metadata == nil {
  101. dismiss(animated: true)
  102. }
  103. fileNameWithoutExt.selectAll(nil)
  104. }
  105. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  106. textField.resignFirstResponder()
  107. rename(textField)
  108. return true
  109. }
  110. // MARK: - Action
  111. @IBAction func cancel(_ sender: Any) {
  112. dismiss(animated: true)
  113. }
  114. @IBAction func rename(_ sender: Any) {
  115. guard let metadata = metadata else { return }
  116. var fileNameWithoutExtNew = ""
  117. var extNew = ""
  118. var fileNameNew = ""
  119. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  120. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  121. return
  122. } else {
  123. fileNameWithoutExtNew = fileNameWithoutExt.text!
  124. }
  125. if metadata.directory {
  126. fileNameNew = fileNameWithoutExtNew
  127. renameMetadata(metadata, fileNameNew: fileNameNew)
  128. } else {
  129. if ext.text == nil || ext.text?.count == 0 {
  130. self.ext.text = metadata.ext
  131. return
  132. } else {
  133. extNew = ext.text!
  134. }
  135. if extNew != metadata.ext {
  136. let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
  137. let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
  138. var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
  139. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  140. fileNameNew = fileNameWithoutExtNew + "." + extNew
  141. self.renameMetadata(metadata, fileNameNew: fileNameNew)
  142. }))
  143. title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
  144. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  145. self.ext.text = metadata.ext
  146. }))
  147. self.present(alertController, animated: true)
  148. } else {
  149. fileNameNew = fileNameWithoutExtNew + "." + extNew
  150. renameMetadata(metadata, fileNameNew: fileNameNew)
  151. }
  152. }
  153. }
  154. // MARK: - Networking
  155. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  156. NCUtility.shared.startActivityIndicator(view: nil)
  157. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  158. NCUtility.shared.stopActivityIndicator()
  159. if errorCode == 0 {
  160. self.dismiss(animated: true)
  161. } else {
  162. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  163. }
  164. }
  165. }
  166. }