NCTransferCell.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // NCTransferCell.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 08/10/2020.
  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 NCTransferCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellProtocol {
  25. @IBOutlet weak var imageItem: UIImageView!
  26. @IBOutlet weak var imageStatus: UIImageView!
  27. @IBOutlet weak var labelTitle: UILabel!
  28. @IBOutlet weak var labelPath: UILabel!
  29. @IBOutlet weak var labelStatus: UILabel!
  30. @IBOutlet weak var labelInfo: UILabel!
  31. @IBOutlet weak var imageMore: UIImageView!
  32. @IBOutlet weak var buttonMore: UIButton!
  33. @IBOutlet weak var progressView: UIProgressView!
  34. @IBOutlet weak var separator: UIView!
  35. @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
  36. private var ocId = ""
  37. private var ocIdTransfer = ""
  38. private var user = ""
  39. weak var delegate: NCTransferCellDelegate?
  40. var fileOcId: String? {
  41. get { return ocId }
  42. set { ocId = newValue ?? "" }
  43. }
  44. var fileOcIdTransfer: String? {
  45. get { return ocIdTransfer }
  46. set { ocIdTransfer = newValue ?? "" }
  47. }
  48. var filePreviewImageView: UIImageView? {
  49. get { return imageItem }
  50. set { imageItem = newValue }
  51. }
  52. var fileStatusImage: UIImageView? {
  53. get { return imageStatus }
  54. set { imageStatus = newValue }
  55. }
  56. var fileUser: String? {
  57. get { return user }
  58. set { user = newValue ?? "" }
  59. }
  60. var fileTitleLabel: UILabel? {
  61. get { return labelTitle }
  62. set { labelTitle = newValue }
  63. }
  64. var fileInfoLabel: UILabel? {
  65. get { return labelInfo }
  66. set { labelInfo = newValue }
  67. }
  68. var fileProgressView: UIProgressView? {
  69. get { return progressView }
  70. set { progressView = newValue }
  71. }
  72. var fileMoreImage: UIImageView? {
  73. get { return imageMore }
  74. set { imageMore = newValue }
  75. }
  76. override func awakeFromNib() {
  77. super.awakeFromNib()
  78. isAccessibilityElement = true
  79. imageItem.layer.cornerRadius = 6
  80. imageItem.layer.masksToBounds = true
  81. progressView.tintColor = NCBrandColor.shared.iconImageColor
  82. progressView.transform = CGAffineTransform(scaleX: 1.0, y: 0.5)
  83. progressView.trackTintColor = .clear
  84. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
  85. longPressedGesture.minimumPressDuration = 0.5
  86. longPressedGesture.delegate = self
  87. longPressedGesture.delaysTouchesBegan = true
  88. self.addGestureRecognizer(longPressedGesture)
  89. let longPressedGestureMore = UILongPressGestureRecognizer(target: self, action: #selector(longPressInsideMore(gestureRecognizer:)))
  90. longPressedGestureMore.minimumPressDuration = 0.5
  91. longPressedGestureMore.delegate = self
  92. longPressedGestureMore.delaysTouchesBegan = true
  93. buttonMore.addGestureRecognizer(longPressedGestureMore)
  94. separator.backgroundColor = .separator
  95. separatorHeightConstraint.constant = 0.5
  96. labelTitle.text = ""
  97. labelInfo.text = ""
  98. }
  99. override func prepareForReuse() {
  100. super.prepareForReuse()
  101. imageItem.backgroundColor = nil
  102. }
  103. @IBAction func touchUpInsideShare(_ sender: Any) {
  104. delegate?.tapShareListItem(with: ocId, ocIdTransfer: ocIdTransfer, sender: sender)
  105. }
  106. @IBAction func touchUpInsideMore(_ sender: Any) {
  107. delegate?.tapMoreListItem(with: ocId, ocIdTransfer: ocIdTransfer, image: imageItem.image, sender: sender)
  108. }
  109. @objc func longPressInsideMore(gestureRecognizer: UILongPressGestureRecognizer) {
  110. delegate?.longPressMoreListItem(with: ocId, ocIdTransfer: ocIdTransfer, gestureRecognizer: gestureRecognizer)
  111. }
  112. @objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
  113. delegate?.longPressListItem(with: ocId, ocIdTransfer: ocIdTransfer, gestureRecognizer: gestureRecognizer)
  114. }
  115. func hideButtonMore(_ status: Bool) {
  116. imageMore.isHidden = status
  117. buttonMore.isHidden = status
  118. }
  119. func setProgress(progress: Float) {
  120. progressView.progress = progress
  121. if progress > 0.0 {
  122. progressView.isHidden = false
  123. } else {
  124. progressView.isHidden = true
  125. }
  126. }
  127. func setButtonMore(image: UIImage) {
  128. imageMore.image = image
  129. self.accessibilityCustomActions = [
  130. UIAccessibilityCustomAction(
  131. name: NSLocalizedString("_cancel_", comment: ""),
  132. target: self,
  133. selector: #selector(touchUpInsideMore))
  134. ]
  135. }
  136. func writeInfoDateSize(date: NSDate, size: Int64) {
  137. labelInfo.text = NCUtility().dateDiff(date as Date) + " · " + NCUtilityFileSystem().transformedSize(size)
  138. }
  139. func setIconOutlines() {
  140. if imageStatus.image != nil {
  141. imageStatus.makeCircularBackground(withColor: .systemBackground)
  142. } else {
  143. imageStatus.backgroundColor = .clear
  144. }
  145. }
  146. }
  147. protocol NCTransferCellDelegate: AnyObject {
  148. func tapShareListItem(with ocId: String, ocIdTransfer: String, sender: Any)
  149. func tapMoreListItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any)
  150. func longPressMoreListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer)
  151. func longPressListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer)
  152. }