NCTransfers.swift 15 KB

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