NCTrashGridCell.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, image: UIImage?, 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 account = ""
  39. var user = ""
  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, image: imageItem.image, sender: sender)
  67. }
  68. fileprivate func setA11yActions() {
  69. self.accessibilityCustomActions = [
  70. UIAccessibilityCustomAction(
  71. name: NSLocalizedString("_more_", comment: ""),
  72. target: self,
  73. selector: #selector(touchUpInsideMore))
  74. ]
  75. }
  76. func setButtonMore(image: UIImage) {
  77. buttonMore.setImage(image, for: .normal)
  78. setA11yActions()
  79. }
  80. func hideButtonMore(_ status: Bool) {
  81. buttonMore.isHidden = status
  82. }
  83. func selected(_ status: Bool, isEditMode: Bool, account: String) {
  84. if isEditMode {
  85. buttonMore.isHidden = true
  86. accessibilityCustomActions = nil
  87. } else {
  88. buttonMore.isHidden = false
  89. setA11yActions()
  90. }
  91. if status {
  92. imageSelect.image = NCImageCache.shared.getImageCheckedYes()
  93. imageSelect.isHidden = false
  94. imageVisualEffect.isHidden = false
  95. } else {
  96. imageSelect.isHidden = true
  97. imageVisualEffect.isHidden = true
  98. }
  99. }
  100. func writeInfoDateSize(date: NSDate, size: Int64) {
  101. let dateFormatter = DateFormatter()
  102. dateFormatter.dateStyle = .short
  103. dateFormatter.timeStyle = .none
  104. dateFormatter.locale = Locale.current
  105. labelInfo.text = dateFormatter.string(from: date as Date)
  106. labelSubinfo.text = NCUtilityFileSystem().transformedSize(size)
  107. }
  108. func setAccessibility(label: String, value: String) {
  109. accessibilityLabel = label
  110. accessibilityValue = value
  111. }
  112. }
  113. // MARK: - Grid Layout
  114. class NCTrashGridLayout: UICollectionViewFlowLayout {
  115. var heightLabelPlusButton: CGFloat = 60
  116. var marginLeftRight: CGFloat = 10
  117. var itemForLine: CGFloat = 3
  118. var itemWidthDefault: CGFloat = 140
  119. // MARK: - View Life Cycle
  120. override init() {
  121. super.init()
  122. sectionHeadersPinToVisibleBounds = false
  123. minimumInteritemSpacing = 1
  124. minimumLineSpacing = marginLeftRight
  125. self.scrollDirection = .vertical
  126. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  127. }
  128. required init?(coder aDecoder: NSCoder) {
  129. fatalError("init(coder:) has not been implemented")
  130. }
  131. override var itemSize: CGSize {
  132. get {
  133. if let collectionView = collectionView {
  134. if collectionView.frame.width < 400 {
  135. itemForLine = 3
  136. } else {
  137. itemForLine = collectionView.frame.width / itemWidthDefault
  138. }
  139. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  140. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  141. return CGSize(width: itemWidth, height: itemHeight)
  142. }
  143. // Default fallback
  144. return CGSize(width: itemWidthDefault, height: itemWidthDefault)
  145. }
  146. set {
  147. super.itemSize = newValue
  148. }
  149. }
  150. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  151. return proposedContentOffset
  152. }
  153. }