NCTransfers.swift 9.2 KB

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