NCActivityTableViewCell.swift 12 KB

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