NCOperationQueue.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // NCOperationQueue.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/06/2020.
  6. // Copyright © 2020 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 Queuer
  25. import NCCommunication
  26. @objc class NCOperationQueue: NSObject {
  27. @objc public static let shared: NCOperationQueue = {
  28. let instance = NCOperationQueue()
  29. return instance
  30. }()
  31. let downloadQueue = Queuer(name: "downloadQueue", maxConcurrentOperationCount: 5, qualityOfService: .default)
  32. let downloadThumbnailQueue = Queuer(name: "downloadThumbnailQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
  33. @objc func download(metadata: tableMetadata, selector: String, setFavorite: Bool) {
  34. downloadQueue.addOperation(NCOperationDownload.init(metadata: metadata, selector: selector, setFavorite: setFavorite))
  35. }
  36. @objc func downloadThumbnail(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  37. if metadata.hasPreview && (!CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileName) || metadata.typeFile == k_metadataTypeFile_document) {
  38. downloadThumbnailQueue.addOperation(NCOperationDownloadThumbnail.init(metadata: metadata, activeUrl: activeUrl, view: view, indexPath: indexPath))
  39. }
  40. }
  41. }
  42. class NCOperationDownload: ConcurrentOperation {
  43. private var metadata: tableMetadata
  44. private var selector: String
  45. private var setFavorite: Bool
  46. init(metadata: tableMetadata, selector: String, setFavorite: Bool) {
  47. self.metadata = metadata
  48. self.selector = selector
  49. self.setFavorite = setFavorite
  50. }
  51. override func start() {
  52. NCNetworking.shared.download(metadata: self.metadata, selector: self.selector, setFavorite: self.setFavorite) { (_) in
  53. self.finish()
  54. }
  55. }
  56. }
  57. class NCOperationDownloadThumbnail: ConcurrentOperation {
  58. private var metadata: tableMetadata
  59. private var activeUrl: String
  60. private var view: Any
  61. private var indexPath: IndexPath
  62. init(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  63. self.metadata = metadata
  64. self.activeUrl = activeUrl
  65. self.view = view
  66. self.indexPath = indexPath
  67. }
  68. override func start() {
  69. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  70. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  71. NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNameLocalPath: fileNameLocalPath, width: Int(k_sizePreview), height: Int(k_sizePreview)) { (account, data, errorCode, errorMessage) in
  72. if errorCode == 0 && data != nil {
  73. if let image = UIImage.init(data: data!) {
  74. if self.view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  75. if let cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) {
  76. if cell is NCListCell {
  77. (cell as! NCListCell).imageItem.image = image
  78. } else if cell is NCGridCell {
  79. (cell as! NCGridCell).imageItem.image = image
  80. } else if cell is NCGridMediaCell {
  81. (cell as! NCGridMediaCell).imageItem.image = image
  82. }
  83. }
  84. }
  85. if self.view is UITableView && CCUtility.fileProviderStorageIconExists(self.metadata.ocId, fileNameView: self.metadata.fileName) && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  86. if let cell = (self.view as! UITableView).cellForRow(at: self.indexPath) {
  87. if cell is CCCellMainTransfer {
  88. (cell as! CCCellMainTransfer).file.image = image
  89. } else if cell is CCCellMain {
  90. (cell as! CCCellMain).file.image = image
  91. }
  92. }
  93. }
  94. }
  95. }
  96. self.finish()
  97. }
  98. }
  99. }