NCRenameFile.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.folder
  63. }
  64. ext.isHidden = true
  65. point.isHidden = true
  66. fileNameWithoutExtTrailingContraint.constant = 20
  67. } else {
  68. if imagePreview == nil {
  69. previewFile.image = NCCollectionCommon.images.file
  70. }
  71. fileNameWithoutExtTrailingContraint.constant = 90
  72. }
  73. }
  74. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  75. cancelButton.setTitleColor(.gray, for: .normal)
  76. cancelButton.layer.cornerRadius = 15
  77. cancelButton.layer.masksToBounds = true
  78. cancelButton.layer.backgroundColor = NCBrandColor.shared.graySoft.withAlphaComponent(0.2).cgColor
  79. cancelButton.layer.borderWidth = 0.3
  80. cancelButton.layer.borderColor = UIColor.gray.cgColor
  81. renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
  82. renameButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  83. renameButton.layer.cornerRadius = 15
  84. renameButton.layer.masksToBounds = true
  85. renameButton.layer.backgroundColor = NCBrandColor.shared.brand.cgColor
  86. }
  87. override func viewWillAppear(_ animated: Bool) {
  88. super.viewWillAppear(animated)
  89. }
  90. override func viewDidAppear(_ animated: Bool) {
  91. super.viewDidAppear(animated)
  92. if metadata == nil {
  93. dismiss(animated: true)
  94. }
  95. fileNameWithoutExt.selectAll(nil)
  96. }
  97. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  98. textField.resignFirstResponder()
  99. rename(textField)
  100. return true
  101. }
  102. // MARK: - Action
  103. @IBAction func cancel(_ sender: Any) {
  104. dismiss(animated: true)
  105. }
  106. @IBAction func rename(_ sender: Any) {
  107. guard let metadata = metadata else { return }
  108. var fileNameWithoutExtNew = ""
  109. var extNew = ""
  110. var fileNameNew = ""
  111. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  112. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  113. return
  114. } else {
  115. fileNameWithoutExtNew = fileNameWithoutExt.text!
  116. }
  117. if metadata.directory {
  118. fileNameNew = fileNameWithoutExtNew
  119. renameMetadata(metadata, fileNameNew: fileNameNew)
  120. } else {
  121. if ext.text == nil || ext.text?.count == 0 {
  122. self.ext.text = metadata.ext
  123. return
  124. } else {
  125. extNew = ext.text!
  126. }
  127. if extNew != metadata.ext {
  128. let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
  129. let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
  130. var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
  131. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  132. fileNameNew = fileNameWithoutExtNew + "." + extNew
  133. self.renameMetadata(metadata, fileNameNew: fileNameNew)
  134. }))
  135. title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
  136. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  137. self.ext.text = metadata.ext
  138. }))
  139. self.present(alertController, animated: true)
  140. } else {
  141. fileNameNew = fileNameWithoutExtNew + "." + extNew
  142. renameMetadata(metadata, fileNameNew: fileNameNew)
  143. }
  144. }
  145. }
  146. // MARK: - Networking
  147. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  148. NCUtility.shared.startActivityIndicator(view: nil)
  149. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  150. NCUtility.shared.stopActivityIndicator()
  151. if errorCode == 0 {
  152. self.dismiss(animated: true)
  153. } else {
  154. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  155. }
  156. }
  157. }
  158. }