NCGridCell.swift 8.5 KB

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