NCTrashGridCell.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // NCTrashGridCell.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/03/2024.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@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. protocol NCTrashGridCellDelegate: AnyObject {
  25. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, indexPath: IndexPath, sender: Any)
  26. }
  27. class NCTrashGridCell: UICollectionViewCell, NCTrashCellProtocol {
  28. @IBOutlet weak var imageItem: UIImageView!
  29. @IBOutlet weak var imageSelect: UIImageView!
  30. @IBOutlet weak var labelTitle: UILabel!
  31. @IBOutlet weak var labelInfo: UILabel!
  32. @IBOutlet weak var labelSubinfo: UILabel!
  33. @IBOutlet weak var buttonMore: UIButton!
  34. @IBOutlet weak var imageVisualEffect: UIVisualEffectView!
  35. weak var delegate: NCTrashGridCellDelegate?
  36. var objectId = ""
  37. var indexPath = IndexPath()
  38. var user = ""
  39. var namedButtonMore = ""
  40. override func awakeFromNib() {
  41. super.awakeFromNib()
  42. // use entire cell as accessibility element
  43. accessibilityHint = nil
  44. accessibilityLabel = nil
  45. accessibilityValue = nil
  46. isAccessibilityElement = true
  47. imageItem.layer.cornerRadius = 6
  48. imageItem.layer.masksToBounds = true
  49. imageVisualEffect.layer.cornerRadius = 6
  50. imageVisualEffect.clipsToBounds = true
  51. imageVisualEffect.alpha = 0.5
  52. labelTitle.text = ""
  53. labelInfo.text = ""
  54. labelSubinfo.text = ""
  55. }
  56. override func prepareForReuse() {
  57. super.prepareForReuse()
  58. imageItem.backgroundColor = nil
  59. accessibilityHint = nil
  60. accessibilityLabel = nil
  61. accessibilityValue = nil
  62. }
  63. override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
  64. return nil
  65. }
  66. @IBAction func touchUpInsideMore(_ sender: Any) {
  67. delegate?.tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, image: imageItem.image, indexPath: indexPath, sender: sender)
  68. }
  69. fileprivate func setA11yActions() {
  70. let moreName = namedButtonMore == NCGlobal.shared.buttonMoreStop ? "_cancel_" : "_more_"
  71. self.accessibilityCustomActions = [
  72. UIAccessibilityCustomAction(
  73. name: NSLocalizedString(moreName, comment: ""),
  74. target: self,
  75. selector: #selector(touchUpInsideMore))
  76. ]
  77. }
  78. func setButtonMore(named: String, image: UIImage) {
  79. namedButtonMore = named
  80. buttonMore.setImage(image, for: .normal)
  81. setA11yActions()
  82. }
  83. func hideButtonMore(_ status: Bool) {
  84. buttonMore.isHidden = status
  85. }
  86. func selectMode(_ status: Bool) {
  87. if status {
  88. imageSelect.isHidden = false
  89. buttonMore.isHidden = true
  90. accessibilityCustomActions = nil
  91. } else {
  92. imageSelect.isHidden = true
  93. imageVisualEffect.isHidden = true
  94. buttonMore.isHidden = false
  95. setA11yActions()
  96. }
  97. }
  98. func selected(_ status: Bool) {
  99. if status {
  100. if traitCollection.userInterfaceStyle == .dark {
  101. imageVisualEffect.effect = UIBlurEffect(style: .dark)
  102. imageVisualEffect.backgroundColor = .black
  103. } else {
  104. imageVisualEffect.effect = UIBlurEffect(style: .extraLight)
  105. imageVisualEffect.backgroundColor = .lightGray
  106. }
  107. imageSelect.image = NCImageCache.images.checkedYes
  108. imageVisualEffect.isHidden = false
  109. } else {
  110. imageSelect.isHidden = true
  111. imageVisualEffect.isHidden = true
  112. }
  113. }
  114. func writeInfoDateSize(date: NSDate, size: Int64) {
  115. let dateFormatter = DateFormatter()
  116. dateFormatter.dateStyle = .short
  117. dateFormatter.timeStyle = .none
  118. dateFormatter.locale = Locale.current
  119. labelInfo.text = dateFormatter.string(from: date as Date)
  120. labelSubinfo.text = NCUtilityFileSystem().transformedSize(size)
  121. }
  122. func setAccessibility(label: String, value: String) {
  123. accessibilityLabel = label
  124. accessibilityValue = value
  125. }
  126. }
  127. // MARK: - Grid Layout
  128. class NCTrashGridLayout: UICollectionViewFlowLayout {
  129. var heightLabelPlusButton: CGFloat = 60
  130. var marginLeftRight: CGFloat = 10
  131. var itemForLine: CGFloat = 3
  132. var itemWidthDefault: CGFloat = 140
  133. // MARK: - View Life Cycle
  134. override init() {
  135. super.init()
  136. sectionHeadersPinToVisibleBounds = false
  137. minimumInteritemSpacing = 1
  138. minimumLineSpacing = marginLeftRight
  139. self.scrollDirection = .vertical
  140. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  141. }
  142. required init?(coder aDecoder: NSCoder) {
  143. fatalError("init(coder:) has not been implemented")
  144. }
  145. override var itemSize: CGSize {
  146. get {
  147. if let collectionView = collectionView {
  148. if collectionView.frame.width < 400 {
  149. itemForLine = 3
  150. } else {
  151. itemForLine = collectionView.frame.width / itemWidthDefault
  152. }
  153. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  154. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  155. return CGSize(width: itemWidth, height: itemHeight)
  156. }
  157. // Default fallback
  158. return CGSize(width: itemWidthDefault, height: itemWidthDefault)
  159. }
  160. set {
  161. super.itemSize = newValue
  162. }
  163. }
  164. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  165. return proposedContentOffset
  166. }
  167. }