NCTransfers.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // NCTransfers.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/09/2020.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@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 UIKit
  24. import NCCommunication
  25. class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate {
  26. var metadataTemp: tableMetadata?
  27. // MARK: - View Life Cycle
  28. required init?(coder aDecoder: NSCoder) {
  29. super.init(coder: aDecoder)
  30. titleCurrentFolder = NSLocalizedString("_transfers_", comment: "")
  31. layoutKey = NCGlobal.shared.layoutViewTransfers
  32. enableSearchBar = false
  33. headerMenuButtonsCommand = false
  34. headerMenuButtonsView = false
  35. headerRichWorkspaceDisable = true
  36. emptyImage = UIImage(named: "arrow.left.arrow.right")?.image(color: .gray, size: UIScreen.main.bounds.width)
  37. emptyTitle = "_no_transfer_"
  38. emptyDescription = "_no_transfer_sub_"
  39. }
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. listLayout.itemHeight = 105
  43. collectionView?.collectionViewLayout = listLayout
  44. self.navigationItem.title = titleCurrentFolder
  45. serverUrl = appDelegate.activeServerUrl
  46. }
  47. override func viewWillAppear(_ animated: Bool) {
  48. super.viewWillAppear(animated)
  49. collectionView?.collectionViewLayout = listLayout
  50. }
  51. override func setNavigationItem() {
  52. self.navigationItem.rightBarButtonItem = nil
  53. self.navigationItem.leftBarButtonItem = nil
  54. }
  55. // MARK: - NotificationCenter
  56. override func applicationWillEnterForeground(_ notification: NSNotification) {
  57. collectionView?.collectionViewLayout = listLayout
  58. reloadDataSource()
  59. }
  60. override func downloadStartFile(_ notification: NSNotification) {
  61. reloadDataSource()
  62. }
  63. override func downloadedFile(_ notification: NSNotification) {
  64. reloadDataSource()
  65. }
  66. override func downloadCancelFile(_ notification: NSNotification) {
  67. reloadDataSource()
  68. }
  69. override func uploadStartFile(_ notification: NSNotification) {
  70. reloadDataSource()
  71. }
  72. override func uploadedFile(_ notification: NSNotification) {
  73. reloadDataSource()
  74. }
  75. override func uploadCancelFile(_ notification: NSNotification) {
  76. reloadDataSource()
  77. }
  78. override func triggerProgressTask(_ notification: NSNotification) {
  79. guard let userInfo = notification.userInfo as NSDictionary?,
  80. let progressNumber = userInfo["progress"] as? NSNumber,
  81. let totalBytes = userInfo["totalBytes"] as? Int64,
  82. let totalBytesExpected = userInfo["totalBytesExpected"] as? Int64,
  83. let ocId = userInfo["ocId"] as? String,
  84. let (indexPath, _) = self.dataSource.getIndexPathMetadata(ocId: ocId) as? (IndexPath, NCMetadatasForSection?)
  85. else {
  86. return
  87. }
  88. let status = userInfo["status"] as? Int ?? NCGlobal.shared.metadataStatusNormal
  89. if let cell = collectionView?.cellForItem(at: indexPath) {
  90. let cell = cell as! NCTransferCell
  91. if progressNumber.floatValue == 1 {
  92. cell.progressView?.isHidden = true
  93. cell.progressView?.progress = .zero
  94. cell.buttonMore.isHidden = true
  95. cell.labelInfo.text = ""
  96. } else if progressNumber.floatValue > 0 {
  97. cell.progressView?.isHidden = false
  98. cell.progressView?.progress = progressNumber.floatValue
  99. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  100. if status == NCGlobal.shared.metadataStatusInDownload {
  101. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  102. } else if status == NCGlobal.shared.metadataStatusInUpload {
  103. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  104. }
  105. }
  106. }
  107. }
  108. // MARK: TAP EVENT
  109. override func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  110. if gestureRecognizer.state != .began { return }
  111. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
  112. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  113. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_all_task_", comment: ""), style: .default, handler: { _ in
  114. NCNetworking.shared.cancelAllTransfer(account: self.appDelegate.account) {
  115. self.reloadDataSource()
  116. }
  117. }))
  118. self.present(alertController, animated: true, completion: nil)
  119. }
  120. override func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  121. if gestureRecognizer.state != .began { return }
  122. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) {
  123. metadataTemp = metadata
  124. let touchPoint = gestureRecognizer.location(in: collectionView)
  125. becomeFirstResponder()
  126. let startTaskItem = UIMenuItem(title: NSLocalizedString("_force_start_", comment: ""), action: #selector(startTask(_:)))
  127. UIMenuController.shared.menuItems = [startTaskItem]
  128. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  129. UIMenuController.shared.setMenuVisible(true, animated: true)
  130. }
  131. }
  132. override func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) { }
  133. @objc func startTask(_ notification: Any) {
  134. guard let metadata = metadataTemp else { return }
  135. metadata.status = NCGlobal.shared.metadataStatusInUpload
  136. metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
  137. NCManageDatabase.shared.addMetadata(metadata)
  138. NCNetworking.shared.upload(metadata: metadata) { } completion: { _, _ in }
  139. }
  140. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  141. if action != #selector(startTask(_:)) { return false }
  142. guard let metadata = metadataTemp else { return false }
  143. if metadata.e2eEncrypted { return false }
  144. if metadata.status == NCGlobal.shared.metadataStatusWaitUpload || metadata.status == NCGlobal.shared.metadataStatusInUpload || metadata.status == NCGlobal.shared.metadataStatusUploading {
  145. return true
  146. }
  147. return false
  148. }
  149. // MARK: - Collection View
  150. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  151. // nothing
  152. }
  153. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  154. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  155. return collectionView.dequeueReusableCell(withReuseIdentifier: "transferCell", for: indexPath) as! NCTransferCell
  156. }
  157. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "transferCell", for: indexPath) as! NCTransferCell
  158. cell.delegate = self
  159. cell.fileObjectId = metadata.ocId
  160. cell.fileUser = metadata.ownerId
  161. cell.indexPath = indexPath
  162. cell.imageItem.image = nil
  163. cell.imageItem.backgroundColor = nil
  164. cell.labelTitle.text = metadata.fileNameView
  165. cell.labelTitle.textColor = NCBrandColor.shared.label
  166. let serverUrlHome = NCUtilityFileSystem.shared.getHomeServer(account: metadata.account)
  167. var pathText = metadata.serverUrl.replacingOccurrences(of: serverUrlHome, with: "")
  168. if pathText == "" { pathText = "/" }
  169. cell.labelPath.text = pathText
  170. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  171. cell.progressView.progress = 0.0
  172. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  173. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  174. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)) {
  175. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  176. }
  177. if cell.imageItem.image == nil {
  178. cell.imageItem.image = NCBrandColor.cacheImages.file
  179. }
  180. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  181. // Transfer
  182. var progress: Float = 0.0
  183. var totalBytes: Int64 = 0
  184. if let progressType = appDelegate.listProgress[metadata.ocId] {
  185. progress = progressType.progress
  186. totalBytes = progressType.totalBytes
  187. }
  188. if metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status == NCGlobal.shared.metadataStatusUploading {
  189. cell.progressView.isHidden = false
  190. cell.progressView.progress = progress
  191. } else {
  192. cell.progressView.isHidden = true
  193. }
  194. // Write status on Label Info
  195. switch metadata.status {
  196. case NCGlobal.shared.metadataStatusWaitDownload:
  197. cell.labelStatus.text = NSLocalizedString("_status_wait_download_", comment: "")
  198. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  199. break
  200. case NCGlobal.shared.metadataStatusInDownload:
  201. cell.labelStatus.text = NSLocalizedString("_status_in_download_", comment: "")
  202. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  203. break
  204. case NCGlobal.shared.metadataStatusDownloading:
  205. cell.labelStatus.text = NSLocalizedString("_status_downloading_", comment: "")
  206. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  207. break
  208. case NCGlobal.shared.metadataStatusWaitUpload:
  209. cell.labelStatus.text = NSLocalizedString("_status_wait_upload_", comment: "")
  210. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  211. break
  212. case NCGlobal.shared.metadataStatusInUpload:
  213. cell.labelStatus.text = NSLocalizedString("_status_in_upload_", comment: "")
  214. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  215. break
  216. case NCGlobal.shared.metadataStatusUploading:
  217. cell.labelStatus.text = NSLocalizedString("_status_uploading_", comment: "")
  218. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  219. break
  220. case NCGlobal.shared.metadataStatusUploadError:
  221. cell.labelStatus.text = NSLocalizedString("_status_upload_error_", comment: "")
  222. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  223. break
  224. default:
  225. cell.labelStatus.text = ""
  226. cell.labelInfo.text = ""
  227. break
  228. }
  229. cell.accessibilityLabel = metadata.fileNameView + ", " + (cell.labelInfo.text ?? "")
  230. // Remove last separator
  231. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  232. cell.separator.isHidden = true
  233. } else {
  234. cell.separator.isHidden = false
  235. }
  236. return cell
  237. }
  238. // MARK: - DataSource + NC Endpoint
  239. override func reloadDataSource() {
  240. super.reloadDataSource()
  241. metadatasSource = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "status != %i", NCGlobal.shared.metadataStatusNormal), page: 1, limit: 100, sorted: "sessionTaskIdentifier", ascending: false)
  242. self.dataSource = NCDataSource(metadatasSource: metadatasSource, account: self.appDelegate.account)
  243. refreshControl.endRefreshing()
  244. collectionView.reloadData()
  245. }
  246. override func reloadDataSourceNetwork(forced: Bool = false) {
  247. super.reloadDataSourceNetwork(forced: forced)
  248. reloadDataSource()
  249. }
  250. }