NCRenameFile.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. import Foundation
  9. import NCCommunication
  10. class NCRenameFile: UIViewController {
  11. @IBOutlet weak var titleLabel: UILabel!
  12. @IBOutlet weak var separatorHeightContraint: NSLayoutConstraint!
  13. @IBOutlet weak var previewFile: UIImageView!
  14. @IBOutlet weak var fileNameWithoutExt: UITextField!
  15. @IBOutlet weak var point: UILabel!
  16. @IBOutlet weak var ext: UITextField!
  17. @IBOutlet weak var fileNameWithoutExtTrailingContraint: NSLayoutConstraint!
  18. @IBOutlet weak var cancelButton: UIButton!
  19. @IBOutlet weak var renameButton: UIButton!
  20. var metadata: tableMetadata?
  21. // MARK: - Life Cycle
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. if let metadata = self.metadata {
  25. fileNameWithoutExt.text = metadata.fileNameWithoutExt
  26. ext.text = metadata.ext
  27. if metadata.directory {
  28. previewFile.image = NCCollectionCommon.images.cellFolderImage
  29. ext.isHidden = true
  30. point.isHidden = true
  31. fileNameWithoutExtTrailingContraint.constant = 20
  32. } else {
  33. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  34. previewFile.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  35. } else {
  36. if metadata.iconName.count > 0 {
  37. previewFile.image = UIImage.init(named: metadata.iconName)
  38. } else {
  39. previewFile.image = NCCollectionCommon.images.cellFileImage
  40. }
  41. }
  42. fileNameWithoutExtTrailingContraint.constant = 90
  43. }
  44. }
  45. titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
  46. separatorHeightContraint.constant = 0.3
  47. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  48. cancelButton.setTitleColor(.white, for: .normal)
  49. cancelButton.layer.cornerRadius = 15
  50. cancelButton.layer.masksToBounds = true
  51. cancelButton.layer.backgroundColor = UIColor.gray.cgColor
  52. /*
  53. cell.primary.isEnabled = false
  54. cell.primary.isHidden = true
  55. cell.primary.titleLabel?.font = .systemFont(ofSize: 14)
  56. cell.primary.setTitleColor(.white, for: .normal)
  57. cell.primary.layer.cornerRadius = 15
  58. cell.primary.layer.masksToBounds = true
  59. cell.primary.layer.backgroundColor = NCBrandColor.shared.brandElement.cgColor
  60. cell.secondary.isEnabled = false
  61. cell.secondary.isHidden = true
  62. cell.secondary.titleLabel?.font = .systemFont(ofSize: 14)
  63. cell.secondary.setTitleColor(.gray, for: .normal)
  64. cell.secondary.layer.cornerRadius = 15
  65. cell.secondary.layer.masksToBounds = true
  66. cell.secondary.layer.backgroundColor = NCBrandColor.shared.graySoft.withAlphaComponent(0.1).cgColor
  67. cell.secondary.layer.borderWidth = 0.3
  68. cell.secondary.layer.borderColor = UIColor.gray.cgColor
  69. */
  70. }
  71. override func viewDidAppear(_ animated: Bool) {
  72. super.viewDidAppear(animated)
  73. if metadata == nil {
  74. dismiss(animated: true)
  75. }
  76. }
  77. // MARK: - Action
  78. @IBAction func cancel(_ sender: Any) {
  79. dismiss(animated: true)
  80. }
  81. @IBAction func rename(_ sender: Any) {
  82. guard let metadata = metadata else { return }
  83. var newFileNameWithoutExt = ""
  84. var newExt = ""
  85. var fileNameNew = ""
  86. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  87. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  88. return
  89. } else {
  90. newFileNameWithoutExt = fileNameWithoutExt.text!
  91. }
  92. if metadata.directory {
  93. fileNameNew = newFileNameWithoutExt
  94. renameMetadata(metadata, fileNameNew: fileNameNew)
  95. } else {
  96. if ext.text == nil || ext.text?.count == 0 {
  97. self.ext.text = metadata.ext
  98. return
  99. } else {
  100. newExt = ext.text!
  101. }
  102. if newExt != metadata.ext {
  103. } else {
  104. fileNameNew = newFileNameWithoutExt + "." + newExt
  105. renameMetadata(metadata, fileNameNew: fileNameNew)
  106. }
  107. }
  108. }
  109. // MARK: - Networking
  110. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  111. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  112. if errorCode == 0 {
  113. self.dismiss(animated: true)
  114. } else {
  115. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  116. }
  117. }
  118. }
  119. }