NCRenameFile.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(.gray, for: .normal)
  49. cancelButton.layer.cornerRadius = 15
  50. cancelButton.layer.masksToBounds = true
  51. cancelButton.layer.backgroundColor = NCBrandColor.shared.graySoft.withAlphaComponent(0.3).cgColor
  52. cancelButton.layer.borderWidth = 0.3
  53. cancelButton.layer.borderColor = UIColor.gray.cgColor
  54. renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
  55. renameButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  56. renameButton.layer.cornerRadius = 15
  57. renameButton.layer.masksToBounds = true
  58. renameButton.layer.backgroundColor = NCBrandColor.shared.brand.cgColor
  59. }
  60. override func viewDidAppear(_ animated: Bool) {
  61. super.viewDidAppear(animated)
  62. if metadata == nil {
  63. dismiss(animated: true)
  64. }
  65. }
  66. // MARK: - Action
  67. @IBAction func cancel(_ sender: Any) {
  68. dismiss(animated: true)
  69. }
  70. @IBAction func rename(_ sender: Any) {
  71. guard let metadata = metadata else { return }
  72. var newFileNameWithoutExt = ""
  73. var newExt = ""
  74. var fileNameNew = ""
  75. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  76. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  77. return
  78. } else {
  79. newFileNameWithoutExt = fileNameWithoutExt.text!
  80. }
  81. if metadata.directory {
  82. fileNameNew = newFileNameWithoutExt
  83. renameMetadata(metadata, fileNameNew: fileNameNew)
  84. } else {
  85. if ext.text == nil || ext.text?.count == 0 {
  86. self.ext.text = metadata.ext
  87. return
  88. } else {
  89. newExt = ext.text!
  90. }
  91. if newExt != metadata.ext {
  92. } else {
  93. fileNameNew = newFileNameWithoutExt + "." + newExt
  94. renameMetadata(metadata, fileNameNew: fileNameNew)
  95. }
  96. }
  97. }
  98. // MARK: - Networking
  99. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  100. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  101. if errorCode == 0 {
  102. self.dismiss(animated: true)
  103. } else {
  104. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  105. }
  106. }
  107. }
  108. }