NCTransfers.swift 12 KB

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