NCTrashListCell+NCTrashCellProtocol.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, NCTrashCellProtocol {
  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 = .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. imageRestore.isHidden = true
  71. buttonRestore.isHidden = true
  72. imageMore.isHidden = true
  73. buttonMore.isHidden = true
  74. } else {
  75. imageItemLeftConstraint.constant = 10
  76. imageSelect.isHidden = true
  77. imageRestore.isHidden = false
  78. buttonRestore.isHidden = false
  79. imageMore.isHidden = false
  80. buttonMore.isHidden = false
  81. backgroundView = nil
  82. }
  83. }
  84. func selected(_ status: Bool) {
  85. if status {
  86. var blurEffect: UIVisualEffect?
  87. var blurEffectView: UIView?
  88. imageSelect.image = NCBrandColor.cacheImages.checkedYes
  89. if traitCollection.userInterfaceStyle == .dark {
  90. blurEffect = UIBlurEffect(style: .dark)
  91. blurEffectView = UIVisualEffectView(effect: blurEffect)
  92. blurEffectView?.backgroundColor = .black
  93. } else {
  94. blurEffect = UIBlurEffect(style: .extraLight)
  95. blurEffectView = UIVisualEffectView(effect: blurEffect)
  96. blurEffectView?.backgroundColor = .lightGray
  97. }
  98. blurEffectView?.frame = self.bounds
  99. blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  100. backgroundView = blurEffectView
  101. separator.isHidden = true
  102. } else {
  103. imageSelect.image = NCBrandColor.cacheImages.checkedNo
  104. backgroundView = nil
  105. separator.isHidden = false
  106. }
  107. }
  108. }
  109. protocol NCTrashListCellDelegate: AnyObject {
  110. func tapRestoreListItem(with objectId: String, image: UIImage?, sender: Any)
  111. func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any)
  112. }
  113. protocol NCTrashCellProtocol {
  114. var objectId: String { get set }
  115. var labelTitle: UILabel! { get set }
  116. var labelInfo: UILabel! { get set }
  117. var imageItem: UIImageView! { get set }
  118. func selectMode(_ status: Bool)
  119. func selected(_ status: Bool)
  120. }
  121. extension NCTrashCellProtocol where Self: UICollectionViewCell {
  122. mutating func setupCellUI(tableTrash: tableTrash, image: UIImage?) {
  123. self.objectId = tableTrash.fileId
  124. self.labelTitle.text = tableTrash.trashbinFileName
  125. self.labelTitle.textColor = .label
  126. if self is NCTrashListCell {
  127. self.labelInfo?.text = CCUtility.dateDiff(tableTrash.date as Date)
  128. } else {
  129. let dateFormatter = DateFormatter()
  130. dateFormatter.dateStyle = .short
  131. dateFormatter.timeStyle = .none
  132. dateFormatter.locale = Locale.current
  133. self.labelInfo?.text = dateFormatter.string(from: tableTrash.date as Date)
  134. }
  135. if tableTrash.directory {
  136. self.imageItem.image = NCBrandColor.cacheImages.folder
  137. } else {
  138. self.imageItem.image = image
  139. self.labelInfo?.text = (self.labelInfo?.text ?? "") + " · " + CCUtility.transformedSize(tableTrash.size)
  140. }
  141. self.accessibilityLabel = tableTrash.trashbinFileName + ", " + (self.labelInfo?.text ?? "")
  142. }
  143. }