NCTransfers.swift 11 KB

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