NCListCell.swift 13 KB

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