-
-
Notifications
You must be signed in to change notification settings - Fork 235
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JoshuaBrest
suggested changes
Jun 22, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to edit all translation files and it would be nice if they were the eta was a separate string on the right.
Add "Progress: & ETA:" back to Wine Download View. Improve handling of Download ETA using optional protection.
99ddd1b
to
8553fec
Compare
I have updated the file, but, i don't have write access so I will have to ask you to do it. //
// WineDownloadView.swift
// Whisky
//
// Created by Isaac Marovitz on 20/06/2023.
//
import SwiftUI
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 {
Text("setup.wine.download")
.font(.title)
Text("setup.wine.download.subtitle")
.font(.subheadline)
.foregroundStyle(.secondary)
Spacer()
VStack {
ProgressView(value: fractionProgress, total: 1)
HStack {
HStack {
Text(String(format: String(localized: "setup.wine.progress"),
formatPercentage(fractionProgress),
formatBytes(completed: completedBytes, total: totalBytes)))
Spacer()
Text(shouldShowEstimate()
? String(format: String(localized: "setup.wine.eta"),
formatRemainingTime(remainingBytes: totalBytes - completedBytes))
: String(localized: "setup.wine.estimating"))
}
.font(.subheadline)
}
}
.padding(.horizontal)
Spacer()
}
Spacer()
}
.frame(width: 400, height: 200)
.onAppear {
Task {
if let downloadInfo = await WineDownload.getLatestWineURL(),
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)
}
}
}
}
}
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 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 = .abbreviated
return formatter.string(from: TimeInterval(remainingTimeInSeconds)) ?? ""
}
func proceed() {
path.append(.wineInstall)
}
} |
|
IsaacMarovitz
approved these changes
Jun 25, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.