NCTrashGridCell.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. initCell()
  43. }
  44. override func prepareForReuse() {
  45. super.prepareForReuse()
  46. initCell()
  47. }
  48. func initCell() {
  49. accessibilityHint = nil
  50. accessibilityLabel = nil
  51. accessibilityValue = nil
  52. isAccessibilityElement = true
  53. imageItem.layer.cornerRadius = 6
  54. imageItem.layer.masksToBounds = true
  55. imageVisualEffect.layer.cornerRadius = 6
  56. imageVisualEffect.clipsToBounds = true
  57. imageVisualEffect.alpha = 0.5
  58. labelTitle.text = ""
  59. labelInfo.text = ""
  60. labelSubinfo.text = ""
  61. }
  62. override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
  63. return nil
  64. }
  65. @IBAction func touchUpInsideMore(_ sender: Any) {
  66. delegate?.tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, image: imageItem.image, indexPath: indexPath, sender: sender)
  67. }
  68. fileprivate func setA11yActions() {
  69. let moreName = namedButtonMore == NCGlobal.shared.buttonMoreStop ? "_cancel_" : "_more_"
  70. self.accessibilityCustomActions = [
  71. UIAccessibilityCustomAction(
  72. name: NSLocalizedString(moreName, comment: ""),
  73. target: self,
  74. selector: #selector(touchUpInsideMore))
  75. ]
  76. }
  77. func setButtonMore(named: String, image: UIImage) {
  78. namedButtonMore = named
  79. buttonMore.setImage(image, for: .normal)
  80. setA11yActions()
  81. }
  82. func hideButtonMore(_ status: Bool) {
  83. buttonMore.isHidden = status
  84. }
  85. func selected(_ status: Bool, isEditMode: Bool) {
  86. if isEditMode {
  87. buttonMore.isHidden = true
  88. accessibilityCustomActions = nil
  89. } else {
  90. buttonMore.isHidden = false
  91. setA11yActions()
  92. }
  93. if status {
  94. imageSelect.image = NCImageCache.images.checkedYes
  95. imageSelect.isHidden = false
  96. imageVisualEffect.isHidden = false
  97. } else {
  98. imageSelect.isHidden = true
  99. imageVisualEffect.isHidden = true
  100. }
  101. }
  102. func writeInfoDateSize(date: NSDate, size: Int64) {
  103. let dateFormatter = DateFormatter()
  104. dateFormatter.dateStyle = .short
  105. dateFormatter.timeStyle = .none
  106. dateFormatter.locale = Locale.current
  107. labelInfo.text = dateFormatter.string(from: date as Date)
  108. labelSubinfo.text = NCUtilityFileSystem().transformedSize(size)
  109. }
  110. func setAccessibility(label: String, value: String) {
  111. accessibilityLabel = label
  112. accessibilityValue = value
  113. }
  114. }
  115. // MARK: - Grid Layout
  116. class NCTrashGridLayout: UICollectionViewFlowLayout {
  117. var heightLabelPlusButton: CGFloat = 60
  118. var marginLeftRight: CGFloat = 10
  119. var itemForLine: CGFloat = 3
  120. var itemWidthDefault: CGFloat = 140
  121. // MARK: - View Life Cycle
  122. override init() {
  123. super.init()
  124. sectionHeadersPinToVisibleBounds = false
  125. minimumInteritemSpacing = 1
  126. minimumLineSpacing = marginLeftRight
  127. self.scrollDirection = .vertical
  128. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  129. }
  130. required init?(coder aDecoder: NSCoder) {
  131. fatalError("init(coder:) has not been implemented")
  132. }
  133. override var itemSize: CGSize {
  134. get {
  135. if let collectionView = collectionView {
  136. if collectionView.frame.width < 400 {
  137. itemForLine = 3
  138. } else {
  139. itemForLine = collectionView.frame.width / itemWidthDefault
  140. }
  141. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  142. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  143. return CGSize(width: itemWidth, height: itemHeight)
  144. }
  145. // Default fallback
  146. return CGSize(width: itemWidthDefault, height: itemWidthDefault)
  147. }
  148. set {
  149. super.itemSize = newValue
  150. }
  151. }
  152. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  153. return proposedContentOffset
  154. }
  155. }