NCShareCell.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 renameFile(named fileName: String)
  29. }
  30. class NCShareCell: UITableViewCell {
  31. @IBOutlet weak var imageCell: UIImageView!
  32. @IBOutlet weak var fileNameCell: UILabel!
  33. @IBOutlet weak var moreButton: UIButton!
  34. @IBOutlet weak var sizeCell: UILabel!
  35. weak var delegate: (NCShareCellDelegate & UIViewController)?
  36. var fileName = ""
  37. func setup(fileName: String) {
  38. self.fileName = fileName
  39. let resultInternalType = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false)
  40. backgroundColor = .systemBackground
  41. imageCell?.layer.cornerRadius = 6
  42. imageCell?.layer.masksToBounds = true
  43. if let image = UIImage.downsample(imageAt: URL(fileURLWithPath: NSTemporaryDirectory() + fileName), to: CGSize(width: 80, height: 80)) {
  44. imageCell.image = image
  45. } else {
  46. if !resultInternalType.iconName.isEmpty {
  47. imageCell?.image = UIImage(named: resultInternalType.iconName)
  48. } else {
  49. imageCell?.image = NCBrandColor.cacheImages.file
  50. }
  51. }
  52. fileNameCell?.text = fileName
  53. let fileSize = NCUtilityFileSystem.shared.getFileSize(filePath: (NSTemporaryDirectory() + fileName))
  54. sizeCell?.text = CCUtility.transformedSize(fileSize)
  55. moreButton?.setImage(NCUtility.shared.loadImage(named: "more").image(color: .label, size: 15), for: .normal)
  56. }
  57. @IBAction func buttonTapped(_ sender: Any) {
  58. guard !fileName.isEmpty, delegate?.uploadStarted != true else { return }
  59. let alertController = UIAlertController(title: "", message: fileName, preferredStyle: .alert)
  60. alertController.addAction(UIAlertAction(title: NSLocalizedString("_rename_file_", comment: ""), style: .default) { _ in
  61. self.delegate?.renameFile(named: self.fileName)
  62. })
  63. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_file_", comment: ""), style: .default) { _ in
  64. self.delegate?.removeFile(named: self.fileName)
  65. })
  66. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  67. delegate?.present(alertController, animated: true, completion: nil)
  68. }
  69. }