NCActivityTableViewCell.swift 13 KB

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