UTMPendingVirtualMachine.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright © 2021 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import Foundation
  17. /// A Virtual Machine that has not finished downloading.
  18. @MainActor class UTMPendingVirtualMachine: Equatable, Identifiable, ObservableObject {
  19. internal init(name: String, onCancel: @escaping () -> ()) {
  20. self.name = name
  21. self.cancel = onCancel
  22. dateFormatter = DateComponentsFormatter()
  23. dateFormatter.allowedUnits = [.second, .minute, .hour]
  24. dateFormatter.unitsStyle = .abbreviated
  25. }
  26. #if DEBUG
  27. /// init for SwiftUI Preview
  28. internal init(name: String) {
  29. dateFormatter = DateComponentsFormatter()
  30. dateFormatter.allowedUnits = [.second, .minute, .hour]
  31. dateFormatter.unitsStyle = .abbreviated
  32. self.name = name
  33. self.downloadProgress = 0.41
  34. self.cancel = {}
  35. }
  36. #endif
  37. let downloadStart = Date()
  38. private let dateFormatter: DateComponentsFormatter
  39. private var lastETAUpdate = Date()
  40. private var lastDownloadSpeedUpdate = Date()
  41. private var bytesWrittenSinceLastDownloadSpeedUpdate: Int64 = 0
  42. nonisolated private let uuid = UUID()
  43. let name: String
  44. let cancel: () -> ()
  45. // TODO: Refactor to avoid non-optional optionals.
  46. // There should be a state enum or something to represent the steps of the pending VM progress
  47. @Published private(set) var downloadedSize: String? = nil
  48. @Published private(set) var estimatedDownloadSize: String? = nil
  49. /// if `nil`, either the download has not started or has finished and it is currently extracting.
  50. /// Can not cancel if currently extracting.
  51. @Published private(set) var estimatedDownloadSpeed: String? = nil
  52. @Published private(set) var downloadProgress: CGFloat = 0
  53. @Published private(set) var estimatedTimeRemaining: String? = nil
  54. nonisolated static func == (lhs: UTMPendingVirtualMachine, rhs: UTMPendingVirtualMachine) -> Bool {
  55. lhs.uuid == rhs.uuid
  56. }
  57. nonisolated var id: UUID {
  58. uuid
  59. }
  60. private func updateETAStringIfNeeded(_ progress: Float) {
  61. /// only update the ETA string every full second, otherwise the UI is too busy
  62. guard lastETAUpdate.timeIntervalSinceNow < -1 else {
  63. return
  64. }
  65. if progress > 0.999 {
  66. estimatedTimeRemaining = nil
  67. return
  68. }
  69. lastETAUpdate = Date()
  70. let elapsed = Float(-downloadStart.timeIntervalSinceNow)
  71. let estimatedTotalTime = elapsed / progress
  72. let estimatedTimeRemaining = estimatedTotalTime - elapsed
  73. let secondsRemaining = TimeInterval(estimatedTimeRemaining).rounded()
  74. guard let etaString = dateFormatter.string(from: secondsRemaining) else {
  75. self.estimatedTimeRemaining = nil
  76. return
  77. }
  78. let localizedFormatString = NSLocalizedString("%@ remaining", comment: "Format string for remaining time until a download finishes")
  79. self.estimatedTimeRemaining = String.localizedStringWithFormat(localizedFormatString, etaString)
  80. }
  81. private func updateDownloadStats(for newBytesWritten: Int64, currentTotal totalBytesWritten: Int64, estimatedTotal totalBytesExpectedToWrite: Int64) {
  82. bytesWrittenSinceLastDownloadSpeedUpdate += newBytesWritten
  83. /// only update the download speed string every full second, otherwise the UI is too busy
  84. let elapsed = -lastDownloadSpeedUpdate.timeIntervalSinceNow
  85. guard elapsed > 1 else {
  86. return
  87. }
  88. lastDownloadSpeedUpdate = Date()
  89. let bytesPerSecond = bytesWrittenSinceLastDownloadSpeedUpdate
  90. bytesWrittenSinceLastDownloadSpeedUpdate = 0
  91. let bytesString = ByteCountFormatter.string(fromByteCount: bytesPerSecond, countStyle: .binary)
  92. let speedFormat = NSLocalizedString("%@/s",
  93. comment: "Format string for the 'per second' part of a download speed.")
  94. estimatedDownloadSpeed = String.localizedStringWithFormat(speedFormat, bytesString)
  95. /// sizes
  96. downloadedSize = ByteCountFormatter.string(fromByteCount: totalBytesWritten, countStyle: .binary)
  97. estimatedDownloadSize = ByteCountFormatter.string(fromByteCount: totalBytesExpectedToWrite, countStyle: .binary)
  98. }
  99. public func setDownloadProgress(new newBytesWritten: Int64, currentTotal totalBytesWritten: Int64, estimatedTotal totalBytesExpectedToWrite: Int64) {
  100. objectWillChange.send()
  101. let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
  102. downloadProgress = CGFloat(progress)
  103. updateETAStringIfNeeded(progress)
  104. updateDownloadStats(for: newBytesWritten, currentTotal: totalBytesWritten, estimatedTotal: totalBytesExpectedToWrite)
  105. }
  106. func resetProgress(to progress: CGFloat) {
  107. objectWillChange.send()
  108. downloadProgress = progress
  109. downloadedSize = estimatedDownloadSize
  110. estimatedDownloadSpeed = nil
  111. estimatedTimeRemaining = nil
  112. }
  113. public func setDownloadStarting() {
  114. resetProgress(to: 0)
  115. }
  116. public func setDownloadFinishedNowProcessing() {
  117. resetProgress(to: 1)
  118. }
  119. }