NCTrashListCell.swift 6.2 KB

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