NCActivityTableViewCell.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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? = nil
  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. override func awakeFromNib() {
  69. super.awakeFromNib()
  70. collectionView.delegate = self
  71. collectionView.dataSource = self
  72. }
  73. }
  74. // MARK: - Table View
  75. extension NCActivity: UITableViewDelegate {
  76. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  77. return 120
  78. }
  79. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  80. return 60
  81. }
  82. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  83. return UITableView.automaticDimension
  84. }
  85. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  86. let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 60))
  87. view.backgroundColor = .clear
  88. let label = UILabel()
  89. label.font = UIFont.boldSystemFont(ofSize: 13)
  90. label.textColor = NCBrandColor.shared.label
  91. label.text = CCUtility.getTitleSectionDate(sectionDates[section])
  92. label.textAlignment = .center
  93. label.layer.cornerRadius = 11
  94. label.layer.masksToBounds = true
  95. label.layer.backgroundColor = UIColor(red: 152.0/255.0, green: 167.0/255.0, blue: 181.0/255.0, alpha: 0.8).cgColor
  96. let widthFrame = label.intrinsicContentSize.width + 30
  97. let xFrame = tableView.bounds.width / 2 - widthFrame / 2
  98. label.frame = CGRect(x: xFrame, y: 10, width: widthFrame, height: 22)
  99. view.addSubview(label)
  100. return view
  101. }
  102. }
  103. // MARK: - Collection View
  104. extension NCActivityTableViewCell: UICollectionViewDelegate {
  105. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  106. // Select not permitted
  107. if !didSelectItemEnable {
  108. return
  109. }
  110. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? NCActivityCollectionViewCell
  111. let activityPreview = activityPreviews[indexPath.row]
  112. if activityPreview.view == "trashbin" {
  113. var responder: UIResponder? = collectionView
  114. while !(responder is UIViewController) {
  115. responder = responder?.next
  116. if nil == responder {
  117. break
  118. }
  119. }
  120. if (responder as? UIViewController)!.navigationController != nil {
  121. if let viewController = UIStoryboard.init(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  122. if let result = NCManageDatabase.shared.getTrashItem(fileId: String(activityPreview.fileId), account: activityPreview.account) {
  123. viewController.blinkFileId = result.fileId
  124. viewController.trashPath = result.filePath
  125. (responder as? UIViewController)!.navigationController?.pushViewController(viewController, animated: true)
  126. } else {
  127. NCContentPresenter.shared.messageNotification("_error_", description: "_trash_file_not_found_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorInternalError)
  128. }
  129. }
  130. }
  131. return
  132. }
  133. if activityPreview.view == "files" && activityPreview.mimeType != "dir" {
  134. guard let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: activityPreview.account, idActivity: activityPreview.idActivity, id: String(activityPreview.fileId)) else {
  135. return
  136. }
  137. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "fileId == %@", activitySubjectRich.id)) {
  138. if let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView) {
  139. do {
  140. let attr = try FileManager.default.attributesOfItem(atPath: filePath)
  141. let fileSize = attr[FileAttributeKey.size] as! UInt64
  142. if fileSize > 0 {
  143. if let viewController = self.viewController {
  144. NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: cell?.imageView.image)
  145. }
  146. return
  147. }
  148. } catch {
  149. print("Error: \(error)")
  150. }
  151. }
  152. }
  153. var pathComponents = activityPreview.link.components(separatedBy: "?")
  154. pathComponents = pathComponents[1].components(separatedBy: "&")
  155. var serverUrlFileName = pathComponents[0].replacingOccurrences(of: "dir=", with: "").removingPercentEncoding!
  156. serverUrlFileName = appDelegate.urlBase + "/" + NCUtilityFileSystem.shared.getWebDAV(account: activityPreview.account) + serverUrlFileName + "/" + activitySubjectRich.name
  157. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(activitySubjectRich.id, fileNameView: activitySubjectRich.name)!
  158. NCUtility.shared.startActivityIndicator(backgroundView: (appDelegate.window?.rootViewController?.view)!, blurEffect: true)
  159. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  160. }, taskHandler: { (_) in
  161. }, progressHandler: { (_) in
  162. }) { (account, etag, date, lenght, allHeaderFields, error, errorCode, errorDescription) in
  163. if account == self.appDelegate.account && errorCode == 0 {
  164. let serverUrl = (serverUrlFileName as NSString).deletingLastPathComponent
  165. let fileName = (serverUrlFileName as NSString).lastPathComponent
  166. let serverUrlFileName = serverUrl + "/" + fileName
  167. NCNetworking.shared.readFile(serverUrlFileName: serverUrlFileName, account: activityPreview.account) { (account, metadata, errorCode, errorDescription) in
  168. NCUtility.shared.stopActivityIndicator()
  169. if account == self.appDelegate.account && errorCode == 0 {
  170. // move from id to oc:id + instanceid (ocId)
  171. let atPath = CCUtility.getDirectoryProviderStorage()! + "/" + activitySubjectRich.id
  172. let toPath = CCUtility.getDirectoryProviderStorage()! + "/" + metadata!.ocId
  173. CCUtility.moveFile(atPath: atPath, toPath: toPath)
  174. NCManageDatabase.shared.addMetadata(metadata!)
  175. if let viewController = self.viewController {
  176. NCViewer.shared.view(viewController: viewController, metadata: metadata!, metadatas: [metadata!], imageIcon: cell?.imageView.image)
  177. }
  178. }
  179. }
  180. } else {
  181. NCUtility.shared.stopActivityIndicator()
  182. }
  183. }
  184. }
  185. }
  186. }
  187. extension NCActivityTableViewCell: UICollectionViewDataSource {
  188. func numberOfSections(in collectionView: UICollectionView) -> Int {
  189. return 1
  190. }
  191. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  192. return activityPreviews.count
  193. }
  194. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  195. let cell: NCActivityCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! NCActivityCollectionViewCell
  196. cell.imageView.image = nil
  197. let activityPreview = activityPreviews[indexPath.row]
  198. let fileId = String(activityPreview.fileId)
  199. // Trashbin
  200. if activityPreview.view == "trashbin" {
  201. let source = activityPreview.source
  202. NCUtility.shared.convertSVGtoPNGWriteToUserData(svgUrlString: source, fileName: nil, width: 100, rewrite: false, account: appDelegate.account) { (imageNamePath) in
  203. if imageNamePath != nil {
  204. if let image = UIImage(contentsOfFile: imageNamePath!) {
  205. cell.imageView.image = image
  206. }
  207. } else {
  208. cell.imageView.image = UIImage.init(named: "file")
  209. }
  210. }
  211. } else {
  212. if activityPreview.isMimeTypeIcon {
  213. let source = activityPreview.source
  214. NCUtility.shared.convertSVGtoPNGWriteToUserData(svgUrlString: source, fileName: nil, width: 100, rewrite: false, account: appDelegate.account) { (imageNamePath) in
  215. if imageNamePath != nil {
  216. if let image = UIImage(contentsOfFile: imageNamePath!) {
  217. cell.imageView.image = image
  218. }
  219. } else {
  220. cell.imageView.image = UIImage.init(named: "file")
  221. }
  222. }
  223. } else {
  224. if let activitySubjectRich = NCManageDatabase.shared.getActivitySubjectRich(account: account, idActivity: idActivity, id: fileId) {
  225. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + activitySubjectRich.name
  226. if FileManager.default.fileExists(atPath: fileNamePath) {
  227. if let image = UIImage(contentsOfFile: fileNamePath) {
  228. cell.imageView.image = image
  229. }
  230. } else {
  231. NCCommunication.shared.downloadPreview(fileNamePathOrFileId: activityPreview.source, fileNamePreviewLocalPath: fileNamePath, widthPreview: 0, heightPreview: 0, etag: nil, useInternalEndpoint: false) { (account, imagePreview, imageIcon, imageOriginal, etag, errorCode, errorDescription) in
  232. if errorCode == 0 && imagePreview != nil {
  233. self.collectionView.reloadData()
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. return cell
  241. }
  242. }
  243. extension NCActivityTableViewCell: UICollectionViewDelegateFlowLayout {
  244. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  245. return CGSize(width: 50, height: 50)
  246. }
  247. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  248. return 20
  249. }
  250. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  251. return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  252. }
  253. }