NCTransfers.swift 6.6 KB

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