Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ETA to Wine Download #161

Merged
merged 6 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions Whisky/Views/Setup Views/WineDownloadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ struct WineDownloadView: View {
@State private var fractionProgress: Double = 0
@State private var completedBytes: Int64 = 0
@State private var totalBytes: Int64 = 0
@State private var downloadSpeed: Double = 0
@State private var downloadTask: URLSessionDownloadTask?
@State private var observation: NSKeyValueObservation?
@State private var startTime: Date?
@Binding var tarLocation: URL
@Binding var path: [SetupStage]

var body: some View {
VStack {
VStack {
Expand All @@ -29,11 +30,18 @@ struct WineDownloadView: View {
VStack {
ProgressView(value: fractionProgress, total: 1)
HStack {
Text(String(format: String(localized: "setup.wine.progress"),
formatPercentage(fractionProgress),
formatBytes(completed: completedBytes, total: totalBytes)))
.font(.subheadline)
Spacer()
HStack {
Text(String(format: String(localized: "setup.wine.progress"),
formatBytes(bytes: completedBytes),
formatBytes(bytes: totalBytes)))
+ Text(" ")
+ (shouldShowEstimate() ?
Text(String(format: String(localized: "setup.wine.eta"),
formatRemainingTime(remainingBytes: totalBytes - completedBytes)))
: Text(""))
Spacer()
}
.font(.subheadline)
}
}
.padding(.horizontal)
Expand All @@ -45,23 +53,27 @@ struct WineDownloadView: View {
.onAppear {
Task {
if let downloadInfo = await WineDownload.getLatestWineURL(),
let url = downloadInfo.directURL {
let url = downloadInfo.directURL {
downloadTask = URLSession.shared.downloadTask(with: url) { url, _, _ in
if let url = url {
tarLocation = url
proceed()
}
}

observation = downloadTask?.observe(\.countOfBytesReceived) { task, _ in
Task {
await MainActor.run {
let currentTime = Date()
let elapsedTime = currentTime.timeIntervalSince(startTime ?? currentTime)
if completedBytes > 0 {
downloadSpeed = Double(completedBytes) / elapsedTime
}
fractionProgress = Double(task.countOfBytesReceived) / Double(totalBytes)
completedBytes = task.countOfBytesReceived
}
}
}

startTime = Date()
downloadTask?.resume()
await MainActor.run {
totalBytes = Int64(downloadInfo.totalByteCount)
Expand All @@ -71,20 +83,29 @@ 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 {
func formatBytes(bytes: Int64) -> String {
let formatter = ByteCountFormatter()
formatter.countStyle = .file
let completed = formatter.string(fromByteCount: completed)
let total = formatter.string(fromByteCount: total)
return "(\(completed)/\(total))"
formatter.zeroPadsFractionDigits = true
return formatter.string(fromByteCount: bytes)
}

func shouldShowEstimate() -> Bool {
let elapsedTime = Date().timeIntervalSince(startTime ?? Date())
return Int(elapsedTime.rounded()) > 5 && completedBytes != 0
}

func formatRemainingTime(remainingBytes: Int64) -> String {
let remainingTimeInSeconds = Double(remainingBytes) / downloadSpeed

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

func proceed() {
Expand Down
3 changes: 2 additions & 1 deletion Whisky/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "%@ of %@";
"setup.wine.eta" = "- %@ remaining";

"setup.wine.install" = "Installing Wine";
"setup.wine.install.subtitle" = "Almost there. Don't tune out yet.";
Expand Down