NCTransfers.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. required init?(coder aDecoder: NSCoder) {
  27. super.init(coder: aDecoder)
  28. appDelegate.activeTransfers = self
  29. titleCurrentFolder = NSLocalizedString("_transfers_", comment: "")
  30. layoutKey = k_layout_view_transfers
  31. enableSearchBar = false
  32. DZNimage = CCGraphics.changeThemingColorImage(UIImage.init(named: "load"), width: 300, height: 300, color: NCBrandColor.sharedInstance.brandElement)
  33. DZNtitle = "_no_transfer_"
  34. DZNdescription = "_no_transfer_sub_"
  35. }
  36. override func viewWillAppear(_ animated: Bool) {
  37. super.viewWillAppear(animated)
  38. }
  39. override func setNavigationItem() {
  40. self.navigationItem.rightBarButtonItem = nil
  41. self.navigationItem.leftBarButtonItem = nil
  42. }
  43. // MARK: - NotificationCenter
  44. override func downloadStartFile(_ notification: NSNotification) {
  45. if self.view?.window == nil { return }
  46. reloadDataSource()
  47. }
  48. override func downloadedFile(_ notification: NSNotification) {
  49. if self.view?.window == nil { return }
  50. reloadDataSource()
  51. }
  52. override func downloadCancelFile(_ notification: NSNotification) {
  53. if self.view?.window == nil { return }
  54. reloadDataSource()
  55. }
  56. override func uploadStartFile(_ notification: NSNotification) {
  57. if self.view?.window == nil { return }
  58. if let userInfo = notification.userInfo as NSDictionary? {
  59. if let metadata = userInfo["metadata"] as? tableMetadata {
  60. if let row = dataSource?.addMetadata(metadata) {
  61. let indexPath = IndexPath(row: row, section: 0)
  62. collectionView?.performBatchUpdates({
  63. collectionView?.insertItems(at: [indexPath])
  64. }, completion: { (_) in
  65. self.collectionView?.reloadData()
  66. })
  67. }
  68. }
  69. }
  70. }
  71. override func uploadedFile(_ notification: NSNotification) {
  72. if self.view?.window == nil { return }
  73. if let userInfo = notification.userInfo as NSDictionary? {
  74. if let metadata = userInfo["metadata"] as? tableMetadata, let ocIdTemp = userInfo["ocIdTemp"] as? String, let errorCode = userInfo["errorCode"] as? Int {
  75. if errorCode == 0 {
  76. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  77. let indexPath = IndexPath(row: row, section: 0)
  78. collectionView?.performBatchUpdates({
  79. collectionView?.deleteItems(at: [indexPath])
  80. }, completion: { (_) in
  81. self.collectionView?.reloadData()
  82. })
  83. } else {
  84. reloadDataSource()
  85. }
  86. } else if errorCode != NSURLErrorCancelled {
  87. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp) {
  88. let indexPath = IndexPath(row: row, section: 0)
  89. collectionView?.performBatchUpdates({
  90. collectionView?.reloadItems(at: [indexPath])
  91. }, completion: { (_) in
  92. self.collectionView?.reloadData()
  93. })
  94. } else {
  95. reloadDataSource()
  96. }
  97. }
  98. }
  99. }
  100. }
  101. override func uploadCancelFile(_ notification: NSNotification) {
  102. if self.view?.window == nil { return }
  103. if let userInfo = notification.userInfo as NSDictionary? {
  104. if let metadata = userInfo["metadata"] as? tableMetadata {
  105. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  106. let indexPath = IndexPath(row: row, section: 0)
  107. collectionView?.performBatchUpdates({
  108. collectionView?.deleteItems(at: [indexPath])
  109. }, completion: { (_) in
  110. self.collectionView?.reloadData()
  111. })
  112. } else {
  113. self.reloadDataSource()
  114. }
  115. }
  116. }
  117. }
  118. // MARK: - Collection View
  119. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  120. super.collectionView(collectionView, didSelectItemAt: indexPath)
  121. }
  122. override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  123. return CGSize(width: collectionView.frame.width, height: 0)
  124. }
  125. // MARK: - NC API & Algorithm
  126. override func reloadDataSource() {
  127. super.reloadDataSource()
  128. var sort: String
  129. var ascending: Bool
  130. var directoryOnTop: Bool
  131. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey)
  132. metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "(session CONTAINS 'upload') OR (session CONTAINS 'download')"), page: 1, limit: 100, sorted: "sessionTaskIdentifier", ascending: false)
  133. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: false)
  134. refreshControl.endRefreshing()
  135. collectionView.reloadData()
  136. }
  137. override func reloadDataSourceNetwork() {
  138. super.reloadDataSourceNetwork()
  139. reloadDataSource()
  140. }
  141. }