NCListCell.swift 11 KB

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