NCListCell.swift 9.9 KB

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