NCGridCell.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 labelInfo: UILabel!
  32. @IBOutlet weak var labelSubinfo: UILabel!
  33. @IBOutlet weak var buttonMore: UIButton!
  34. @IBOutlet weak var imageVisualEffect: UIVisualEffectView!
  35. var objectId = ""
  36. var indexPath = IndexPath()
  37. private var user = ""
  38. weak var gridCellDelegate: NCGridCellDelegate?
  39. var namedButtonMore = ""
  40. var fileObjectId: String? {
  41. get { return objectId }
  42. set { objectId = newValue ?? "" }
  43. }
  44. var filePreviewImageView: UIImageView? {
  45. get { return imageItem }
  46. set { imageItem = newValue }
  47. }
  48. var fileUser: String? {
  49. get { return user }
  50. set { user = newValue ?? "" }
  51. }
  52. var fileTitleLabel: UILabel? {
  53. get { return labelTitle }
  54. set { labelTitle = newValue }
  55. }
  56. var fileInfoLabel: UILabel? {
  57. get { return labelInfo }
  58. set { labelInfo = newValue }
  59. }
  60. var fileSubinfoLabel: UILabel? {
  61. get { return labelSubinfo }
  62. set { labelSubinfo = newValue }
  63. }
  64. var fileStatusImage: UIImageView? {
  65. get { return imageStatus }
  66. set { imageStatus = newValue }
  67. }
  68. var fileLocalImage: UIImageView? {
  69. get { return imageLocal }
  70. set { imageLocal = newValue }
  71. }
  72. var fileFavoriteImage: UIImageView? {
  73. get { return imageFavorite }
  74. set { imageFavorite = newValue }
  75. }
  76. override func awakeFromNib() {
  77. super.awakeFromNib()
  78. initCell()
  79. }
  80. override func prepareForReuse() {
  81. super.prepareForReuse()
  82. initCell()
  83. }
  84. func initCell() {
  85. accessibilityHint = nil
  86. accessibilityLabel = nil
  87. accessibilityValue = nil
  88. isAccessibilityElement = true
  89. imageItem.layer.cornerRadius = 6
  90. imageItem.layer.masksToBounds = true
  91. imageVisualEffect.layer.cornerRadius = 6
  92. imageVisualEffect.clipsToBounds = true
  93. imageVisualEffect.alpha = 0.5
  94. imageSelect.isHidden = true
  95. imageSelect.image = NCImageCache.images.checkedYes
  96. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
  97. longPressedGesture.minimumPressDuration = 0.5
  98. longPressedGesture.delegate = self
  99. longPressedGesture.delaysTouchesBegan = true
  100. self.addGestureRecognizer(longPressedGesture)
  101. labelTitle.text = ""
  102. labelInfo.text = ""
  103. labelSubinfo.text = ""
  104. }
  105. override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
  106. return nil
  107. }
  108. @IBAction func touchUpInsideMore(_ sender: Any) {
  109. gridCellDelegate?.tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, image: imageItem.image, indexPath: indexPath, sender: sender)
  110. }
  111. @objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
  112. gridCellDelegate?.longPressGridItem(with: objectId, indexPath: indexPath, gestureRecognizer: gestureRecognizer)
  113. }
  114. fileprivate func setA11yActions() {
  115. let moreName = namedButtonMore == NCGlobal.shared.buttonMoreStop ? "_cancel_" : "_more_"
  116. self.accessibilityCustomActions = [
  117. UIAccessibilityCustomAction(
  118. name: NSLocalizedString(moreName, comment: ""),
  119. target: self,
  120. selector: #selector(touchUpInsideMore))
  121. ]
  122. }
  123. func setButtonMore(named: String, image: UIImage) {
  124. namedButtonMore = named
  125. buttonMore.setImage(image, for: .normal)
  126. setA11yActions()
  127. }
  128. func hideButtonMore(_ status: Bool) {
  129. buttonMore.isHidden = status
  130. }
  131. func selected(_ status: Bool, isEditMode: Bool) {
  132. if isEditMode {
  133. buttonMore.isHidden = true
  134. accessibilityCustomActions = nil
  135. } else {
  136. buttonMore.isHidden = false
  137. setA11yActions()
  138. }
  139. if status {
  140. imageSelect.isHidden = false
  141. imageVisualEffect.isHidden = false
  142. } else {
  143. imageSelect.isHidden = true
  144. imageVisualEffect.isHidden = true
  145. }
  146. }
  147. func writeInfoDateSize(date: NSDate, size: Int64) {
  148. let dateFormatter = DateFormatter()
  149. dateFormatter.dateStyle = .short
  150. dateFormatter.timeStyle = .none
  151. dateFormatter.locale = Locale.current
  152. labelInfo.text = dateFormatter.string(from: date as Date)
  153. labelSubinfo.text = NCUtilityFileSystem().transformedSize(size)
  154. }
  155. func setAccessibility(label: String, value: String) {
  156. accessibilityLabel = label
  157. accessibilityValue = value
  158. }
  159. func setIconOutlines() {
  160. if imageStatus.image != nil {
  161. imageStatus.makeCircularBackground(withColor: .systemBackground)
  162. } else {
  163. imageStatus.backgroundColor = .clear
  164. }
  165. }
  166. }
  167. protocol NCGridCellDelegate: AnyObject {
  168. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, indexPath: IndexPath, sender: Any)
  169. func longPressGridItem(with objectId: String, indexPath: IndexPath, gestureRecognizer: UILongPressGestureRecognizer)
  170. }
  171. // MARK: - Grid Layout
  172. class NCGridLayout: UICollectionViewFlowLayout {
  173. var heightLabelPlusButton: CGFloat = 60
  174. var marginLeftRight: CGFloat = 10
  175. var column: CGFloat = 3
  176. var itemWidthDefault: CGFloat = 140
  177. override init() {
  178. super.init()
  179. sectionHeadersPinToVisibleBounds = false
  180. minimumInteritemSpacing = 1
  181. minimumLineSpacing = marginLeftRight
  182. self.scrollDirection = .vertical
  183. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  184. }
  185. required init?(coder aDecoder: NSCoder) {
  186. fatalError("init(coder:) has not been implemented")
  187. }
  188. override var itemSize: CGSize {
  189. get {
  190. if let collectionView = collectionView {
  191. if collectionView.frame.width < 400 {
  192. column = 3
  193. } else {
  194. column = collectionView.frame.width / itemWidthDefault
  195. }
  196. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (column - 1)) / column
  197. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  198. return CGSize(width: itemWidth, height: itemHeight)
  199. }
  200. return CGSize(width: itemWidthDefault, height: itemWidthDefault)
  201. }
  202. set {
  203. super.itemSize = newValue
  204. }
  205. }
  206. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  207. return proposedContentOffset
  208. }
  209. }