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 1 commit
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
43 changes: 34 additions & 9 deletions Whisky/Views/Setup Views/WineDownloadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ struct WineDownloadView: View {
@State private var totalBytes: Int64 = 0
@State private var downloadTask: URLSessionDownloadTask?
@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 @@ -30,8 +33,9 @@ struct WineDownloadView: View {
HStack {
Text(String(format: String(localized: "setup.wine.progress"),
formatPercentage(fractionProgress),
formatBytes(completed: completedBytes, total: totalBytes)))
.font(.subheadline)
formatBytes(completed: completedBytes, total: totalBytes),
estimateRemainingTime()))
.font(.subheadline)
Spacer()
benwaco marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand All @@ -44,23 +48,30 @@ 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 @@ -69,23 +80,37 @@ 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..."
benwaco marked this conversation as resolved.
Show resolved Hide resolved
}

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) ?? ""
}

func proceed() {
path.append(.wineInstall)
}
Expand Down
2 changes: 1 addition & 1 deletion Whisky/da.lproj/Localizable.strings
IsaacMarovitz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

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

"setup.wine.install" = "Installing Wine";
"setup.wine.install.subtitle" = "Almost there. Don't tune out yet.";
Expand Down
2 changes: 1 addition & 1 deletion Whisky/es.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/fi.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/fr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/nl.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/pl.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/pt-BR.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/pt-PT.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/ru.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/tr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/uk.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/vi.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

"setup.wine.download" = "Downloading Wine";
"setup.wine.download.subtitle" = "Speeds will vary on your internet connection.";
"setup.wine.progress" = "Progress: %@ %@";
"setup.wine.progress" = "Progress: %@ %@ ETA: %@";
2 changes: 1 addition & 1 deletion Whisky/zh-Hant.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@

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