NCRenameFile.swift 7.7 KB

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