NCGridCell.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // NCGridCell.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 08/10/2018.
  6. // Copyright © 2018 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. class NCGridCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellProtocol, NCTrashCell {
  25. var labelInfo: UILabel?
  26. @IBOutlet weak var imageItem: UIImageView!
  27. @IBOutlet weak var imageSelect: UIImageView!
  28. @IBOutlet weak var imageStatus: UIImageView!
  29. @IBOutlet weak var imageFavorite: UIImageView!
  30. @IBOutlet weak var imageLocal: UIImageView!
  31. @IBOutlet weak var labelTitle: UILabel!
  32. @IBOutlet weak var buttonMore: UIButton!
  33. @IBOutlet weak var imageVisualEffect: UIVisualEffectView!
  34. @IBOutlet weak var progressView: UIProgressView!
  35. internal var objectId = ""
  36. private var user = ""
  37. weak var delegate: NCGridCellDelegate?
  38. var namedButtonMore = ""
  39. var fileAvatarImageView: UIImageView? {
  40. get {
  41. return nil
  42. }
  43. }
  44. var fileObjectId: String? {
  45. get {
  46. return objectId
  47. }
  48. set {
  49. objectId = newValue ?? ""
  50. }
  51. }
  52. var filePreviewImageView: UIImageView? {
  53. get {
  54. return imageItem
  55. }
  56. }
  57. var fileUser: String? {
  58. get {
  59. return user
  60. }
  61. set {
  62. user = newValue ?? ""
  63. }
  64. }
  65. override func awakeFromNib() {
  66. super.awakeFromNib()
  67. imageItem.layer.cornerRadius = 6
  68. imageItem.layer.masksToBounds = true
  69. imageVisualEffect.layer.cornerRadius = 6
  70. imageVisualEffect.clipsToBounds = true
  71. imageVisualEffect.alpha = 0.5
  72. progressView.tintColor = NCBrandColor.shared.brandElement
  73. progressView.transform = CGAffineTransform(scaleX: 1.0, y: 0.5)
  74. progressView.trackTintColor = .clear
  75. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
  76. longPressedGesture.minimumPressDuration = 0.5
  77. longPressedGesture.delegate = self
  78. longPressedGesture.delaysTouchesBegan = true
  79. self.addGestureRecognizer(longPressedGesture)
  80. let longPressedGestureMore = UILongPressGestureRecognizer(target: self, action: #selector(longPressInsideMore(gestureRecognizer:)))
  81. longPressedGestureMore.minimumPressDuration = 0.5
  82. longPressedGestureMore.delegate = self
  83. longPressedGestureMore.delaysTouchesBegan = true
  84. buttonMore.addGestureRecognizer(longPressedGestureMore)
  85. }
  86. override func prepareForReuse() {
  87. super.prepareForReuse()
  88. imageItem.backgroundColor = nil
  89. }
  90. @IBAction func touchUpInsideMore(_ sender: Any) {
  91. delegate?.tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, image: imageItem.image, sender: sender)
  92. }
  93. @objc func longPressInsideMore(gestureRecognizer: UILongPressGestureRecognizer) {
  94. delegate?.longPressMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, gestureRecognizer: gestureRecognizer)
  95. }
  96. @objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
  97. delegate?.longPressGridItem(with: objectId, gestureRecognizer: gestureRecognizer)
  98. }
  99. func setButtonMore(named: String, image: UIImage) {
  100. namedButtonMore = named
  101. buttonMore.setImage(image, for: .normal)
  102. }
  103. func hideButtonMore(_ status: Bool) {
  104. buttonMore.isHidden = status
  105. }
  106. func selectMode(_ status: Bool) {
  107. if status {
  108. imageSelect.isHidden = false
  109. } else {
  110. imageSelect.isHidden = true
  111. imageVisualEffect.isHidden = true
  112. }
  113. }
  114. func selected(_ status: Bool) {
  115. if status {
  116. if traitCollection.userInterfaceStyle == .dark {
  117. imageVisualEffect.effect = UIBlurEffect(style: .dark)
  118. imageVisualEffect.backgroundColor = .black
  119. } else {
  120. imageVisualEffect.effect = UIBlurEffect(style: .extraLight)
  121. imageVisualEffect.backgroundColor = .lightGray
  122. }
  123. imageSelect.image = NCBrandColor.cacheImages.checkedYes
  124. imageVisualEffect.isHidden = false
  125. } else {
  126. imageSelect.isHidden = true
  127. imageVisualEffect.isHidden = true
  128. }
  129. }
  130. }
  131. protocol NCGridCellDelegate: AnyObject {
  132. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any)
  133. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer)
  134. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer)
  135. }
  136. // optional func
  137. extension NCGridCellDelegate {
  138. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {}
  139. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {}
  140. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {}
  141. }
  142. // MARK: - Grid Layout
  143. class NCGridLayout: UICollectionViewFlowLayout {
  144. var heightLabelPlusButton: CGFloat = 45
  145. var marginLeftRight: CGFloat = 6
  146. var itemForLine: CGFloat = 3
  147. var itemWidthDefault: CGFloat = 120
  148. // MARK: - View Life Cycle
  149. override init() {
  150. super.init()
  151. sectionHeadersPinToVisibleBounds = false
  152. minimumInteritemSpacing = 1
  153. minimumLineSpacing = marginLeftRight
  154. self.scrollDirection = .vertical
  155. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  156. }
  157. required init?(coder aDecoder: NSCoder) {
  158. fatalError("init(coder:) has not been implemented")
  159. }
  160. override var itemSize: CGSize {
  161. get {
  162. if let collectionView = collectionView {
  163. if collectionView.frame.width < 400 {
  164. itemForLine = 3
  165. } else {
  166. itemForLine = collectionView.frame.width / itemWidthDefault
  167. }
  168. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  169. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  170. return CGSize(width: itemWidth, height: itemHeight)
  171. }
  172. // Default fallback
  173. return CGSize(width: itemWidthDefault, height: itemWidthDefault)
  174. }
  175. set {
  176. super.itemSize = newValue
  177. }
  178. }
  179. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  180. return proposedContentOffset
  181. }
  182. }