NCActivityTableViewCell.swift 10 KB

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