NCRenameFile.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 = NCBrandColor.cacheImages.folder
  63. }
  64. ext.isHidden = true
  65. point.isHidden = true
  66. fileNameWithoutExtTrailingContraint.constant = 20
  67. } else {
  68. if imagePreview == nil {
  69. previewFile.image = NCBrandColor.cacheImages.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. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  87. changeTheming()
  88. }
  89. override func viewWillAppear(_ animated: Bool) {
  90. super.viewWillAppear(animated)
  91. }
  92. override func viewDidAppear(_ animated: Bool) {
  93. super.viewDidAppear(animated)
  94. if metadata == nil {
  95. dismiss(animated: true)
  96. }
  97. fileNameWithoutExt.selectAll(nil)
  98. }
  99. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  100. textField.resignFirstResponder()
  101. rename(textField)
  102. return true
  103. }
  104. // MARK: - NotificationCenter
  105. @objc func changeTheming() {
  106. view.backgroundColor = NCBrandColor.shared.backgroundForm
  107. }
  108. // MARK: - Action
  109. @IBAction func cancel(_ sender: Any) {
  110. dismiss(animated: true)
  111. }
  112. @IBAction func rename(_ sender: Any) {
  113. guard let metadata = metadata else { return }
  114. var fileNameWithoutExtNew = ""
  115. var extNew = ""
  116. var fileNameNew = ""
  117. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  118. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  119. return
  120. } else {
  121. fileNameWithoutExtNew = fileNameWithoutExt.text!
  122. }
  123. if metadata.directory {
  124. fileNameNew = fileNameWithoutExtNew
  125. renameMetadata(metadata, fileNameNew: fileNameNew)
  126. } else {
  127. if ext.text == nil || ext.text?.count == 0 {
  128. self.ext.text = metadata.ext
  129. return
  130. } else {
  131. extNew = ext.text!
  132. }
  133. if extNew != metadata.ext {
  134. let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
  135. let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
  136. var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
  137. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  138. fileNameNew = fileNameWithoutExtNew + "." + extNew
  139. self.renameMetadata(metadata, fileNameNew: fileNameNew)
  140. }))
  141. title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
  142. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  143. self.ext.text = metadata.ext
  144. }))
  145. self.present(alertController, animated: true)
  146. } else {
  147. fileNameNew = fileNameWithoutExtNew + "." + extNew
  148. renameMetadata(metadata, fileNameNew: fileNameNew)
  149. }
  150. }
  151. }
  152. // MARK: - Networking
  153. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  154. NCUtility.shared.startActivityIndicator(backgroundView: nil, blurEffect: true)
  155. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  156. NCUtility.shared.stopActivityIndicator()
  157. if errorCode == 0 {
  158. self.dismiss(animated: true)
  159. } else {
  160. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  161. }
  162. }
  163. }
  164. }