NCListCell.swift 14 KB

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