NCTransfers.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 UIKit
  24. import NextcloudKit
  25. class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate {
  26. var metadataTemp: tableMetadata?
  27. required init?(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. titleCurrentFolder = NSLocalizedString("_transfers_", comment: "")
  30. layoutKey = NCGlobal.shared.layoutViewTransfers
  31. enableSearchBar = false
  32. headerRichWorkspaceDisable = true
  33. headerMenuTransferView = false
  34. emptyImageName = "arrow.left.arrow.right.circle"
  35. emptyTitle = "_no_transfer_"
  36. emptyDescription = "_no_transfer_sub_"
  37. }
  38. // MARK: - View Life Cycle
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. listLayout.itemHeight = 105
  42. self.database.setLayoutForView(account: session.account, key: layoutKey, serverUrl: serverUrl, layout: NCGlobal.shared.layoutList)
  43. self.navigationItem.title = titleCurrentFolder
  44. }
  45. override func viewWillAppear(_ animated: Bool) {
  46. super.viewWillAppear(animated)
  47. reloadDataSource()
  48. }
  49. override func setNavigationLeftItems() {
  50. self.navigationItem.rightBarButtonItem = nil
  51. self.navigationItem.leftBarButtonItem = nil
  52. }
  53. // MARK: - NotificationCenter
  54. override func deleteFile(_ notification: NSNotification) {
  55. reloadDataSource()
  56. }
  57. override func renameFile(_ notification: NSNotification) {
  58. reloadDataSource()
  59. }
  60. override func createFolder(_ notification: NSNotification) {
  61. reloadDataSource()
  62. }
  63. override func copyMoveFile(_ notification: NSNotification) {
  64. reloadDataSource()
  65. }
  66. override func downloadStartFile(_ notification: NSNotification) {
  67. reloadDataSource()
  68. }
  69. override func downloadedFile(_ notification: NSNotification) {
  70. reloadDataSource()
  71. }
  72. override func downloadCancelFile(_ notification: NSNotification) {
  73. reloadDataSource()
  74. }
  75. override func uploadStartFile(_ notification: NSNotification) {
  76. reloadDataSource()
  77. }
  78. override func uploadedFile(_ notification: NSNotification) {
  79. reloadDataSource()
  80. }
  81. override func uploadedLivePhoto(_ notification: NSNotification) {
  82. reloadDataSource()
  83. }
  84. override func uploadCancelFile(_ notification: NSNotification) {
  85. reloadDataSource()
  86. }
  87. override func triggerProgressTask(_ notification: NSNotification) {
  88. guard let userInfo = notification.userInfo as NSDictionary?,
  89. let progressNumber = userInfo["progress"] as? NSNumber,
  90. let totalBytes = userInfo["totalBytes"] as? Int64,
  91. let totalBytesExpected = userInfo["totalBytesExpected"] as? Int64,
  92. let ocId = userInfo["ocId"] as? String,
  93. let ocIdTransfer = userInfo["ocIdTransfer"] as? String,
  94. let session = userInfo["session"] as? String
  95. else { return }
  96. let chunk: Int = userInfo["chunk"] as? Int ?? 0
  97. let e2eEncrypted: Bool = userInfo["e2eEncrypted"] as? Bool ?? false
  98. NCTransferProgress.shared.append(NCTransferProgress.Transfer(ocId: ocId, ocIdTransfer: ocIdTransfer, session: session, chunk: chunk, e2eEncrypted: e2eEncrypted, progressNumber: progressNumber, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected))
  99. DispatchQueue.main.async {
  100. for case let cell as NCTransferCell in self.collectionView.visibleCells {
  101. if cell.fileOcIdTransfer == ocIdTransfer {
  102. cell.setProgress(progress: progressNumber.floatValue)
  103. cell.fileInfoLabel?.text = self.utilityFileSystem.transformedSize(totalBytesExpected) + " - " + self.utilityFileSystem.transformedSize(totalBytes)
  104. }
  105. }
  106. }
  107. }
  108. // MARK: TAP EVENT
  109. override func tapMoreGridItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any) {
  110. guard let metadata = self.database.getMetadataFromOcIdAndocIdTransfer(ocIdTransfer) else { return }
  111. NCNetworking.shared.cancelTask(metadata: metadata)
  112. }
  113. override func longPressMoreListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) {
  114. if gestureRecognizer.state != .began { return }
  115. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
  116. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  117. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_all_task_", comment: ""), style: .default, handler: { _ in
  118. NCNetworking.shared.cancelAllTask()
  119. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  120. self.reloadDataSource()
  121. }
  122. }))
  123. self.present(alertController, animated: true, completion: nil)
  124. }
  125. override func longPressListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) {
  126. if gestureRecognizer.state != .began { return }
  127. if let metadata = self.database.getMetadataFromOcIdAndocIdTransfer(ocIdTransfer) {
  128. metadataTemp = metadata
  129. let touchPoint = gestureRecognizer.location(in: collectionView)
  130. becomeFirstResponder()
  131. let startTaskItem = UIMenuItem(title: NSLocalizedString("_force_start_", comment: ""), action: #selector(startTask(_:)))
  132. UIMenuController.shared.menuItems = [startTaskItem]
  133. UIMenuController.shared.showMenu(from: collectionView, rect: CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0))
  134. }
  135. }
  136. override func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) { }
  137. @objc func startTask(_ notification: Any) {
  138. guard let metadata = metadataTemp else { return }
  139. let cameraRoll = NCCameraRoll()
  140. cameraRoll.extractCameraRoll(from: metadata) { metadatas in
  141. for metadata in metadatas {
  142. if let metadata = self.database.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusUploading) {
  143. NCTransferProgress.shared.clearCountError(ocIdTransfer: metadata.ocIdTransfer)
  144. NCNetworking.shared.upload(metadata: metadata)
  145. }
  146. }
  147. }
  148. }
  149. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  150. if action != #selector(startTask(_:)) { return false }
  151. guard let metadata = metadataTemp else { return false }
  152. if metadata.isDirectoryE2EE { return false }
  153. if metadata.isUpload {
  154. return true
  155. }
  156. return false
  157. }
  158. // MARK: - Collection View
  159. override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  160. return nil
  161. }
  162. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  163. // nothing
  164. }
  165. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  166. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "transferCell", for: indexPath) as? NCTransferCell)!
  167. guard let metadata = self.dataSource.getResultMetadata(indexPath: indexPath) else {
  168. return cell
  169. }
  170. let transfer = NCTransferProgress.shared.get(ocId: metadata.ocId, ocIdTransfer: metadata.ocIdTransfer, session: metadata.session)
  171. cell.delegate = self
  172. cell.fileOcId = metadata.ocId
  173. cell.fileOcIdTransfer = metadata.ocIdTransfer
  174. cell.fileUser = metadata.ownerId
  175. cell.filePreviewImageView?.image = imageCache.getImageFile()
  176. cell.filePreviewImageView?.backgroundColor = nil
  177. cell.labelTitle.text = metadata.fileNameView
  178. cell.labelTitle.textColor = NCBrandColor.shared.textColor
  179. let serverUrlHome = utilityFileSystem.getHomeServer(session: session)
  180. var pathText = metadata.serverUrl.replacingOccurrences(of: serverUrlHome, with: "")
  181. if pathText.isEmpty { pathText = "/" }
  182. cell.labelPath.text = pathText
  183. cell.setButtonMore(image: imageCache.getImageButtonStop())
  184. /// Image item
  185. if !metadata.iconName.isEmpty {
  186. cell.filePreviewImageView?.image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account)
  187. } else {
  188. cell.filePreviewImageView?.image = imageCache.getImageFile()
  189. }
  190. /// Status and Info
  191. let user = (metadata.user == session.user ? "" : " - " + metadata.account)
  192. switch metadata.status {
  193. case NCGlobal.shared.metadataStatusWaitCreateFolder:
  194. cell.fileStatusImage?.image = utility.loadImage(named: "arrow.triangle.2.circlepath", colors: NCBrandColor.shared.iconImageMultiColors)
  195. cell.labelStatus.text = NSLocalizedString("_status_wait_create_folder_", comment: "") + user
  196. cell.labelInfo.text = ""
  197. case NCGlobal.shared.metadataStatusWaitDelete:
  198. cell.fileStatusImage?.image = utility.loadImage(named: "trash.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  199. cell.labelStatus.text = NSLocalizedString("_status_wait_delete_", comment: "") + user
  200. cell.labelInfo.text = ""
  201. case NCGlobal.shared.metadataStatusWaitFavorite:
  202. cell.fileStatusImage?.image = utility.loadImage(named: "star.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  203. cell.labelStatus.text = NSLocalizedString("_status_wait_favorite_", comment: "") + user
  204. cell.labelInfo.text = ""
  205. case NCGlobal.shared.metadataStatusWaitCopy:
  206. cell.fileStatusImage?.image = utility.loadImage(named: "c.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  207. cell.labelStatus.text = NSLocalizedString("_status_wait_copy_", comment: "") + user
  208. cell.labelInfo.text = ""
  209. case NCGlobal.shared.metadataStatusWaitMove:
  210. cell.fileStatusImage?.image = utility.loadImage(named: "m.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  211. cell.labelStatus.text = NSLocalizedString("_status_wait_move_", comment: "") + user
  212. cell.labelInfo.text = ""
  213. case NCGlobal.shared.metadataStatusWaitRename:
  214. cell.fileStatusImage?.image = utility.loadImage(named: "a.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  215. cell.labelStatus.text = NSLocalizedString("_status_wait_rename_", comment: "") + user
  216. cell.labelInfo.text = ""
  217. case NCGlobal.shared.metadataStatusWaitDownload:
  218. cell.fileStatusImage?.image = utility.loadImage(named: "arrow.triangle.2.circlepath", colors: NCBrandColor.shared.iconImageMultiColors)
  219. cell.labelStatus.text = NSLocalizedString("_status_wait_download_", comment: "") + user
  220. cell.labelInfo.text = utilityFileSystem.transformedSize(metadata.size)
  221. case NCGlobal.shared.metadataStatusDownloading:
  222. if #available(iOS 17.0, *) {
  223. cell.fileStatusImage?.image = utility.loadImage(named: "arrowshape.down.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  224. }
  225. cell.labelStatus.text = NSLocalizedString("_status_downloading_", comment: "") + user
  226. cell.labelInfo.text = utilityFileSystem.transformedSize(metadata.size) + " - " + self.utilityFileSystem.transformedSize(transfer.totalBytes)
  227. case NCGlobal.shared.metadataStatusWaitUpload:
  228. cell.fileStatusImage?.image = utility.loadImage(named: "arrow.triangle.2.circlepath", colors: NCBrandColor.shared.iconImageMultiColors)
  229. cell.labelStatus.text = NSLocalizedString("_status_wait_upload_", comment: "") + user
  230. cell.labelInfo.text = ""
  231. case NCGlobal.shared.metadataStatusUploading:
  232. if #available(iOS 17.0, *) {
  233. cell.fileStatusImage?.image = utility.loadImage(named: "arrowshape.up.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  234. }
  235. cell.labelStatus.text = NSLocalizedString("_status_uploading_", comment: "") + user
  236. cell.labelInfo.text = utilityFileSystem.transformedSize(metadata.size) + " - " + self.utilityFileSystem.transformedSize(transfer.totalBytes)
  237. case NCGlobal.shared.metadataStatusDownloadError, NCGlobal.shared.metadataStatusUploadError:
  238. cell.fileStatusImage?.image = utility.loadImage(named: "exclamationmark.circle", colors: NCBrandColor.shared.iconImageMultiColors)
  239. cell.labelStatus.text = NSLocalizedString("_status_upload_error_", comment: "") + user
  240. cell.labelInfo.text = metadata.sessionError
  241. default:
  242. cell.fileStatusImage?.image = nil
  243. cell.labelStatus.text = ""
  244. cell.labelInfo.text = ""
  245. }
  246. if metadata.session == NCNetworking.shared.sessionUploadBackgroundWWan && !(NCNetworking.shared.networkReachability == .reachableEthernetOrWiFi) {
  247. cell.labelInfo.text = NSLocalizedString("_waiting_for_", comment: "") + " " + NSLocalizedString("_reachable_wifi_", comment: "")
  248. }
  249. cell.accessibilityLabel = metadata.fileNameView + ", " + (cell.labelInfo.text ?? "")
  250. /// Progress view
  251. if let transfer = NCTransferProgress.shared.get(ocIdTransfer: metadata.ocIdTransfer) {
  252. cell.setProgress(progress: transfer.progressNumber.floatValue)
  253. } else {
  254. cell.setProgress(progress: 0.0)
  255. }
  256. /// Remove last separator
  257. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  258. cell.separator.isHidden = true
  259. } else {
  260. cell.separator.isHidden = false
  261. }
  262. return cell
  263. }
  264. // MARK: - DataSource
  265. override func reloadDataSource() {
  266. if let results = self.database.getResultsMetadatas(predicate: NSPredicate(format: "status != %i", NCGlobal.shared.metadataStatusNormal), sortedByKeyPath: "sessionDate", ascending: true) {
  267. self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView)
  268. } else {
  269. self.dataSource.removeAll()
  270. }
  271. if self.dataSource.isEmpty() {
  272. NCTransferProgress.shared.removeAll()
  273. }
  274. super.reloadDataSource()
  275. }
  276. override func getServerData() {
  277. reloadDataSource()
  278. }
  279. }