NCActivityTableViewCell.swift 12 KB

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