NCTransfers.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.reloadDataSource()
  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. self.reloadDataSource()
  126. }
  127. }))
  128. self.present(alertController, animated: true, completion: nil)
  129. }
  130. override func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  131. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) {
  132. metadataTemp = metadata
  133. let touchPoint = gestureRecognizer.location(in: collectionView)
  134. becomeFirstResponder()
  135. let startTaskItem = UIMenuItem.init(title: NSLocalizedString("_force_start_", comment: ""), action: #selector(startTask(_:)))
  136. UIMenuController.shared.menuItems = [startTaskItem]
  137. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  138. UIMenuController.shared.setMenuVisible(true, animated: true)
  139. }
  140. }
  141. override var canBecomeFirstResponder: Bool {
  142. return true
  143. }
  144. @objc func startTask(_ notification: Any) {
  145. guard let metadata = metadataTemp else { return }
  146. metadata.status = Int(k_metadataStatusInUpload)
  147. metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
  148. NCManageDatabase.sharedInstance.addMetadata(metadata)
  149. NCNetworking.shared.upload(metadata: metadata) { (_, _) in }
  150. }
  151. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  152. if action != #selector(startTask(_:)) { return false }
  153. guard let metadata = metadataTemp else { return false }
  154. if metadata.e2eEncrypted { return false }
  155. if metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading {
  156. return true
  157. }
  158. return false
  159. }
  160. // MARK: - Collection View
  161. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  162. // nothing
  163. }
  164. override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  165. return CGSize(width: collectionView.frame.width, height: 0)
  166. }
  167. // MARK: - NC API & Algorithm
  168. override func reloadDataSource() {
  169. super.reloadDataSource()
  170. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey)
  171. metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "(session CONTAINS 'upload') OR (session CONTAINS 'download')"), page: 1, limit: 100, sorted: "sessionTaskIdentifier", ascending: false)
  172. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: "sessionTaskIdentifier", ascending: true, directoryOnTop: false, filterLivePhoto: false)
  173. refreshControl.endRefreshing()
  174. collectionView.reloadData()
  175. }
  176. override func reloadDataSourceNetwork() {
  177. super.reloadDataSourceNetwork()
  178. reloadDataSource()
  179. }
  180. }