NCTransfers.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 Foundation
  24. import NCCommunication
  25. class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate {
  26. var metadataTemp: tableMetadata?
  27. required init?(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. appDelegate.activeTransfers = self
  30. titleCurrentFolder = NSLocalizedString("_transfers_", comment: "")
  31. layoutKey = k_layout_view_transfers
  32. enableSearchBar = false
  33. emptyImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "load"), width: 300, height: 300, color: .gray)
  34. emptyTitle = "_no_transfer_"
  35. emptyDescription = "_no_transfer_sub_"
  36. }
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. listLayout.itemHeight = 105
  40. }
  41. override func viewWillAppear(_ animated: Bool) {
  42. appDelegate.activeViewController = self
  43. collectionView?.collectionViewLayout = listLayout
  44. self.navigationItem.title = titleCurrentFolder
  45. setNavigationItem()
  46. reloadDataSource()
  47. }
  48. override func setNavigationItem() {
  49. self.navigationItem.rightBarButtonItem = nil
  50. self.navigationItem.leftBarButtonItem = nil
  51. }
  52. // MARK: - NotificationCenter
  53. override func downloadStartFile(_ notification: NSNotification) {
  54. if self.view?.window == nil { return }
  55. reloadDataSource()
  56. }
  57. override func downloadedFile(_ notification: NSNotification) {
  58. if self.view?.window == nil { return }
  59. reloadDataSource()
  60. }
  61. override func downloadCancelFile(_ notification: NSNotification) {
  62. if self.view?.window == nil { return }
  63. reloadDataSource()
  64. }
  65. override func uploadStartFile(_ notification: NSNotification) {
  66. if self.view?.window == nil { return }
  67. if let userInfo = notification.userInfo as NSDictionary? {
  68. if let metadata = userInfo["metadata"] as? tableMetadata {
  69. if let row = dataSource.addMetadata(metadata) {
  70. let indexPath = IndexPath(row: row, section: 0)
  71. collectionView?.performBatchUpdates({
  72. collectionView?.insertItems(at: [indexPath])
  73. }, completion: { (_) in
  74. self.reloadDataSource()
  75. })
  76. }
  77. }
  78. }
  79. }
  80. override func uploadedFile(_ notification: NSNotification) {
  81. if self.view?.window == nil { return }
  82. if let userInfo = notification.userInfo as NSDictionary? {
  83. if let metadata = userInfo["metadata"] as? tableMetadata, let ocIdTemp = userInfo["ocIdTemp"] as? String, let errorCode = userInfo["errorCode"] as? Int {
  84. if errorCode == 0 {
  85. if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
  86. let indexPath = IndexPath(row: row, section: 0)
  87. collectionView?.performBatchUpdates({
  88. collectionView?.deleteItems(at: [indexPath])
  89. }, completion: { (_) in
  90. self.collectionView?.reloadData()
  91. })
  92. } else {
  93. reloadDataSource()
  94. }
  95. } else if errorCode != NSURLErrorCancelled {
  96. if let row = dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp) {
  97. let indexPath = IndexPath(row: row, section: 0)
  98. collectionView?.performBatchUpdates({
  99. collectionView?.reloadItems(at: [indexPath])
  100. }, completion: { (_) in
  101. self.collectionView?.reloadData()
  102. })
  103. } else {
  104. reloadDataSource()
  105. }
  106. }
  107. }
  108. }
  109. }
  110. override func uploadCancelFile(_ notification: NSNotification) {
  111. if self.view?.window == nil { return }
  112. if let userInfo = notification.userInfo as NSDictionary? {
  113. if let metadata = userInfo["metadata"] as? tableMetadata {
  114. if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
  115. let indexPath = IndexPath(row: row, section: 0)
  116. collectionView?.performBatchUpdates({
  117. collectionView?.deleteItems(at: [indexPath])
  118. }, completion: { (_) in
  119. self.collectionView?.reloadData()
  120. })
  121. } else {
  122. self.reloadDataSource()
  123. }
  124. }
  125. }
  126. }
  127. // MARK: TAP EVENT
  128. override func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  129. if gestureRecognizer.state != .began { return }
  130. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
  131. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  132. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_all_task_", comment: ""), style: .default, handler: { action in
  133. NCNetworking.shared.cancelAllTransfer(account: self.appDelegate.account) {
  134. self.reloadDataSource()
  135. }
  136. }))
  137. self.present(alertController, animated: true, completion: nil)
  138. }
  139. override func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  140. if gestureRecognizer.state != .began { return }
  141. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) {
  142. metadataTemp = metadata
  143. let touchPoint = gestureRecognizer.location(in: collectionView)
  144. becomeFirstResponder()
  145. let startTaskItem = UIMenuItem.init(title: NSLocalizedString("_force_start_", comment: ""), action: #selector(startTask(_:)))
  146. UIMenuController.shared.menuItems = [startTaskItem]
  147. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  148. UIMenuController.shared.setMenuVisible(true, animated: true)
  149. }
  150. }
  151. override func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) { }
  152. @objc func startTask(_ notification: Any) {
  153. guard let metadata = metadataTemp else { return }
  154. metadata.status = Int(k_metadataStatusInUpload)
  155. metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
  156. NCManageDatabase.sharedInstance.addMetadata(metadata)
  157. NCNetworking.shared.upload(metadata: metadata) { (_, _) in }
  158. }
  159. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  160. if action != #selector(startTask(_:)) { return false }
  161. guard let metadata = metadataTemp else { return false }
  162. if metadata.e2eEncrypted { return false }
  163. if metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading {
  164. return true
  165. }
  166. return false
  167. }
  168. // MARK: - Collection View
  169. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  170. // nothing
  171. }
  172. override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  173. return CGSize(width: collectionView.frame.width, height: 0)
  174. }
  175. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  176. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  177. return UICollectionViewCell()
  178. }
  179. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "transferCell", for: indexPath) as! NCTransferCell
  180. cell.delegate = self
  181. cell.objectId = metadata.ocId
  182. cell.indexPath = indexPath
  183. cell.imageItem.image = nil
  184. cell.imageItem.backgroundColor = nil
  185. cell.labelTitle.text = metadata.fileNameView
  186. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  187. let serverUrlHome = NCUtility.shared.getHomeServer(urlBase: metadata.urlBase, account: metadata.account)
  188. var pathText = metadata.serverUrl.replacingOccurrences(of: serverUrlHome, with: "")
  189. if pathText == "" { pathText = "/" }
  190. cell.labelPath.text = pathText
  191. cell.setButtonMore(named: k_buttonMoreStop, image: NCCollectionCommon.images.cellButtonStop)
  192. cell.progressView.progress = 0.0
  193. cell.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  194. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  195. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  196. } else {
  197. if metadata.hasPreview {
  198. cell.imageItem.backgroundColor = .lightGray
  199. } else {
  200. if metadata.iconName.count > 0 {
  201. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  202. } else {
  203. cell.imageItem.image = NCCollectionCommon.images.cellFileImage
  204. }
  205. }
  206. }
  207. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  208. // Transfer
  209. var progress: Float = 0.0
  210. var totalBytes: Double = 0.0
  211. let progressArray = appDelegate.listProgressMetadata.object(forKey: metadata.ocId) as? NSArray
  212. if progressArray != nil && progressArray?.count == 3 {
  213. progress = progressArray?.object(at: 0) as? Float ?? 0
  214. totalBytes = progressArray?.object(at: 1) as? Double ?? 0
  215. }
  216. if metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status >= k_metadataStatusTypeUpload {
  217. cell.progressView.isHidden = false
  218. } else {
  219. cell.progressView.isHidden = true
  220. cell.progressView.progress = progress
  221. }
  222. // Write status on Label Info
  223. switch metadata.status {
  224. case Int(k_metadataStatusWaitDownload):
  225. cell.labelStatus.text = NSLocalizedString("_status_wait_download_", comment: "")
  226. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  227. break
  228. case Int(k_metadataStatusInDownload):
  229. cell.labelStatus.text = NSLocalizedString("_status_in_download_", comment: "")
  230. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  231. break
  232. case Int(k_metadataStatusDownloading):
  233. cell.labelStatus.text = NSLocalizedString("_status_downloading_", comment: "")
  234. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  235. break
  236. case Int(k_metadataStatusWaitUpload):
  237. cell.labelStatus.text = NSLocalizedString("_status_wait_upload_", comment: "")
  238. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  239. break
  240. case Int(k_metadataStatusInUpload):
  241. cell.labelStatus.text = NSLocalizedString("_status_in_upload_", comment: "")
  242. cell.labelInfo.text = CCUtility.transformedSize(metadata.size)
  243. break
  244. case Int(k_metadataStatusUploading):
  245. cell.labelStatus.text = NSLocalizedString("_status_uploading_", comment: "")
  246. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  247. break
  248. default:
  249. cell.labelStatus.text = ""
  250. cell.labelInfo.text = ""
  251. break
  252. }
  253. // Remove last separator
  254. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  255. cell.separator.isHidden = true
  256. } else {
  257. cell.separator.isHidden = false
  258. }
  259. return cell
  260. }
  261. // MARK: - DataSource + NC Endpoint
  262. override func reloadDataSource() {
  263. super.reloadDataSource()
  264. metadatasSource = NCManageDatabase.sharedInstance.getAdvancedMetadatas(predicate: NSPredicate(format: "(session CONTAINS 'upload') OR (session CONTAINS 'download')"), page: 1, limit: 100, sorted: "sessionTaskIdentifier", ascending: false)
  265. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource)
  266. refreshControl.endRefreshing()
  267. collectionView.reloadData()
  268. }
  269. override func reloadDataSourceNetwork(forced: Bool = false) {
  270. super.reloadDataSourceNetwork(forced: forced)
  271. reloadDataSource()
  272. }
  273. }