NCShareCell.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // NCShareCell.swift
  3. // Share
  4. //
  5. // Created by Henrik Storch on 29.12.21.
  6. // Copyright © 2021 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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 UIKit
  24. import NextcloudKit
  25. protocol NCShareCellDelegate: AnyObject {
  26. var uploadStarted: Bool { get }
  27. func removeFile(named fileName: String)
  28. func showRenameFileDialog(named fileName: String, account: String)
  29. func renameFile(oldName: String, newName: String, account: String)
  30. }
  31. class NCShareCell: UITableViewCell {
  32. @IBOutlet weak var imageCell: UIImageView!
  33. @IBOutlet weak var fileNameCell: UILabel!
  34. @IBOutlet weak var moreButton: UIButton!
  35. @IBOutlet weak var sizeCell: UILabel!
  36. weak var delegate: (NCShareCellDelegate & UIViewController)?
  37. var fileName: String = ""
  38. var account: String = ""
  39. let utilityFileSystem = NCUtilityFileSystem()
  40. let utility = NCUtility()
  41. func setup(fileName: String, account: String) {
  42. self.fileName = fileName
  43. self.account = account
  44. let resultInternalType = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false, account: account)
  45. backgroundColor = .systemBackground
  46. imageCell?.layer.cornerRadius = 6
  47. imageCell?.layer.masksToBounds = true
  48. if let image = UIImage.downsample(imageAt: URL(fileURLWithPath: NSTemporaryDirectory() + fileName), to: CGSize(width: 80, height: 80)) {
  49. imageCell.image = image
  50. imageCell.contentMode = .scaleAspectFill
  51. } else {
  52. imageCell.image = utility.loadImage(named: resultInternalType.iconName, useTypeIconFile: true, account: account)
  53. imageCell.contentMode = .scaleAspectFit
  54. }
  55. fileNameCell?.text = fileName
  56. let fileSize = utilityFileSystem.getFileSize(filePath: (NSTemporaryDirectory() + fileName))
  57. sizeCell?.text = utilityFileSystem.transformedSize(fileSize)
  58. moreButton?.setImage(NCImageCache.shared.getImageButtonMore(), for: .normal)
  59. }
  60. @IBAction func buttonTapped(_ sender: Any) {
  61. guard !fileName.isEmpty, delegate?.uploadStarted != true else { return }
  62. let alertController = UIAlertController(title: "", message: fileName, preferredStyle: .alert)
  63. alertController.addAction(UIAlertAction(title: NSLocalizedString("_rename_file_", comment: ""), style: .default) { _ in
  64. self.delegate?.showRenameFileDialog(named: self.fileName, account: self.account)
  65. })
  66. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_file_", comment: ""), style: .default) { _ in
  67. self.delegate?.removeFile(named: self.fileName)
  68. })
  69. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  70. delegate?.present(alertController, animated: true, completion: nil)
  71. }
  72. }