NCTransfers.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 {
  26. var metadataTemp: tableMetadata?
  27. required init?(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. appDelegate.activeTransfers = self
  30. titleCurrentFolder = NSLocalizedString("_transfers_", comment: "")
  31. layoutKey = k_layout_view_transfers
  32. enableSearchBar = false
  33. DZNimage = CCGraphics.changeThemingColorImage(UIImage.init(named: "load"), width: 300, height: 300, color: .gray)
  34. DZNtitle = "_no_transfer_"
  35. DZNdescription = "_no_transfer_sub_"
  36. }
  37. override func setNavigationItem() {
  38. self.navigationItem.rightBarButtonItem = nil
  39. self.navigationItem.leftBarButtonItem = nil
  40. }
  41. // MARK: - NotificationCenter
  42. override func downloadStartFile(_ notification: NSNotification) {
  43. if self.view?.window == nil { return }
  44. reloadDataSource()
  45. }
  46. override func downloadedFile(_ notification: NSNotification) {
  47. if self.view?.window == nil { return }
  48. reloadDataSource()
  49. }
  50. override func downloadCancelFile(_ notification: NSNotification) {
  51. if self.view?.window == nil { return }
  52. reloadDataSource()
  53. }
  54. override func uploadStartFile(_ notification: NSNotification) {
  55. if self.view?.window == nil { return }
  56. if let userInfo = notification.userInfo as NSDictionary? {
  57. if let metadata = userInfo["metadata"] as? tableMetadata {
  58. if let row = dataSource.addMetadata(metadata) {
  59. let indexPath = IndexPath(row: row, section: 0)
  60. collectionView?.performBatchUpdates({
  61. collectionView?.insertItems(at: [indexPath])
  62. }, completion: { (_) in
  63. self.reloadDataSource()
  64. })
  65. }
  66. }
  67. }
  68. }
  69. override func uploadedFile(_ notification: NSNotification) {
  70. if self.view?.window == nil { return }
  71. if let userInfo = notification.userInfo as NSDictionary? {
  72. if let metadata = userInfo["metadata"] as? tableMetadata, let ocIdTemp = userInfo["ocIdTemp"] as? String, let errorCode = userInfo["errorCode"] as? Int {
  73. if errorCode == 0 {
  74. if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
  75. let indexPath = IndexPath(row: row, section: 0)
  76. collectionView?.performBatchUpdates({
  77. collectionView?.deleteItems(at: [indexPath])
  78. }, completion: { (_) in
  79. self.collectionView?.reloadData()
  80. })
  81. } else {
  82. reloadDataSource()
  83. }
  84. } else if errorCode != NSURLErrorCancelled {
  85. if let row = dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp) {
  86. let indexPath = IndexPath(row: row, section: 0)
  87. collectionView?.performBatchUpdates({
  88. collectionView?.reloadItems(at: [indexPath])
  89. }, completion: { (_) in
  90. self.collectionView?.reloadData()
  91. })
  92. } else {
  93. reloadDataSource()
  94. }
  95. }
  96. }
  97. }
  98. }
  99. override func uploadCancelFile(_ notification: NSNotification) {
  100. if self.view?.window == nil { return }
  101. if let userInfo = notification.userInfo as NSDictionary? {
  102. if let metadata = userInfo["metadata"] as? tableMetadata {
  103. if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
  104. let indexPath = IndexPath(row: row, section: 0)
  105. collectionView?.performBatchUpdates({
  106. collectionView?.deleteItems(at: [indexPath])
  107. }, completion: { (_) in
  108. self.collectionView?.reloadData()
  109. })
  110. } else {
  111. self.reloadDataSource()
  112. }
  113. }
  114. }
  115. }
  116. // MARK: TAP EVENT
  117. override func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  118. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
  119. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  120. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_all_task_", comment: ""), style: .default, handler: { action in
  121. NCNetworking.shared.cancelAllTransfer(account: self.appDelegate.account) {
  122. self.reloadDataSource()
  123. }
  124. }))
  125. self.present(alertController, animated: true, completion: nil)
  126. }
  127. override func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  128. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) {
  129. metadataTemp = metadata
  130. let touchPoint = gestureRecognizer.location(in: collectionView)
  131. becomeFirstResponder()
  132. let startTaskItem = UIMenuItem.init(title: NSLocalizedString("_force_start_", comment: ""), action: #selector(startTask(_:)))
  133. UIMenuController.shared.menuItems = [startTaskItem]
  134. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  135. UIMenuController.shared.setMenuVisible(true, animated: true)
  136. }
  137. }
  138. override func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) { }
  139. @objc func startTask(_ notification: Any) {
  140. guard let metadata = metadataTemp else { return }
  141. metadata.status = Int(k_metadataStatusInUpload)
  142. metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
  143. NCManageDatabase.sharedInstance.addMetadata(metadata)
  144. NCNetworking.shared.upload(metadata: metadata) { (_, _) in }
  145. }
  146. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  147. if action != #selector(startTask(_:)) { return false }
  148. guard let metadata = metadataTemp else { return false }
  149. if metadata.e2eEncrypted { return false }
  150. if metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading {
  151. return true
  152. }
  153. return false
  154. }
  155. // MARK: - Collection View
  156. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  157. // nothing
  158. }
  159. override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  160. return CGSize(width: collectionView.frame.width, height: 0)
  161. }
  162. // MARK: - DataSource + NC Endpoint
  163. override func reloadDataSource() {
  164. super.reloadDataSource()
  165. metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "(session CONTAINS 'upload') OR (session CONTAINS 'download')"), page: 1, limit: 100, sorted: "sessionTaskIdentifier", ascending: false)
  166. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, directoryOnTop: false, favoriteOnTop: false, filterLivePhoto: false)
  167. refreshControl.endRefreshing()
  168. collectionView.reloadData()
  169. }
  170. override func reloadDataSourceNetwork(forced: Bool = false) {
  171. super.reloadDataSourceNetwork(forced: forced)
  172. reloadDataSource()
  173. }
  174. }