NCActivityTableViewCell.swift 10 KB

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