NCActivityTableViewCell.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. class NCActivityCollectionViewCell: UICollectionViewCell {
  28. @IBOutlet weak var imageView: UIImageView!
  29. var fileId = ""
  30. var indexPath = IndexPath()
  31. override func awakeFromNib() {
  32. super.awakeFromNib()
  33. }
  34. }
  35. class NCActivityTableViewCell: UITableViewCell, NCCellProtocol {
  36. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  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 var user: String = ""
  44. private var index = IndexPath()
  45. var idActivity: Int = 0
  46. var activityPreviews: [tableActivityPreview] = []
  47. var didSelectItemEnable: Bool = true
  48. var viewController = UIViewController()
  49. var indexPath: IndexPath {
  50. get { return index }
  51. set { index = newValue }
  52. }
  53. var fileAvatarImageView: UIImageView? {
  54. get { return avatar }
  55. }
  56. var fileUser: String? {
  57. get { return user }
  58. set { user = newValue ?? "" }
  59. }
  60. override func awakeFromNib() {
  61. super.awakeFromNib()
  62. collectionView.delegate = self
  63. collectionView.dataSource = self
  64. let avatarRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAvatarImage))
  65. avatar.addGestureRecognizer(avatarRecognizer)
  66. }
  67. @objc func tapAvatarImage() {
  68. guard let fileUser = fileUser else { return }
  69. viewController.showProfileMenu(userId: fileUser)
  70. }
  71. }
  72. // MARK: - Collection View
  73. extension NCActivityTableViewCell: UICollectionViewDelegate {
  74. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  75. // Select not permitted
  76. if !didSelectItemEnable {
  77. return
  78. }
  79. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? NCActivityCollectionViewCell
  80. let activityPreview = activityPreviews[indexPath.row]
  81. if activityPreview.view == "trashbin" {
  82. var responder: UIResponder? = collectionView
  83. while !(responder is UIViewController) {
  84. responder = responder?.next
  85. if responder == nil {
  86. break
  87. }
  88. }
  89. if (responder as? UIViewController)!.navigationController != nil {
  90. if let viewController = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  91. if let result = NCManageDatabase.shared.getTrashItem(fileId: String(activityPreview.fileId), account: activityPreview.account) {
  92. viewController.blinkFileId = result.fileId
  93. viewController.trashPath = result.filePath
  94. (responder as? UIViewController)!.navigationController?.pushViewController(viewController, animated: true)
  95. } else {
  96. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_trash_file_not_found_")
  97. NCContentPresenter.shared.showError(error: error)
  98. }
  99. }
  100. }
  101. return
  102. }
  103. if activityPreview.view == NCGlobal.shared.appName && activityPreview.mimeType != "dir" {
  104. guard let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: activityPreview.account, idActivity: activityPreview.idActivity, id: String(activityPreview.fileId)) else {
  105. return
  106. }
  107. NCActionCenter.shared.viewerFile(account: appDelegate.account, fileId: activitySubjectRich.id, viewController: viewController)
  108. }
  109. }
  110. }
  111. extension NCActivityTableViewCell: UICollectionViewDataSource {
  112. func numberOfSections(in collectionView: UICollectionView) -> Int {
  113. return 1
  114. }
  115. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  116. let results = activityPreviews.unique { $0.fileId }
  117. return results.count
  118. }
  119. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  120. let cell: NCActivityCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! NCActivityCollectionViewCell
  121. cell.imageView.image = nil
  122. cell.indexPath = indexPath
  123. let activityPreview = activityPreviews[indexPath.row]
  124. let fileId = String(activityPreview.fileId)
  125. // Trashbin
  126. if activityPreview.view == "trashbin" {
  127. let source = activityPreview.source
  128. NCUtility.shared.convertSVGtoPNGWriteToUserData(svgUrlString: source, width: 100, rewrite: false, account: appDelegate.account, id: idActivity) { imageNamePath, id in
  129. if let imageNamePath = imageNamePath, id == self.idActivity, let image = UIImage(contentsOfFile: imageNamePath) {
  130. cell.imageView.image = image
  131. } else {
  132. cell.imageView.image = UIImage(named: "file")
  133. }
  134. }
  135. } else {
  136. if activityPreview.isMimeTypeIcon {
  137. let source = activityPreview.source
  138. NCUtility.shared.convertSVGtoPNGWriteToUserData(svgUrlString: source, width: 100, rewrite: false, account: appDelegate.account, id: idActivity) { imageNamePath, id in
  139. if let imageNamePath = imageNamePath, id == self.idActivity, let image = UIImage(contentsOfFile: imageNamePath) {
  140. cell.imageView.image = image
  141. } else {
  142. cell.imageView.image = UIImage(named: "file")
  143. }
  144. }
  145. } else {
  146. if let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: activityPreview.account, idActivity: idActivity, id: fileId) {
  147. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + activitySubjectRich.name
  148. if FileManager.default.fileExists(atPath: fileNamePath), let image = UIImage(contentsOfFile: fileNamePath) {
  149. cell.imageView.image = image
  150. } else {
  151. NCOperationQueue.shared.downloadThumbnailActivity(fileNamePathOrFileId: activityPreview.source, fileNamePreviewLocalPath: fileNamePath, fileId: fileId, cell: cell, collectionView: collectionView)
  152. }
  153. }
  154. }
  155. }
  156. return cell
  157. }
  158. }
  159. extension NCActivityTableViewCell: UICollectionViewDelegateFlowLayout {
  160. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  161. return CGSize(width: 50, height: 50)
  162. }
  163. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  164. return 20
  165. }
  166. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  167. return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  168. }
  169. }