NCOperationQueue.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 UIKit
  24. import Queuer
  25. import NextcloudKit
  26. import JGProgressHUD
  27. @objc class NCOperationQueue: NSObject {
  28. @objc public static let shared: NCOperationQueue = {
  29. let instance = NCOperationQueue()
  30. return instance
  31. }()
  32. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  33. // MARK: - Download file
  34. func download(metadata: tableMetadata, selector: String) {
  35. for case let operation as NCOperationDownload in appDelegate.downloadQueue.operations where operation.metadata.ocId == metadata.ocId { return }
  36. appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: selector))
  37. }
  38. // MARK: - Download Avatar
  39. func downloadAvatar(user: String, dispalyName: String?, fileName: String, cell: NCCellProtocol, view: UIView?, cellImageView: UIImageView?) {
  40. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  41. if let image = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) {
  42. cellImageView?.image = image
  43. cell.fileAvatarImageView?.image = image
  44. return
  45. }
  46. if let account = NCManageDatabase.shared.getActiveAccount() {
  47. cellImageView?.image = NCUtility.shared.loadUserImage(
  48. for: user,
  49. displayName: dispalyName,
  50. userBaseUrl: account)
  51. }
  52. for case let operation as NCOperationDownloadAvatar in appDelegate.downloadAvatarQueue.operations where operation.fileName == fileName { return }
  53. appDelegate.downloadAvatarQueue.addOperation(NCOperationDownloadAvatar(user: user, fileName: fileName, fileNameLocalPath: fileNameLocalPath, cell: cell, view: view, cellImageView: cellImageView))
  54. }
  55. // MARK: - Save Live Photo
  56. func saveLivePhoto(metadata: tableMetadata, metadataMOV: tableMetadata) {
  57. for case let operation as NCOperationSaveLivePhoto in appDelegate.saveLivePhotoQueue.operations where operation.metadata.fileName == metadata.fileName { return }
  58. appDelegate.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV))
  59. }
  60. }
  61. // MARK: -
  62. class NCOperationDownload: ConcurrentOperation {
  63. var metadata: tableMetadata
  64. var selector: String
  65. init(metadata: tableMetadata, selector: String) {
  66. self.metadata = tableMetadata.init(value: metadata)
  67. self.selector = selector
  68. }
  69. override func start() {
  70. guard !isCancelled else { return self.finish() }
  71. NCNetworking.shared.download(metadata: metadata, selector: self.selector) { _, _ in
  72. self.finish()
  73. }
  74. }
  75. }
  76. // MARK: -
  77. class NCOperationDownloadAvatar: ConcurrentOperation {
  78. var user: String
  79. var fileName: String
  80. var etag: String?
  81. var fileNameLocalPath: String
  82. var cell: NCCellProtocol!
  83. var view: UIView?
  84. var cellImageView: UIImageView?
  85. init(user: String, fileName: String, fileNameLocalPath: String, cell: NCCellProtocol, view: UIView?, cellImageView: UIImageView?) {
  86. self.user = user
  87. self.fileName = fileName
  88. self.fileNameLocalPath = fileNameLocalPath
  89. self.cell = cell
  90. self.view = view
  91. self.etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  92. self.cellImageView = cellImageView
  93. }
  94. override func start() {
  95. guard !isCancelled else { return self.finish() }
  96. NextcloudKit.shared.downloadAvatar(user: user,
  97. fileNameLocalPath: fileNameLocalPath,
  98. sizeImage: NCGlobal.shared.avatarSize,
  99. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  100. etag: self.etag,
  101. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, imageAvatar, _, etag, error in
  102. if error == .success, let imageAvatar = imageAvatar, let etag = etag {
  103. NCManageDatabase.shared.addAvatar(fileName: self.fileName, etag: etag)
  104. DispatchQueue.main.async {
  105. if self.user == self.cell.fileUser, let avatarImageView = self.cellImageView {
  106. UIView.transition(with: avatarImageView,
  107. duration: 0.75,
  108. options: .transitionCrossDissolve,
  109. animations: { avatarImageView.image = imageAvatar },
  110. completion: nil)
  111. } else {
  112. if self.view is UICollectionView {
  113. (self.view as? UICollectionView)?.reloadData()
  114. } else if self.view is UITableView {
  115. (self.view as? UITableView)?.reloadData()
  116. }
  117. }
  118. }
  119. } else if error.errorCode == NCGlobal.shared.errorNotModified {
  120. NCManageDatabase.shared.setAvatarLoaded(fileName: self.fileName)
  121. }
  122. self.finish()
  123. }
  124. }
  125. }
  126. // MARK: -
  127. class NCOperationSaveLivePhoto: ConcurrentOperation {
  128. var metadata: tableMetadata
  129. var metadataMOV: tableMetadata
  130. let hud = JGProgressHUD()
  131. let appDelegate = UIApplication.shared.delegate as? AppDelegate
  132. init(metadata: tableMetadata, metadataMOV: tableMetadata) {
  133. self.metadata = tableMetadata.init(value: metadata)
  134. self.metadataMOV = tableMetadata.init(value: metadataMOV)
  135. }
  136. override func start() {
  137. guard !isCancelled else { return self.finish() }
  138. DispatchQueue.main.async {
  139. self.hud.indicatorView = JGProgressHUDRingIndicatorView()
  140. if let indicatorView = self.hud.indicatorView as? JGProgressHUDRingIndicatorView {
  141. indicatorView.ringWidth = 1.5
  142. }
  143. self.hud.textLabel.text = NSLocalizedString("_download_image_", comment: "")
  144. self.hud.detailTextLabel.text = self.metadata.fileName
  145. self.hud.show(in: (self.appDelegate?.window?.rootViewController?.view)!)
  146. }
  147. NCNetworking.shared.download(metadata: metadata, selector: "", notificationCenterProgressTask: false, checkfileProviderStorageExists: true) { _ in
  148. } progressHandler: { progress in
  149. self.hud.progress = Float(progress.fractionCompleted)
  150. } completion: { _, error in
  151. guard error == .success else {
  152. DispatchQueue.main.async {
  153. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  154. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  155. self.hud.dismiss()
  156. }
  157. return self.finish()
  158. }
  159. NCNetworking.shared.download(metadata: self.metadataMOV, selector: "", notificationCenterProgressTask: false, checkfileProviderStorageExists: true) { _ in
  160. DispatchQueue.main.async {
  161. self.hud.textLabel.text = NSLocalizedString("_download_video_", comment: "")
  162. self.hud.detailTextLabel.text = self.metadataMOV.fileName
  163. }
  164. } progressHandler: { progress in
  165. self.hud.progress = Float(progress.fractionCompleted)
  166. } completion: { _, error in
  167. guard error == .success else {
  168. DispatchQueue.main.async {
  169. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  170. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  171. self.hud.dismiss()
  172. }
  173. return self.finish()
  174. }
  175. self.saveLivePhotoToDisk(metadata: self.metadata, metadataMov: self.metadataMOV)
  176. }
  177. }
  178. }
  179. func saveLivePhotoToDisk(metadata: tableMetadata, metadataMov: tableMetadata) {
  180. let fileNameImage = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
  181. let fileNameMov = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadataMov.ocId, fileNameView: metadataMov.fileNameView)!)
  182. DispatchQueue.main.async {
  183. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_", comment: "")
  184. self.hud.detailTextLabel.text = ""
  185. }
  186. NCLivePhoto.generate(from: fileNameImage, videoURL: fileNameMov, progress: { progress in
  187. self.hud.progress = Float(progress)
  188. }, completion: { _, resources in
  189. if resources != nil {
  190. NCLivePhoto.saveToLibrary(resources!) { result in
  191. DispatchQueue.main.async {
  192. if !result {
  193. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  194. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  195. } else {
  196. self.hud.indicatorView = JGProgressHUDSuccessIndicatorView()
  197. self.hud.textLabel.text = NSLocalizedString("_success_", comment: "")
  198. }
  199. self.hud.dismiss()
  200. }
  201. return self.finish()
  202. }
  203. } else {
  204. DispatchQueue.main.async {
  205. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  206. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  207. self.hud.dismiss()
  208. }
  209. return self.finish()
  210. }
  211. })
  212. }
  213. }