NCListCell.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 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. var ocId = ""
  46. var ocIdTransfer = ""
  47. var user = ""
  48. weak var listCellDelegate: NCListCellDelegate?
  49. var fileAvatarImageView: UIImageView? {
  50. return imageShared
  51. }
  52. var fileOcId: String? {
  53. get { return ocId }
  54. set { ocId = newValue ?? "" }
  55. }
  56. var fileOcIdTransfer: String? {
  57. get { return ocIdTransfer }
  58. set { ocIdTransfer = newValue ?? "" }
  59. }
  60. var filePreviewImageView: UIImageView? {
  61. get { return imageItem }
  62. set { imageItem = newValue }
  63. }
  64. var fileUser: String? {
  65. get { return user }
  66. set { user = newValue ?? "" }
  67. }
  68. var fileTitleLabel: UILabel? {
  69. get { return labelTitle }
  70. set { labelTitle = newValue }
  71. }
  72. var fileInfoLabel: UILabel? {
  73. get { return labelInfo }
  74. set { labelInfo = newValue }
  75. }
  76. var fileSubinfoLabel: UILabel? {
  77. get { return labelSubinfo }
  78. set { labelSubinfo = 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. initCell()
  107. }
  108. override func prepareForReuse() {
  109. super.prepareForReuse()
  110. initCell()
  111. }
  112. func initCell() {
  113. accessibilityHint = nil
  114. accessibilityLabel = nil
  115. accessibilityValue = nil
  116. isAccessibilityElement = true
  117. imageItem.image = nil
  118. imageItem.layer.cornerRadius = 6
  119. imageItem.layer.masksToBounds = true
  120. imageStatus.image = nil
  121. imageFavorite.image = nil
  122. imageFavoriteBackground.isHidden = true
  123. imageLocal.image = nil
  124. labelTitle.text = ""
  125. labelInfo.text = ""
  126. labelSubinfo.text = ""
  127. imageShared.image = nil
  128. imageMore.image = nil
  129. separatorHeightConstraint.constant = 0.5
  130. tag0.text = ""
  131. tag1.text = ""
  132. titleInfoTrailingDefault()
  133. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
  134. longPressedGesture.minimumPressDuration = 0.5
  135. longPressedGesture.delegate = self
  136. longPressedGesture.delaysTouchesBegan = true
  137. self.addGestureRecognizer(longPressedGesture)
  138. }
  139. override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
  140. return nil
  141. }
  142. @IBAction func touchUpInsideShare(_ sender: Any) {
  143. listCellDelegate?.tapShareListItem(with: ocId, ocIdTransfer: ocIdTransfer, sender: sender)
  144. }
  145. @IBAction func touchUpInsideMore(_ sender: Any) {
  146. listCellDelegate?.tapMoreListItem(with: ocId, ocIdTransfer: ocIdTransfer, image: imageItem.image, sender: sender)
  147. }
  148. @objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
  149. listCellDelegate?.longPressListItem(with: ocId, ocIdTransfer: ocIdTransfer, gestureRecognizer: gestureRecognizer)
  150. }
  151. fileprivate func setA11yActions() {
  152. self.accessibilityCustomActions = [
  153. UIAccessibilityCustomAction(
  154. name: NSLocalizedString("_share_", comment: ""),
  155. target: self,
  156. selector: #selector(touchUpInsideShare)),
  157. UIAccessibilityCustomAction(
  158. name: NSLocalizedString("_more_", comment: ""),
  159. target: self,
  160. selector: #selector(touchUpInsideMore))
  161. ]
  162. }
  163. func titleInfoTrailingFull() {
  164. titleTrailingConstraint.constant = 10
  165. subInfoTrailingConstraint.constant = 10
  166. }
  167. func titleInfoTrailingDefault() {
  168. titleTrailingConstraint.constant = 90
  169. subInfoTrailingConstraint.constant = 90
  170. }
  171. func setButtonMore(image: UIImage) {
  172. imageMore.image = image
  173. setA11yActions()
  174. }
  175. func hideButtonMore(_ status: Bool) {
  176. imageMore.isHidden = status
  177. buttonMore.isHidden = status
  178. }
  179. func hideButtonShare(_ status: Bool) {
  180. imageShared.isHidden = status
  181. buttonShared.isHidden = status
  182. }
  183. func selected(_ status: Bool, isEditMode: Bool) {
  184. if isEditMode {
  185. imageItemLeftConstraint.constant = 45
  186. imageSelect.isHidden = false
  187. imageShared.isHidden = true
  188. imageMore.isHidden = true
  189. buttonShared.isHidden = true
  190. buttonMore.isHidden = true
  191. accessibilityCustomActions = nil
  192. } else {
  193. imageItemLeftConstraint.constant = 10
  194. imageSelect.isHidden = true
  195. imageShared.isHidden = false
  196. imageMore.isHidden = false
  197. buttonShared.isHidden = false
  198. buttonMore.isHidden = false
  199. backgroundView = nil
  200. setA11yActions()
  201. }
  202. if status {
  203. var blurEffectView: UIView?
  204. blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemMaterial))
  205. blurEffectView?.backgroundColor = .lightGray
  206. blurEffectView?.frame = self.bounds
  207. blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  208. imageSelect.image = NCImageCache.shared.getImageCheckedYes()
  209. backgroundView = blurEffectView
  210. separator.isHidden = true
  211. } else {
  212. imageSelect.image = NCImageCache.shared.getImageCheckedNo()
  213. backgroundView = nil
  214. separator.isHidden = false
  215. }
  216. }
  217. func writeInfoDateSize(date: NSDate, size: Int64) {
  218. labelInfo.text = NCUtility().dateDiff(date as Date)
  219. labelSubinfo.text = " · " + NCUtilityFileSystem().transformedSize(size)
  220. }
  221. func setAccessibility(label: String, value: String) {
  222. accessibilityLabel = label
  223. accessibilityValue = value
  224. }
  225. func setTags(tags: [String]) {
  226. if tags.isEmpty {
  227. tag0.isHidden = true
  228. tag1.isHidden = true
  229. labelInfo.isHidden = false
  230. labelSubinfo.isHidden = false
  231. } else {
  232. tag0.isHidden = false
  233. tag1.isHidden = true
  234. labelInfo.isHidden = true
  235. labelSubinfo.isHidden = true
  236. if let tag = tags.first {
  237. tag0.text = tag
  238. if tags.count > 1 {
  239. tag1.isHidden = false
  240. tag1.text = "+\(tags.count - 1)"
  241. }
  242. }
  243. }
  244. }
  245. func setIconOutlines() {
  246. imageFavoriteBackground.isHidden = fileFavoriteImage?.image == nil
  247. if imageStatus.image != nil {
  248. imageStatus.makeCircularBackground(withColor: .systemBackground)
  249. } else {
  250. imageStatus.backgroundColor = .clear
  251. }
  252. if imageLocal.image != nil {
  253. imageLocal.makeCircularBackground(withColor: .systemBackground)
  254. } else {
  255. imageLocal.backgroundColor = .clear
  256. }
  257. }
  258. }
  259. protocol NCListCellDelegate: AnyObject {
  260. func tapShareListItem(with ocId: String, ocIdTransfer: String, sender: Any)
  261. func tapMoreListItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any)
  262. func longPressListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer)
  263. }
  264. // MARK: - List Layout
  265. class NCListLayout: UICollectionViewFlowLayout {
  266. var itemHeight: CGFloat = 60
  267. override init() {
  268. super.init()
  269. sectionHeadersPinToVisibleBounds = false
  270. minimumInteritemSpacing = 0
  271. minimumLineSpacing = 1
  272. self.scrollDirection = .vertical
  273. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  274. }
  275. required init?(coder aDecoder: NSCoder) {
  276. fatalError("init(coder:) has not been implemented")
  277. }
  278. override var itemSize: CGSize {
  279. get {
  280. if let collectionView = collectionView {
  281. let itemWidth: CGFloat = collectionView.frame.width
  282. return CGSize(width: itemWidth, height: self.itemHeight)
  283. }
  284. return CGSize(width: 100, height: 100)
  285. }
  286. set {
  287. super.itemSize = newValue
  288. }
  289. }
  290. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  291. return proposedContentOffset
  292. }
  293. }