NCGridCell.swift 7.4 KB

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