NCTransfers.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // MARK: - View Life Cycle
  28. required init?(coder aDecoder: NSCoder) {
  29. super.init(coder: aDecoder)
  30. titleCurrentFolder = NSLocalizedString("_transfers_", comment: "")
  31. layoutKey = NCGlobal.shared.layoutViewTransfers
  32. enableSearchBar = false
  33. headerMenuButtonsCommand = false
  34. headerMenuButtonsView = false
  35. headerRichWorkspaceDisable = true
  36. emptyImage = UIImage(named: "arrow.left.arrow.right")?.image(color: .gray, size: UIScreen.main.bounds.width)
  37. emptyTitle = "_no_transfer_"
  38. emptyDescription = "_no_transfer_sub_"
  39. }
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. listLayout.itemHeight = 105
  43. NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: NCGlobal.shared.layoutList)
  44. self.navigationItem.title = titleCurrentFolder
  45. }
  46. override func setNavigationItem() {
  47. self.navigationItem.rightBarButtonItem = nil
  48. self.navigationItem.leftBarButtonItem = nil
  49. }
  50. // MARK: - NotificationCenter
  51. override func downloadStartFile(_ notification: NSNotification) {
  52. reloadDataSource()
  53. }
  54. override func downloadedFile(_ notification: NSNotification) {
  55. reloadDataSource()
  56. }
  57. override func downloadCancelFile(_ notification: NSNotification) {
  58. reloadDataSource()
  59. }
  60. override func uploadStartFile(_ notification: NSNotification) {
  61. reloadDataSource()
  62. }
  63. override func uploadedFile(_ notification: NSNotification) {
  64. reloadDataSource()
  65. }
  66. override func uploadCancelFile(_ notification: NSNotification) {
  67. reloadDataSource()
  68. }
  69. // MARK: TAP EVENT
  70. override func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  71. if gestureRecognizer.state != .began { return }
  72. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
  73. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  74. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_all_task_", comment: ""), style: .default, handler: { _ in
  75. NCNetworking.shared.cancelAllTransfer(account: self.appDelegate.account) {
  76. self.reloadDataSource()
  77. }
  78. }))
  79. self.present(alertController, animated: true, completion: nil)
  80. }
  81. override func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  82. if gestureRecognizer.state != .began { return }
  83. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) {
  84. metadataTemp = metadata
  85. let touchPoint = gestureRecognizer.location(in: collectionView)
  86. becomeFirstResponder()
  87. let startTaskItem = UIMenuItem(title: NSLocalizedString("_force_start_", comment: ""), action: #selector(startTask(_:)))
  88. UIMenuController.shared.menuItems = [startTaskItem]
  89. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  90. UIMenuController.shared.setMenuVisible(true, animated: true)
  91. }
  92. }
  93. override func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) { }
  94. @objc func startTask(_ notification: Any) {
  95. guard let metadata = metadataTemp else { return }
  96. guard appDelegate.account == metadata.account else { return }
  97. NCUtility.shared.extractFiles(from: metadata) { metadatas in
  98. for metadata in metadatas {
  99. if let metadata = NCManageDatabase.shared.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusInUpload) {
  100. NCNetworking.shared.upload(metadata: metadata)
  101. }
  102. }
  103. }
  104. }
  105. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  106. if action != #selector(startTask(_:)) { return false }
  107. guard let metadata = metadataTemp else { return false }
  108. if metadata.e2eEncrypted { return false }
  109. if metadata.status == NCGlobal.shared.metadataStatusWaitUpload || metadata.status == NCGlobal.shared.metadataStatusInUpload || metadata.status == NCGlobal.shared.metadataStatusUploading {
  110. return true
  111. }
  112. return false
  113. }
  114. // MARK: - Collection View
  115. override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  116. return nil
  117. }
  118. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  119. // nothing
  120. }
  121. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  122. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  123. return collectionView.dequeueReusableCell(withReuseIdentifier: "transferCell", for: indexPath) as! NCTransferCell
  124. }
  125. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "transferCell", for: indexPath) as! NCTransferCell
  126. cell.delegate = self
  127. cell.fileObjectId = metadata.ocId
  128. cell.fileUser = metadata.ownerId
  129. cell.indexPath = indexPath
  130. cell.imageItem.image = NCBrandColor.cacheImages.file
  131. cell.imageItem.backgroundColor = nil
  132. cell.labelTitle.text = metadata.fileNameView
  133. cell.labelTitle.textColor = .label
  134. let serverUrlHome = NCUtilityFileSystem.shared.getHomeServer(account: metadata.account)
  135. var pathText = metadata.serverUrl.replacingOccurrences(of: serverUrlHome, with: "")
  136. if pathText == "" { pathText = "/" }
  137. cell.labelPath.text = pathText
  138. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  139. cell.progressView.progress = 0.0
  140. if let image = NCUtility.shared.createFilePreviewImage(ocId: metadata.ocId, etag: metadata.etag, fileNameView: metadata.fileNameView, classFile: metadata.classFile, status: metadata.status, createPreviewMedia: true) {
  141. cell.imageItem.image = image
  142. } else if !metadata.iconName.isEmpty {
  143. cell.imageItem.image = UIImage(named: metadata.iconName)
  144. } else {
  145. cell.imageItem.image = UIImage(named: "file")
  146. }
  147. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  148. if metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status == NCGlobal.shared.metadataStatusUploading {
  149. cell.progressView.isHidden = false
  150. } else {
  151. cell.progressView.isHidden = true
  152. }
  153. // Write status on Label Info
  154. switch metadata.status {
  155. case NCGlobal.shared.metadataStatusWaitDownload:
  156. cell.labelStatus.text = NSLocalizedString("_status_wait_download_", comment: "")
  157. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  158. break
  159. case NCGlobal.shared.metadataStatusInDownload:
  160. cell.labelStatus.text = NSLocalizedString("_status_in_download_", comment: "")
  161. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  162. break
  163. case NCGlobal.shared.metadataStatusDownloading:
  164. cell.labelStatus.text = NSLocalizedString("_status_downloading_", comment: "")
  165. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↓ …"
  166. break
  167. case NCGlobal.shared.metadataStatusWaitUpload:
  168. cell.labelStatus.text = NSLocalizedString("_status_wait_upload_", comment: "")
  169. cell.labelInfo.text = ""
  170. break
  171. case NCGlobal.shared.metadataStatusInUpload:
  172. cell.labelStatus.text = NSLocalizedString("_status_in_upload_", comment: "")
  173. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  174. break
  175. case NCGlobal.shared.metadataStatusUploading:
  176. cell.labelStatus.text = NSLocalizedString("_status_uploading_", comment: "")
  177. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↑ …"
  178. break
  179. case NCGlobal.shared.metadataStatusUploadError:
  180. cell.labelStatus.text = NSLocalizedString("_status_upload_error_", comment: "")
  181. cell.labelInfo.text = metadata.sessionError
  182. break
  183. default:
  184. cell.labelStatus.text = ""
  185. cell.labelInfo.text = ""
  186. break
  187. }
  188. if self.appDelegate.account != metadata.account {
  189. cell.labelInfo.text = NSLocalizedString("_waiting_for_", comment: "") + " " + NSLocalizedString("_user_", comment: "").lowercased() + " \(metadata.userId) " + NSLocalizedString("_in_", comment: "") + " \(metadata.urlBase)"
  190. }
  191. let isWiFi = NCNetworking.shared.networkReachability == NKCommon.typeReachability.reachableEthernetOrWiFi
  192. if metadata.session == NCNetworking.shared.sessionIdentifierBackgroundWWan && !isWiFi {
  193. cell.labelInfo.text = NSLocalizedString("_waiting_for_", comment: "") + " " + NSLocalizedString("_reachable_wifi_", comment: "")
  194. }
  195. cell.accessibilityLabel = metadata.fileNameView + ", " + (cell.labelInfo.text ?? "")
  196. // Remove last separator
  197. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  198. cell.separator.isHidden = true
  199. } else {
  200. cell.separator.isHidden = false
  201. }
  202. return cell
  203. }
  204. // MARK: - DataSource + NC Endpoint
  205. override func reloadDataSource(forced: Bool = true) {
  206. super.reloadDataSource()
  207. DispatchQueue.global().async {
  208. let metadatas = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "status != %i", NCGlobal.shared.metadataStatusNormal), page: 1, limit: 100, sorted: "sessionTaskIdentifier", ascending: false)
  209. self.dataSource = NCDataSource(metadatas: metadatas, account: self.appDelegate.account)
  210. DispatchQueue.main.async {
  211. self.refreshControl.endRefreshing()
  212. self.collectionView.reloadData()
  213. }
  214. }
  215. }
  216. override func reloadDataSourceNetwork(forced: Bool = false) {
  217. super.reloadDataSourceNetwork(forced: forced)
  218. reloadDataSource()
  219. }
  220. }