NCActivityTableViewCell.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. override func awakeFromNib() {
  31. super.awakeFromNib()
  32. }
  33. }
  34. class NCActivityTableViewCell: UITableViewCell, NCCellProtocol {
  35. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  36. @IBOutlet weak var collectionView: UICollectionView!
  37. @IBOutlet weak var icon: UIImageView!
  38. @IBOutlet weak var avatar: UIImageView!
  39. @IBOutlet weak var subject: UILabel!
  40. @IBOutlet weak var subjectTrailingConstraint: NSLayoutConstraint!
  41. @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
  42. private var user: String = ""
  43. var idActivity: Int = 0
  44. var activityPreviews: [tableActivityPreview] = []
  45. var didSelectItemEnable: Bool = true
  46. var viewController: UIViewController?
  47. var fileAvatarImageView: UIImageView? {
  48. get { return avatar }
  49. }
  50. var fileUser: String? {
  51. get { return user }
  52. set { user = newValue ?? "" }
  53. }
  54. override func awakeFromNib() {
  55. super.awakeFromNib()
  56. collectionView.delegate = self
  57. collectionView.dataSource = self
  58. let avatarRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAvatarImage))
  59. avatar.addGestureRecognizer(avatarRecognizer)
  60. }
  61. @objc func tapAvatarImage() {
  62. guard let fileUser = fileUser else { return }
  63. viewController?.showProfileMenu(userId: fileUser)
  64. }
  65. }
  66. // MARK: - Collection View
  67. extension NCActivityTableViewCell: UICollectionViewDelegate {
  68. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  69. // Select not permitted
  70. if !didSelectItemEnable {
  71. return
  72. }
  73. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? NCActivityCollectionViewCell
  74. let activityPreview = activityPreviews[indexPath.row]
  75. if activityPreview.view == "trashbin" {
  76. var responder: UIResponder? = collectionView
  77. while !(responder is UIViewController) {
  78. responder = responder?.next
  79. if responder == nil {
  80. break
  81. }
  82. }
  83. if (responder as? UIViewController)!.navigationController != nil {
  84. if let viewController = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  85. if let result = NCManageDatabase.shared.getTrashItem(fileId: String(activityPreview.fileId), account: activityPreview.account) {
  86. viewController.blinkFileId = result.fileId
  87. viewController.trashPath = result.filePath
  88. (responder as? UIViewController)!.navigationController?.pushViewController(viewController, animated: true)
  89. } else {
  90. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_trash_file_not_found_")
  91. NCContentPresenter.shared.showError(error: error)
  92. }
  93. }
  94. }
  95. return
  96. }
  97. if activityPreview.view == NCGlobal.shared.appName && activityPreview.mimeType != "dir" {
  98. guard let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: activityPreview.account, idActivity: activityPreview.idActivity, id: String(activityPreview.fileId)) else {
  99. return
  100. }
  101. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "fileId == %@", activitySubjectRich.id)) {
  102. if let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView) {
  103. do {
  104. let attr = try FileManager.default.attributesOfItem(atPath: filePath)
  105. let fileSize = attr[FileAttributeKey.size] as! UInt64
  106. if fileSize > 0 {
  107. if let viewController = self.viewController {
  108. NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: cell?.imageView.image)
  109. }
  110. return
  111. }
  112. } catch {
  113. print("Error: \(error)")
  114. }
  115. }
  116. }
  117. let hud = JGProgressHUD()
  118. hud.indicatorView = JGProgressHUDRingIndicatorView()
  119. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
  120. indicatorView.ringWidth = 1.5
  121. }
  122. guard let view = appDelegate.window?.rootViewController?.view else { return }
  123. hud.show(in: view)
  124. NextcloudKit.shared.getFileFromFileId(fileId: String(activityPreview.fileId)) { account, file, data, error in
  125. if let file = file {
  126. let isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(file: file)
  127. let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
  128. NCManageDatabase.shared.addMetadata(metadata)
  129. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  130. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  131. NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { _ in
  132. }, taskHandler: { _ in
  133. }, progressHandler: { progress in
  134. hud.progress = Float(progress.fractionCompleted)
  135. }) { account, _, _, _, _, _, error in
  136. hud.dismiss()
  137. if account == self.appDelegate.account && error == .success {
  138. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  139. if let viewController = self.viewController {
  140. NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: cell?.imageView.image)
  141. }
  142. }
  143. }
  144. } else {
  145. hud.dismiss()
  146. NCContentPresenter.shared.showError(error: error)
  147. }
  148. }
  149. }
  150. }
  151. }
  152. extension NCActivityTableViewCell: UICollectionViewDataSource {
  153. func numberOfSections(in collectionView: UICollectionView) -> Int {
  154. return 1
  155. }
  156. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  157. let results = activityPreviews.unique { $0.fileId }
  158. return results.count
  159. }
  160. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  161. let cell: NCActivityCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! NCActivityCollectionViewCell
  162. cell.imageView.image = nil
  163. let activityPreview = activityPreviews[indexPath.row]
  164. let fileId = String(activityPreview.fileId)
  165. // Trashbin
  166. if activityPreview.view == "trashbin" {
  167. let source = activityPreview.source
  168. NCUtility.shared.convertSVGtoPNGWriteToUserData(svgUrlString: source, width: 100, rewrite: false, account: appDelegate.account, id: idActivity) { imageNamePath, id in
  169. if let imageNamePath = imageNamePath, id == self.idActivity, let image = UIImage(contentsOfFile: imageNamePath) {
  170. cell.imageView.image = image
  171. } else {
  172. cell.imageView.image = UIImage(named: "file")
  173. }
  174. }
  175. } else {
  176. if activityPreview.isMimeTypeIcon {
  177. let source = activityPreview.source
  178. NCUtility.shared.convertSVGtoPNGWriteToUserData(svgUrlString: source, width: 100, rewrite: false, account: appDelegate.account, id: idActivity) { imageNamePath, id in
  179. if let imageNamePath = imageNamePath, id == self.idActivity, let image = UIImage(contentsOfFile: imageNamePath) {
  180. cell.imageView.image = image
  181. } else {
  182. cell.imageView.image = UIImage(named: "file")
  183. }
  184. }
  185. } else {
  186. if let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: activityPreview.account, idActivity: idActivity, id: fileId) {
  187. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + activitySubjectRich.name
  188. if FileManager.default.fileExists(atPath: fileNamePath), let image = UIImage(contentsOfFile: fileNamePath) {
  189. cell.imageView.image = image
  190. } else {
  191. NCOperationQueue.shared.downloadThumbnailActivity(fileNamePathOrFileId: activityPreview.source, fileNamePreviewLocalPath: fileNamePath, fileId: fileId, cell: cell, collectionView: collectionView)
  192. }
  193. }
  194. }
  195. }
  196. return cell
  197. }
  198. }
  199. extension NCActivityTableViewCell: UICollectionViewDelegateFlowLayout {
  200. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  201. return CGSize(width: 50, height: 50)
  202. }
  203. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  204. return 20
  205. }
  206. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  207. return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  208. }
  209. }