NCTrashListCell+NCTrashCellProtocol.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. var indexPath = IndexPath()
  41. override func awakeFromNib() {
  42. super.awakeFromNib()
  43. isAccessibilityElement = true
  44. self.accessibilityCustomActions = [
  45. UIAccessibilityCustomAction(
  46. name: NSLocalizedString("_restore_", comment: ""),
  47. target: self,
  48. selector: #selector(touchUpInsideRestore)),
  49. UIAccessibilityCustomAction(
  50. name: NSLocalizedString("_delete_", comment: ""),
  51. target: self,
  52. selector: #selector(touchUpInsideMore))
  53. ]
  54. imageRestore.image = NCBrandColor.cacheImages.buttonRestore
  55. imageMore.image = NCBrandColor.cacheImages.buttonTrash
  56. imageItem.layer.cornerRadius = 6
  57. imageItem.layer.masksToBounds = true
  58. separator.backgroundColor = .separator
  59. separatorHeightConstraint.constant = 0.5
  60. }
  61. @IBAction func touchUpInsideMore(_ sender: Any) {
  62. delegate?.tapMoreListItem(with: objectId, image: imageItem.image, sender: sender)
  63. }
  64. @IBAction func touchUpInsideRestore(_ sender: Any) {
  65. delegate?.tapRestoreListItem(with: objectId, image: imageItem.image, sender: sender)
  66. }
  67. func selectMode(_ status: Bool) {
  68. if status {
  69. imageItemLeftConstraint.constant = 45
  70. imageSelect.isHidden = false
  71. imageRestore.isHidden = true
  72. buttonRestore.isHidden = true
  73. imageMore.isHidden = true
  74. buttonMore.isHidden = true
  75. } else {
  76. imageItemLeftConstraint.constant = 10
  77. imageSelect.isHidden = true
  78. imageRestore.isHidden = false
  79. buttonRestore.isHidden = false
  80. imageMore.isHidden = false
  81. buttonMore.isHidden = false
  82. backgroundView = nil
  83. }
  84. }
  85. func selected(_ status: Bool) {
  86. if status {
  87. var blurEffect: UIVisualEffect?
  88. var blurEffectView: UIView?
  89. imageSelect.image = NCBrandColor.cacheImages.checkedYes
  90. if traitCollection.userInterfaceStyle == .dark {
  91. blurEffect = UIBlurEffect(style: .dark)
  92. blurEffectView = UIVisualEffectView(effect: blurEffect)
  93. blurEffectView?.backgroundColor = .black
  94. } else {
  95. blurEffect = UIBlurEffect(style: .extraLight)
  96. blurEffectView = UIVisualEffectView(effect: blurEffect)
  97. blurEffectView?.backgroundColor = .lightGray
  98. }
  99. blurEffectView?.frame = self.bounds
  100. blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  101. backgroundView = blurEffectView
  102. separator.isHidden = true
  103. } else {
  104. imageSelect.image = NCBrandColor.cacheImages.checkedNo
  105. backgroundView = nil
  106. separator.isHidden = false
  107. }
  108. }
  109. }
  110. protocol NCTrashListCellDelegate: AnyObject {
  111. func tapRestoreListItem(with objectId: String, image: UIImage?, sender: Any)
  112. func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any)
  113. }
  114. protocol NCTrashCellProtocol {
  115. var objectId: String { get set }
  116. var labelTitle: UILabel! { get set }
  117. var labelInfo: UILabel! { get set }
  118. var imageItem: UIImageView! { get set }
  119. var indexPath: IndexPath { get set }
  120. func selectMode(_ status: Bool)
  121. func selected(_ status: Bool)
  122. }
  123. extension NCTrashCellProtocol where Self: UICollectionViewCell {
  124. mutating func setupCellUI(tableTrash: tableTrash, image: UIImage?) {
  125. self.objectId = tableTrash.fileId
  126. self.labelTitle.text = tableTrash.trashbinFileName
  127. self.labelTitle.textColor = .label
  128. if self is NCTrashListCell {
  129. self.labelInfo?.text = CCUtility.dateDiff(tableTrash.date as Date)
  130. } else {
  131. let dateFormatter = DateFormatter()
  132. dateFormatter.dateStyle = .short
  133. dateFormatter.timeStyle = .none
  134. dateFormatter.locale = Locale.current
  135. self.labelInfo?.text = dateFormatter.string(from: tableTrash.date as Date)
  136. }
  137. if tableTrash.directory {
  138. self.imageItem.image = NCBrandColor.cacheImages.folder
  139. } else {
  140. self.imageItem.image = image
  141. self.labelInfo?.text = (self.labelInfo?.text ?? "") + " · " + CCUtility.transformedSize(tableTrash.size)
  142. }
  143. self.accessibilityLabel = tableTrash.trashbinFileName + ", " + (self.labelInfo?.text ?? "")
  144. }
  145. }