NCActivityTableViewCell.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // NCActivityCollectionViewCell.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 17/01/2019.
  6. // Copyright © 2021. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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 Foundation
  24. import NextcloudKit
  25. import FloatingPanel
  26. import JGProgressHUD
  27. import Queuer
  28. class NCActivityCollectionViewCell: UICollectionViewCell {
  29. @IBOutlet weak var imageView: UIImageView!
  30. var fileId = ""
  31. var indexPath = IndexPath()
  32. override func awakeFromNib() {
  33. super.awakeFromNib()
  34. }
  35. }
  36. class NCActivityTableViewCell: UITableViewCell, NCCellProtocol {
  37. @IBOutlet weak var collectionView: UICollectionView!
  38. @IBOutlet weak var icon: UIImageView!
  39. @IBOutlet weak var avatar: UIImageView!
  40. @IBOutlet weak var subject: UILabel!
  41. @IBOutlet weak var subjectTrailingConstraint: NSLayoutConstraint!
  42. @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
  43. private let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  44. private var user: String = ""
  45. private var index = IndexPath()
  46. var idActivity: Int = 0
  47. var activityPreviews: [tableActivityPreview] = []
  48. var didSelectItemEnable: Bool = true
  49. var viewController = UIViewController()
  50. let utility = NCUtility()
  51. var indexPath: IndexPath {
  52. get { return index }
  53. set { index = newValue }
  54. }
  55. var fileAvatarImageView: UIImageView? {
  56. return avatar
  57. }
  58. var fileUser: String? {
  59. get { return user }
  60. set { user = newValue ?? "" }
  61. }
  62. override func awakeFromNib() {
  63. super.awakeFromNib()
  64. collectionView.delegate = self
  65. collectionView.dataSource = self
  66. let avatarRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAvatarImage))
  67. avatar.addGestureRecognizer(avatarRecognizer)
  68. }
  69. @objc func tapAvatarImage() {
  70. guard let fileUser = fileUser else { return }
  71. viewController.showProfileMenu(userId: fileUser)
  72. }
  73. }
  74. // MARK: - Collection View
  75. extension NCActivityTableViewCell: UICollectionViewDelegate {
  76. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  77. // Select not permitted
  78. if !didSelectItemEnable {
  79. return
  80. }
  81. let activityPreview = activityPreviews[indexPath.row]
  82. if activityPreview.view == "trashbin" {
  83. var responder: UIResponder? = collectionView
  84. while !(responder is UIViewController) {
  85. responder = responder?.next
  86. if responder == nil {
  87. break
  88. }
  89. }
  90. if (responder as? UIViewController)!.navigationController != nil {
  91. if let viewController = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  92. if let result = NCManageDatabase.shared.getTrashItem(fileId: String(activityPreview.fileId), account: activityPreview.account) {
  93. viewController.blinkFileId = result.fileId
  94. viewController.filePath = result.filePath
  95. (responder as? UIViewController)!.navigationController?.pushViewController(viewController, animated: true)
  96. } else {
  97. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_trash_file_not_found_")
  98. NCContentPresenter().showError(error: error)
  99. }
  100. }
  101. }
  102. return
  103. }
  104. if activityPreview.view == NCGlobal.shared.appName && activityPreview.mimeType != "dir" {
  105. guard let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: activityPreview.account, idActivity: activityPreview.idActivity, id: String(activityPreview.fileId)) else {
  106. return
  107. }
  108. NCActionCenter.shared.viewerFile(account: appDelegate.account, fileId: activitySubjectRich.id, viewController: viewController)
  109. }
  110. }
  111. }
  112. extension NCActivityTableViewCell: UICollectionViewDataSource {
  113. func numberOfSections(in collectionView: UICollectionView) -> Int {
  114. return 1
  115. }
  116. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  117. let results = activityPreviews.unique { $0.fileId }
  118. return results.count
  119. }
  120. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  121. guard let cell: NCActivityCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? NCActivityCollectionViewCell else {
  122. return UICollectionViewCell()
  123. }
  124. cell.imageView.image = nil
  125. cell.indexPath = indexPath
  126. let activityPreview = activityPreviews[indexPath.row]
  127. let fileId = String(activityPreview.fileId)
  128. // Trashbin
  129. if activityPreview.view == "trashbin" {
  130. let source = activityPreview.source
  131. utility.convertSVGtoPNGWriteToUserData(svgUrlString: source, width: 100, rewrite: false, account: appDelegate.account, id: idActivity) { imageNamePath, id in
  132. if let imageNamePath = imageNamePath, id == self.idActivity, let image = UIImage(contentsOfFile: imageNamePath) {
  133. cell.imageView.image = image
  134. } else {
  135. cell.imageView.image = UIImage(named: "file")
  136. }
  137. }
  138. } else {
  139. if activityPreview.isMimeTypeIcon {
  140. let source = activityPreview.source
  141. utility.convertSVGtoPNGWriteToUserData(svgUrlString: source, width: 100, rewrite: false, account: appDelegate.account, id: idActivity) { imageNamePath, id in
  142. if let imageNamePath = imageNamePath, id == self.idActivity, let image = UIImage(contentsOfFile: imageNamePath) {
  143. cell.imageView.image = image
  144. } else {
  145. cell.imageView.image = UIImage(named: "file")
  146. }
  147. }
  148. } else {
  149. if let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: activityPreview.account, idActivity: idActivity, id: fileId) {
  150. let fileNamePath = NCUtilityFileSystem().directoryUserData + "/" + activitySubjectRich.name
  151. if FileManager.default.fileExists(atPath: fileNamePath), let image = UIImage(contentsOfFile: fileNamePath) {
  152. cell.imageView.image = image
  153. } else {
  154. cell.imageView?.image = UIImage(named: "file_photo")
  155. cell.fileId = fileId
  156. if !FileManager.default.fileExists(atPath: fileNamePath) {
  157. if NCNetworking.shared.downloadThumbnailActivityQueue.operations.filter({ ($0 as? NCOperationDownloadThumbnailActivity)?.fileId == fileId }).isEmpty {
  158. NCNetworking.shared.downloadThumbnailActivityQueue.addOperation(NCOperationDownloadThumbnailActivity(fileNamePathOrFileId: activityPreview.source, fileNamePreviewLocalPath: fileNamePath, fileId: fileId, cell: cell, collectionView: collectionView))
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. return cell
  166. }
  167. }
  168. extension NCActivityTableViewCell: UICollectionViewDelegateFlowLayout {
  169. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  170. return CGSize(width: 50, height: 50)
  171. }
  172. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  173. return 20
  174. }
  175. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  176. return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  177. }
  178. }
  179. class NCOperationDownloadThumbnailActivity: ConcurrentOperation {
  180. var cell: NCActivityCollectionViewCell?
  181. var collectionView: UICollectionView?
  182. var fileNamePathOrFileId: String
  183. var fileNamePreviewLocalPath: String
  184. var fileId: String
  185. init(fileNamePathOrFileId: String, fileNamePreviewLocalPath: String, fileId: String, cell: NCActivityCollectionViewCell?, collectionView: UICollectionView?) {
  186. self.fileNamePathOrFileId = fileNamePathOrFileId
  187. self.fileNamePreviewLocalPath = fileNamePreviewLocalPath
  188. self.fileId = fileId
  189. self.cell = cell
  190. self.collectionView = collectionView
  191. }
  192. override func start() {
  193. guard !isCancelled else { return self.finish() }
  194. NextcloudKit.shared.downloadPreview(fileNamePathOrFileId: fileNamePathOrFileId,
  195. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  196. widthPreview: 0,
  197. heightPreview: 0,
  198. etag: nil,
  199. useInternalEndpoint: false,
  200. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, imagePreview, _, _, _, error in
  201. if error == .success, let imagePreview = imagePreview {
  202. DispatchQueue.main.async {
  203. if self.fileId == self.cell?.fileId, let imageView = self.cell?.imageView {
  204. UIView.transition(with: imageView,
  205. duration: 0.75,
  206. options: .transitionCrossDissolve,
  207. animations: { imageView.image = imagePreview },
  208. completion: nil)
  209. } else {
  210. self.collectionView?.reloadData()
  211. }
  212. }
  213. }
  214. self.finish()
  215. }
  216. }
  217. }