NCListCell.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // NCListCell.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/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 NCListCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellProtocol {
  25. @IBOutlet weak var imageItem: UIImageView!
  26. @IBOutlet weak var imageItemLeftConstraint: NSLayoutConstraint!
  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 labelInfo: UILabel!
  33. @IBOutlet weak var imageShared: UIImageView!
  34. @IBOutlet weak var buttonShared: UIButton!
  35. @IBOutlet weak var imageMore: UIImageView!
  36. @IBOutlet weak var buttonMore: UIButton!
  37. @IBOutlet weak var progressView: UIProgressView!
  38. @IBOutlet weak var separator: UIView!
  39. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  40. private var objectId = ""
  41. private var user = ""
  42. weak var delegate: NCListCellDelegate?
  43. var namedButtonMore = ""
  44. var fileAvatarImageView: UIImageView? {
  45. get {
  46. return imageShared
  47. }
  48. }
  49. var fileObjectId: String? {
  50. get {
  51. return objectId
  52. }
  53. set {
  54. objectId = newValue ?? ""
  55. }
  56. }
  57. var filePreviewImageView: UIImageView? {
  58. get {
  59. return imageItem
  60. }
  61. }
  62. var fileUser: String? {
  63. get {
  64. return user
  65. }
  66. set {
  67. user = newValue ?? ""
  68. }
  69. }
  70. override func awakeFromNib() {
  71. super.awakeFromNib()
  72. imageItem.layer.cornerRadius = 6
  73. imageItem.layer.masksToBounds = true
  74. // use entire cell as accessibility element
  75. accessibilityHint = nil
  76. accessibilityLabel = nil
  77. accessibilityValue = nil
  78. isAccessibilityElement = true
  79. progressView.tintColor = NCBrandColor.shared.brandElement
  80. progressView.transform = CGAffineTransform(scaleX: 1.0, y: 0.5)
  81. progressView.trackTintColor = .clear
  82. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
  83. longPressedGesture.minimumPressDuration = 0.5
  84. longPressedGesture.delegate = self
  85. longPressedGesture.delaysTouchesBegan = true
  86. self.addGestureRecognizer(longPressedGesture)
  87. let longPressedGestureMore = UILongPressGestureRecognizer(target: self, action: #selector(longPressInsideMore(gestureRecognizer:)))
  88. longPressedGestureMore.minimumPressDuration = 0.5
  89. longPressedGestureMore.delegate = self
  90. longPressedGestureMore.delaysTouchesBegan = true
  91. buttonMore.addGestureRecognizer(longPressedGestureMore)
  92. separator.backgroundColor = NCBrandColor.shared.separator
  93. separatorHeightConstraint.constant = 0.5
  94. }
  95. override func prepareForReuse() {
  96. super.prepareForReuse()
  97. imageItem.backgroundColor = nil
  98. accessibilityHint = nil
  99. accessibilityLabel = nil
  100. accessibilityValue = nil
  101. }
  102. @IBAction func touchUpInsideShare(_ sender: Any) {
  103. delegate?.tapShareListItem(with: objectId, sender: sender)
  104. }
  105. @IBAction func touchUpInsideMore(_ sender: Any) {
  106. delegate?.tapMoreListItem(with: objectId, namedButtonMore: namedButtonMore, image: imageItem.image, sender: sender)
  107. }
  108. @objc func longPressInsideMore(gestureRecognizer: UILongPressGestureRecognizer) {
  109. delegate?.longPressMoreListItem(with: objectId, namedButtonMore: namedButtonMore, gestureRecognizer: gestureRecognizer)
  110. }
  111. @objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
  112. delegate?.longPressListItem(with: objectId, gestureRecognizer: gestureRecognizer)
  113. }
  114. fileprivate func setA11yActions() {
  115. let moreName = namedButtonMore == NCGlobal.shared.buttonMoreStop ? "_cancel_" : "_more_"
  116. self.accessibilityCustomActions = [
  117. UIAccessibilityCustomAction(
  118. name: NSLocalizedString("_share_", comment: ""),
  119. target: self,
  120. selector: #selector(touchUpInsideShare)),
  121. UIAccessibilityCustomAction(
  122. name: NSLocalizedString(moreName, comment: ""),
  123. target: self,
  124. selector: #selector(touchUpInsideMore))
  125. ]
  126. }
  127. func setButtonMore(named: String, image: UIImage) {
  128. namedButtonMore = named
  129. imageMore.image = image
  130. setA11yActions()
  131. }
  132. func hideButtonMore(_ status: Bool) {
  133. imageMore.isHidden = status
  134. buttonMore.isHidden = status
  135. }
  136. func hideButtonShare(_ status: Bool) {
  137. imageShared.isHidden = status
  138. buttonShared.isHidden = status
  139. }
  140. func hideSeparator(_ status: Bool) {
  141. separator.isHidden = status
  142. }
  143. func selectMode(_ status: Bool) {
  144. if status {
  145. imageItemLeftConstraint.constant = 45
  146. imageSelect.isHidden = false
  147. accessibilityCustomActions = nil
  148. } else {
  149. imageItemLeftConstraint.constant = 10
  150. imageSelect.isHidden = true
  151. backgroundView = nil
  152. setA11yActions()
  153. }
  154. }
  155. func selected(_ status: Bool) {
  156. if status {
  157. var blurEffect: UIVisualEffect?
  158. var blurEffectView: UIView?
  159. imageSelect.image = NCBrandColor.cacheImages.checkedYes
  160. if traitCollection.userInterfaceStyle == .dark {
  161. blurEffect = UIBlurEffect(style: .dark)
  162. blurEffectView = UIVisualEffectView(effect: blurEffect)
  163. blurEffectView?.backgroundColor = .black
  164. } else {
  165. blurEffect = UIBlurEffect(style: .extraLight)
  166. blurEffectView = UIVisualEffectView(effect: blurEffect)
  167. blurEffectView?.backgroundColor = .lightGray
  168. }
  169. blurEffectView?.frame = self.bounds
  170. blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  171. backgroundView = blurEffectView
  172. separator.isHidden = true
  173. } else {
  174. imageSelect.image = NCBrandColor.cacheImages.checkedNo
  175. backgroundView = nil
  176. separator.isHidden = false
  177. }
  178. }
  179. }
  180. protocol NCListCellDelegate: AnyObject {
  181. func tapShareListItem(with objectId: String, sender: Any)
  182. func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any)
  183. func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer)
  184. func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer)
  185. }
  186. // optional func
  187. extension NCListCellDelegate {
  188. func tapShareListItem(with objectId: String, sender: Any) {}
  189. func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {}
  190. func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {}
  191. func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {}
  192. }
  193. // MARK: - List Layout
  194. class NCListLayout: UICollectionViewFlowLayout {
  195. var itemHeight: CGFloat = 60
  196. // MARK: - View Life Cycle
  197. override init() {
  198. super.init()
  199. sectionHeadersPinToVisibleBounds = false
  200. minimumInteritemSpacing = 0
  201. minimumLineSpacing = 1
  202. self.scrollDirection = .vertical
  203. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  204. }
  205. required init?(coder aDecoder: NSCoder) {
  206. fatalError("init(coder:) has not been implemented")
  207. }
  208. override var itemSize: CGSize {
  209. get {
  210. if let collectionView = collectionView {
  211. let itemWidth: CGFloat = collectionView.frame.width
  212. return CGSize(width: itemWidth, height: self.itemHeight)
  213. }
  214. // Default fallback
  215. return CGSize(width: 100, height: 100)
  216. }
  217. set {
  218. super.itemSize = newValue
  219. }
  220. }
  221. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  222. return proposedContentOffset
  223. }
  224. }