NCTransfersProgress.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // NCTransfersProgress.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/08/24.
  6. // Copyright © 2024 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 NextcloudKit
  25. public class NCTransferProgress: NSObject {
  26. static let shared = NCTransferProgress()
  27. public class Transfer {
  28. var ocId: String
  29. var ocIdTransfer: String
  30. var session: String
  31. var chunk: Int
  32. var e2eEncrypted: Bool
  33. var progressNumber: NSNumber
  34. var totalBytes: Int64
  35. var totalBytesExpected: Int64
  36. var countError: Int = 0
  37. init(ocId: String, ocIdTransfer: String, session: String, chunk: Int, e2eEncrypted: Bool, progressNumber: NSNumber, totalBytes: Int64, totalBytesExpected: Int64) {
  38. self.ocId = ocId
  39. self.ocIdTransfer = ocIdTransfer
  40. self.session = session
  41. self.chunk = chunk
  42. self.e2eEncrypted = e2eEncrypted
  43. self.progressNumber = progressNumber
  44. self.totalBytes = totalBytes
  45. self.totalBytesExpected = totalBytesExpected
  46. }
  47. }
  48. private var transfers = ThreadSafeArray<Transfer>()
  49. private var lastOcIdTransferInForeground: String = ""
  50. override private init() {}
  51. @discardableResult
  52. func append(_ transfer: Transfer) -> Transfer {
  53. remove(ocIdTransfer: transfer.ocIdTransfer)
  54. transfers.append(transfer)
  55. if transfer.chunk > 0 || transfer.e2eEncrypted {
  56. lastOcIdTransferInForeground = transfer.ocIdTransfer
  57. }
  58. return transfer
  59. }
  60. func remove(ocIdTransfer: String) {
  61. transfers.remove(where: { $0.ocIdTransfer == ocIdTransfer })
  62. }
  63. func removeAll() {
  64. transfers.removeAll()
  65. }
  66. func get(ocIdTransfer: String) -> Transfer? {
  67. return transfers.filter({ $0.ocIdTransfer == ocIdTransfer}).first
  68. }
  69. func get(ocId: String, ocIdTransfer: String, session: String) -> Transfer {
  70. if let transfer = transfers.filter({ $0.ocIdTransfer == ocIdTransfer}).first {
  71. return transfer
  72. }
  73. return Transfer(ocId: ocId, ocIdTransfer: ocIdTransfer, session: session, chunk: 0, e2eEncrypted: false, progressNumber: 0, totalBytes: 0, totalBytesExpected: 0)
  74. }
  75. func getAll() -> ThreadSafeArray<Transfer> {
  76. return transfers
  77. }
  78. func getLastTransferProgressInForeground() -> Float? {
  79. if !lastOcIdTransferInForeground.isEmpty {
  80. let transfer = get(ocIdTransfer: lastOcIdTransferInForeground)
  81. return transfer?.progressNumber.floatValue
  82. }
  83. return nil
  84. }
  85. func haveUploadInForeground() -> Bool {
  86. var result: Bool = false
  87. transfers.forEach { transfer in
  88. if transfer.chunk > 0 || transfer.e2eEncrypted {
  89. result = true
  90. }
  91. }
  92. return result
  93. }
  94. func addCountError(ocIdTransfer: String) {
  95. if let transfer = transfers.filter({ $0.ocIdTransfer == ocIdTransfer}).first {
  96. transfer.countError += 1
  97. }
  98. }
  99. func clearCountError(ocIdTransfer: String) {
  100. if let transfer = transfers.filter({ $0.ocIdTransfer == ocIdTransfer}).first {
  101. transfer.countError = 0
  102. }
  103. }
  104. func clearAllCountError() {
  105. transfers.forEach { transfer in
  106. transfer.countError = 0
  107. }
  108. }
  109. }