NCListCell.swift 11 KB

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