NCListCell.swift 13 KB

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