NCListCell.swift 12 KB

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