NCTransfers.swift 16 KB

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