NCTrashListCell.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // NCTrashListCell.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 08/10/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. // Copyright © 2022 Henrik Storch. All rights reserved.
  8. //
  9. // Author Henrik Storch <henrik.storch@nextcloud.com>
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. class NCTrashListCell: UICollectionViewCell, NCTrashCell {
  27. @IBOutlet weak var imageItem: UIImageView!
  28. @IBOutlet weak var imageItemLeftConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var imageSelect: UIImageView!
  30. @IBOutlet weak var labelTitle: UILabel!
  31. @IBOutlet weak var labelInfo: UILabel!
  32. @IBOutlet weak var imageRestore: UIImageView!
  33. @IBOutlet weak var imageMore: UIImageView!
  34. @IBOutlet weak var buttonMore: UIButton!
  35. @IBOutlet weak var buttonRestore: UIButton!
  36. @IBOutlet weak var separator: UIView!
  37. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  38. weak var delegate: NCTrashListCellDelegate?
  39. var objectId = ""
  40. override func awakeFromNib() {
  41. super.awakeFromNib()
  42. isAccessibilityElement = true
  43. self.accessibilityCustomActions = [
  44. UIAccessibilityCustomAction(
  45. name: NSLocalizedString("_restore_", comment: ""),
  46. target: self,
  47. selector: #selector(touchUpInsideRestore)),
  48. UIAccessibilityCustomAction(
  49. name: NSLocalizedString("_delete_", comment: ""),
  50. target: self,
  51. selector: #selector(touchUpInsideMore))
  52. ]
  53. imageRestore.image = NCBrandColor.cacheImages.buttonRestore
  54. imageMore.image = NCBrandColor.cacheImages.buttonTrash
  55. imageItem.layer.cornerRadius = 6
  56. imageItem.layer.masksToBounds = true
  57. separator.backgroundColor = NCBrandColor.shared.separator
  58. separatorHeightConstraint.constant = 0.5
  59. }
  60. @IBAction func touchUpInsideMore(_ sender: Any) {
  61. delegate?.tapMoreListItem(with: objectId, image: imageItem.image, sender: sender)
  62. }
  63. @IBAction func touchUpInsideRestore(_ sender: Any) {
  64. delegate?.tapRestoreListItem(with: objectId, image: imageItem.image, sender: sender)
  65. }
  66. func selectMode(_ status: Bool) {
  67. if status {
  68. imageItemLeftConstraint.constant = 45
  69. imageSelect.isHidden = false
  70. } else {
  71. imageItemLeftConstraint.constant = 10
  72. imageSelect.isHidden = true
  73. backgroundView = nil
  74. }
  75. }
  76. func selected(_ status: Bool) {
  77. if status {
  78. var blurEffect: UIVisualEffect?
  79. var blurEffectView: UIView?
  80. imageSelect.image = NCBrandColor.cacheImages.checkedYes
  81. if traitCollection.userInterfaceStyle == .dark {
  82. blurEffect = UIBlurEffect(style: .dark)
  83. blurEffectView = UIVisualEffectView(effect: blurEffect)
  84. blurEffectView?.backgroundColor = .black
  85. } else {
  86. blurEffect = UIBlurEffect(style: .extraLight)
  87. blurEffectView = UIVisualEffectView(effect: blurEffect)
  88. blurEffectView?.backgroundColor = .lightGray
  89. }
  90. blurEffectView?.frame = self.bounds
  91. blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  92. backgroundView = blurEffectView
  93. separator.isHidden = true
  94. } else {
  95. imageSelect.image = NCBrandColor.cacheImages.checkedNo
  96. backgroundView = nil
  97. separator.isHidden = false
  98. }
  99. }
  100. }
  101. protocol NCTrashListCellDelegate: AnyObject {
  102. func tapRestoreListItem(with objectId: String, image: UIImage?, sender: Any)
  103. func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any)
  104. }
  105. protocol NCTrashCell {
  106. var objectId: String { get set }
  107. var labelTitle: UILabel! { get set }
  108. var labelInfo: UILabel! { get set }
  109. var imageItem: UIImageView! { get set }
  110. func selectMode(_ status: Bool)
  111. func selected(_ status: Bool)
  112. }
  113. extension NCTrashCell where Self: UICollectionViewCell {
  114. mutating func setupCellUI(tableTrash: tableTrash, image: UIImage?) {
  115. self.objectId = tableTrash.fileId
  116. self.labelTitle.text = tableTrash.trashbinFileName
  117. self.labelTitle.textColor = NCBrandColor.shared.label
  118. let infoText: String
  119. if tableTrash.directory {
  120. self.imageItem.image = NCBrandColor.cacheImages.folder
  121. infoText = CCUtility.dateDiff(tableTrash.date as Date)
  122. } else {
  123. self.imageItem.image = image
  124. infoText = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
  125. }
  126. self.labelInfo?.text = infoText
  127. self.accessibilityLabel = tableTrash.trashbinFileName + ", " + infoText
  128. }
  129. }