NCTransfers.swift 12 KB

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