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

Improve downloading output #625

Merged
merged 1 commit into from
Oct 30, 2024
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 .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"NeverForceUnwrap": true,
"NeverUseForceTry": true,
"NeverUseImplicitlyUnwrappedOptionals": true,
"NoAccessLevelOnExtensionDeclaration": true,
"NoAccessLevelOnExtensionDeclaration": false,
"NoAssignmentInExpressions": true,
"NoBlockComments": true,
"NoCasesWithOnlyFallthrough": true,
Expand Down
1 change: 0 additions & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

# Rule options
--commas always
--extensionacl on-declarations
--hexliteralcase lowercase
--importgrouping testable-last
--lineaftermarks false
Expand Down
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ disabled_rules:
- function_body_length
- inert_defer
- legacy_objc_type
- no_extension_access_modifier
- no_grouping_extension
- number_separator
- one_declaration_per_file
Expand Down
34 changes: 30 additions & 4 deletions Sources/mas/AppStore/PurchaseDownloadObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
import CommerceKit
import StoreFoundation

private let downloadingPhase: Int64 = 0
private let installingPhase: Int64 = 1
private let downloadedPhase: Int64 = 5

@objc
class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver {
let purchase: SSPurchase
var completionHandler: (() -> Void)?
var errorHandler: ((MASError) -> Void)?
var priorPhaseType: Int64?

init(purchase: SSPurchase) {
self.purchase = purchase
Expand All @@ -30,6 +35,21 @@ class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver {
if status.isFailed || status.isCancelled {
queue.removeDownload(withItemIdentifier: download.metadata.itemIdentifier)
} else {
if priorPhaseType != status.activePhase.phaseType {
switch status.activePhase.phaseType {
case downloadedPhase:
if priorPhaseType == downloadingPhase {
clearLine()
printInfo("Downloaded \(download.progressDescription)")
}
case installingPhase:
clearLine()
printInfo("Installing \(download.progressDescription)")
default:
break
}
priorPhaseType = status.activePhase.phaseType
}
progress(status.progressState)
}
}
Expand All @@ -39,7 +59,7 @@ class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver {
return
}
clearLine()
printInfo("Downloading \(download.metadata.title)")
printInfo("Downloading \(download.progressDescription)")
}

func downloadQueue(_: CKDownloadQueue, changedWithRemoval download: SSDownload) {
Expand All @@ -56,7 +76,7 @@ class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver {
} else if status.isCancelled {
errorHandler?(.cancelled)
} else {
printInfo("Installed \(download.metadata.title)")
printInfo("Installed \(download.progressDescription)")
completionHandler?()
}
}
Expand Down Expand Up @@ -94,6 +114,12 @@ func progress(_ state: ProgressState) {
fflush(stdout)
}

private extension SSDownload {
var progressDescription: String {
"\(metadata.title) (\(metadata.bundleVersion ?? "unknown version"))"
}
}

extension SSDownloadStatus {
var progressState: ProgressState {
ProgressState(percentComplete: percentComplete, phase: activePhase.phaseDescription)
Expand All @@ -103,9 +129,9 @@ extension SSDownloadStatus {
extension SSDownloadPhase {
var phaseDescription: String {
switch phaseType {
case 0:
case downloadingPhase:
return "Downloading"
case 1:
case installingPhase:
return "Installing"
default:
return "Waiting"
Expand Down
2 changes: 1 addition & 1 deletion Tests/masTests/.swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"NeverForceUnwrap": false,
"NeverUseForceTry": false,
"NeverUseImplicitlyUnwrappedOptionals": true,
"NoAccessLevelOnExtensionDeclaration": true,
"NoAccessLevelOnExtensionDeclaration": false,
"NoAssignmentInExpressions": true,
"NoBlockComments": true,
"NoCasesWithOnlyFallthrough": true,
Expand Down
Loading