Skip to content

Commit

Permalink
Fix Wine Download View & Improve Download ETA Handling
Browse files Browse the repository at this point in the history
Add "Progress: & ETA:" back to Wine Download View. Improve handling of Download ETA using optional protection.
  • Loading branch information
benwaco committed Jun 23, 2023
1 parent 7a11af6 commit 8553fec
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions Whisky/Views/Setup Views/WineDownloadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ struct WineDownloadView: View {
@State private var observation: NSKeyValueObservation?
@State private var startTime: Date?
@State private var downloadSpeed: Double?

@Binding var tarLocation: URL
@Binding var path: [SetupStage]

var body: some View {
VStack {
VStack {
Expand All @@ -32,9 +30,9 @@ struct WineDownloadView: View {
ProgressView(value: fractionProgress, total: 1)
HStack {
HStack {
Text(formatBytes(completed: completedBytes, total: totalBytes))
Text("Progress: \(formatBytes(completed: completedBytes, total: totalBytes))")
Spacer()
Text(estimateRemainingTime())
Text("ETA: \(estimateRemainingTime() ?? "Estimating...")")
}
.font(.subheadline)
Spacer()
Expand All @@ -56,7 +54,6 @@ struct WineDownloadView: View {
proceed()
}
}

observation = downloadTask?.observe(\.countOfBytesReceived) { task, _ in
Task {
await MainActor.run {
Expand All @@ -65,13 +62,11 @@ struct WineDownloadView: View {
if completedBytes > 0 {
downloadSpeed = Double(completedBytes) / elapsedTime
}

fractionProgress = Double(task.countOfBytesReceived) / Double(totalBytes)
completedBytes = task.countOfBytesReceived
}
}
}

startTime = Date()
downloadTask?.resume()
await MainActor.run {
Expand All @@ -81,37 +76,33 @@ struct WineDownloadView: View {
}
}
}

func formatPercentage(_ value: Double) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 0
formatter.minimumIntegerDigits = 1
return formatter.string(from: value as NSNumber) ?? ""
}

func formatBytes(completed: Int64, total: Int64) -> String {
let formatter = ByteCountFormatter()
formatter.countStyle = .file
let completed = formatter.string(fromByteCount: completed)
let total = formatter.string(fromByteCount: total)
return "(\(completed)/\(total))"
}

func estimateRemainingTime() -> String {
guard let speed = downloadSpeed else {
return "Estimating..."
func estimateRemainingTime() -> String? {
guard let speed = downloadSpeed, speed != 0 else {
return nil
}

let remainingBytes = totalBytes - completedBytes
let remainingTimeInSeconds = Double(remainingBytes) / speed

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .abbreviated
return formatter.string(from: remainingTimeInSeconds) ?? ""
return formatter.string(from: TimeInterval(remainingTimeInSeconds))
}

func proceed() {
path.append(.wineInstall)
}
Expand Down

0 comments on commit 8553fec

Please sign in to comment.