NCTransfers.swift 12 KB

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