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 installation progress bar to dock icon #423

Merged
merged 4 commits into from
Nov 22, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ jobs:
- uses: actions/checkout@v3
- name: Run tests
env:
DEVELOPER_DIR: /Applications/Xcode_13.2.1.app
DEVELOPER_DIR: /Applications/Xcode_14.2.app
run: xcodebuild test -scheme Xcodes
17 changes: 17 additions & 0 deletions Xcodes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
E8E98A9025D8631800EC89A0 /* InstallationStepRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAFBC3FF259AC17F00E2A3D8 /* InstallationStepRowView.swift */; };
E8E98A9625D863D700EC89A0 /* InstallationStepDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8E98A9525D863D700EC89A0 /* InstallationStepDetailView.swift */; };
E8F81FC4282D8A17006CBD0F /* Sparkle in Frameworks */ = {isa = PBXBuildFile; productRef = E8F81FC3282D8A17006CBD0F /* Sparkle */; };
E689540325BE8C64000EBCEA /* DockProgress in Frameworks */ = {isa = PBXBuildFile; productRef = E689540225BE8C64000EBCEA /* DockProgress */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -326,6 +327,7 @@
E8F81FC4282D8A17006CBD0F /* Sparkle in Frameworks */,
CABFA9E42592F08E00380FEE /* Version in Frameworks */,
CABFA9FD2592F13300380FEE /* LegibleError in Frameworks */,
E689540325BE8C64000EBCEA /* DockProgress in Frameworks */,
CA9FF86D25951C6E00E47BAF /* XCModel in Frameworks */,
CABFA9F82592F0F900380FEE /* KeychainAccess in Frameworks */,
CAA858CD25A3D8BC00ACF8C0 /* ErrorHandling in Frameworks */,
Expand Down Expand Up @@ -665,6 +667,7 @@
CA9FF86C25951C6E00E47BAF /* XCModel */,
CAA858CC25A3D8BC00ACF8C0 /* ErrorHandling */,
E8F81FC3282D8A17006CBD0F /* Sparkle */,
E689540225BE8C64000EBCEA /* DockProgress */,
);
productName = XcodesMac;
productReference = CAD2E79E2449574E00113D76 /* Xcodes.app */;
Expand Down Expand Up @@ -748,6 +751,7 @@
CAA858CB25A3D8BC00ACF8C0 /* XCRemoteSwiftPackageReference "ErrorHandling" */,
CAC28186259EE27200B8AB0B /* XCRemoteSwiftPackageReference "CombineExpectations" */,
E8F81FC2282D8A17006CBD0F /* XCRemoteSwiftPackageReference "Sparkle" */,
E689540125BE8C64000EBCEA /* XCRemoteSwiftPackageReference "DockProgress" */,
);
productRefGroup = CAD2E79F2449574E00113D76 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -1475,6 +1479,14 @@
minimumVersion = 2.0.0;
};
};
E689540125BE8C64000EBCEA /* XCRemoteSwiftPackageReference "DockProgress" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/sindresorhus/DockProgress";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 3.2.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand Down Expand Up @@ -1527,6 +1539,11 @@
package = E8F81FC2282D8A17006CBD0F /* XCRemoteSwiftPackageReference "Sparkle" */;
productName = Sparkle;
};
E689540225BE8C64000EBCEA /* DockProgress */ = {
isa = XCSwiftPackageProductDependency;
package = E689540125BE8C64000EBCEA /* XCRemoteSwiftPackageReference "DockProgress" */;
productName = DockProgress;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = CAD2E7962449574E00113D76 /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
"version": null
}
},
{
"package": "DockProgress",
"repositoryURL": "https://github.com/sindresorhus/DockProgress",
"state": {
"branch": null,
"revision": "7100b68571e2dafe3a06ad5905b80fc3b0107b4b",
"version": "3.2.0"
}
},
{
"package": "ErrorHandling",
"repositoryURL": "https://github.com/RobotsAndPencils/ErrorHandling",
Expand Down
32 changes: 32 additions & 0 deletions Xcodes/Backend/AppState+Install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import AppleAPI
import Version
import LegibleError
import os.log
import DockProgress

/// Downloads and installs Xcodes
extension AppState {
Expand Down Expand Up @@ -43,6 +44,8 @@ extension AppState {

Logger.appState.info("Using \(downloader) downloader")

setupDockProgress()

return validateSession()
.flatMap { _ in
self.getXcodeArchive(installationType, downloader: downloader)
Expand All @@ -51,6 +54,8 @@ extension AppState {
self.installArchivedXcode(xcode, at: url)
}
.catch { error -> AnyPublisher<InstalledXcode, Swift.Error> in
self.resetDockProgressTracking()

switch error {
case InstallationError.damagedXIP(let damagedXIPURL):
guard attemptNumber < 1 else { return Fail(error: error).eraseToAnyPublisher() }
Expand Down Expand Up @@ -99,6 +104,7 @@ extension AppState {
self.downloadOrUseExistingArchive(for: availableXcode, downloader: downloader, progressChanged: { [unowned self] progress in
DispatchQueue.main.async {
self.setInstallationStep(of: availableXcode.version, to: .downloading(progress: progress))
self.overallProgress.addChild(progress, withPendingUnitCount: AppState.totalProgressUnits - AppState.unxipProgressWeight)
}
})
.map { return (availableXcode, $0) }
Expand Down Expand Up @@ -151,6 +157,7 @@ extension AppState {
cookies
)
progressChanged(progress)

return publisher
.map { _ in destination.url }
.eraseToAnyPublisher()
Expand All @@ -165,6 +172,7 @@ extension AppState {
to: destination.url,
resumingWith: resumeData ?? persistedResumeData)
progressChanged(progress)

return publisher
.map { $0.saveLocation }
.eraseToAnyPublisher()
Expand All @@ -176,6 +184,9 @@ extension AppState {
}

public func installArchivedXcode(_ availableXcode: AvailableXcode, at archiveURL: URL) -> AnyPublisher<InstalledXcode, Error> {
unxipProgress.completedUnitCount = 0
overallProgress.addChild(unxipProgress, withPendingUnitCount: AppState.unxipProgressWeight)

do {
let destinationURL = Path.installDirectory.join("Xcode-\(availableXcode.version.descriptionWithoutBuildMetadata).app").url
switch archiveURL.pathExtension {
Expand Down Expand Up @@ -415,6 +426,9 @@ extension AppState {
}
self.presentedAlert = .privilegedHelper
}

unxipProgress.completedUnitCount = AppState.totalProgressUnits
resetDockProgressTracking()

return helperInstallConsentSubject
.flatMap {
Expand Down Expand Up @@ -455,6 +469,24 @@ extension AppState {
.eraseToAnyPublisher()
}

// MARK: - Dock Progress Tracking

private func setupDockProgress() {
DockProgress.progressInstance = nil
DockProgress.style = .bar

let progress = Progress(totalUnitCount: AppState.totalProgressUnits)
progress.kind = .file
progress.fileOperationKind = .downloading
overallProgress = progress

DockProgress.progressInstance = overallProgress
}

func resetDockProgressTracking() {
DockProgress.progress = 1 // Only way to completely remove overlay with DockProgress is setting progress to complete
}

// MARK: -

func setInstallationStep(of version: Version, to step: InstallationStep) {
Expand Down
16 changes: 16 additions & 0 deletions Xcodes/Backend/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KeychainAccess
import Path
import Version
import os.log
import DockProgress

class AppState: ObservableObject {
private let client = AppleAPI.Client()
Expand Down Expand Up @@ -106,6 +107,19 @@ class AppState: ObservableObject {
private var selectPublisher: AnyCancellable?
private var uninstallPublisher: AnyCancellable?
private var autoInstallTimer: Timer?

// MARK: - Dock Progress Tracking

public static let totalProgressUnits = Int64(10)
public static let unxipProgressWeight = Int64(1)
var overallProgress = Progress()
var unxipProgress = {
let progress = Progress(totalUnitCount: totalProgressUnits)
progress.kind = .file
progress.fileOperationKind = .copying
return progress
}()

// MARK: -

var dataSource: DataSource {
Expand Down Expand Up @@ -489,6 +503,8 @@ class AppState: ObservableObject {

// Cancel the publisher
installationPublishers[id] = nil

resetDockProgressTracking()

// If the download is cancelled by the user, clean up the download files that aria2 creates.
// This isn't done as part of the publisher with handleEvents(receiveCancel:) because it shouldn't happen when e.g. the app quits.
Expand Down
15 changes: 15 additions & 0 deletions Xcodes/Resources/Licenses.rtf
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,21 @@ For more information, please refer to &lt;<http://unlicense.org/>&gt;\
otherwise be required by Sections 4(a), 4(b) and 4(d) of the License.\
\

\fs34 DockProgress\
\

\fs26 MIT License\
\
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)\
\
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\
\
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\
\
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\
\
\

\fs34 KeychainAccess\
\

Expand Down