mirror of
https://github.com/mas-cli/mas
synced 2025-03-06 23:57:21 +00:00
Better encapsulate PurchaseDownloadObserver
.
Partial #696 Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
This commit is contained in:
parent
3950606b99
commit
87f97491b7
3 changed files with 45 additions and 19 deletions
|
@ -41,3 +41,21 @@ file_types_order:
|
|||
[preview_provider],
|
||||
[library_content_provider],
|
||||
]
|
||||
type_contents_order:
|
||||
order:
|
||||
[
|
||||
[case],
|
||||
[type_alias, associated_type],
|
||||
[subtype],
|
||||
[type_property],
|
||||
[instance_property],
|
||||
[ib_inspectable],
|
||||
[ib_outlet],
|
||||
[initializer],
|
||||
[deinitializer],
|
||||
[type_method],
|
||||
[view_life_cycle_method],
|
||||
[ib_action],
|
||||
[other_method],
|
||||
[subscript],
|
||||
]
|
||||
|
|
|
@ -7,23 +7,27 @@
|
|||
//
|
||||
|
||||
import CommerceKit
|
||||
import PromiseKit
|
||||
import StoreFoundation
|
||||
|
||||
private let downloadingPhase: Int64 = 0
|
||||
private let installingPhase: Int64 = 1
|
||||
private let downloadedPhase: Int64 = 5
|
||||
|
||||
@objc
|
||||
class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver {
|
||||
class PurchaseDownloadObserver: CKDownloadQueueObserver {
|
||||
private let purchase: SSPurchase
|
||||
var completionHandler: (() -> Void)?
|
||||
var errorHandler: ((MASError) -> Void)?
|
||||
private var completionHandler: (() -> Void)?
|
||||
private var errorHandler: ((MASError) -> Void)?
|
||||
private var priorPhaseType: Int64?
|
||||
|
||||
init(purchase: SSPurchase) {
|
||||
self.purchase = purchase
|
||||
}
|
||||
|
||||
deinit {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func downloadQueue(_ queue: CKDownloadQueue, statusChangedFor download: SSDownload) {
|
||||
guard
|
||||
download.metadata.itemIdentifier == purchase.itemIdentifier,
|
||||
|
@ -82,7 +86,7 @@ class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver {
|
|||
}
|
||||
}
|
||||
|
||||
struct ProgressState {
|
||||
private struct ProgressState {
|
||||
let percentComplete: Float
|
||||
let phase: String
|
||||
|
||||
|
@ -92,7 +96,7 @@ struct ProgressState {
|
|||
}
|
||||
}
|
||||
|
||||
func progress(_ state: ProgressState) {
|
||||
private func progress(_ state: ProgressState) {
|
||||
// Don't display the progress bar if we're not on a terminal
|
||||
guard isatty(fileno(stdout)) != 0 else {
|
||||
return
|
||||
|
@ -112,13 +116,13 @@ private extension SSDownload {
|
|||
}
|
||||
}
|
||||
|
||||
extension SSDownloadStatus {
|
||||
private extension SSDownloadStatus {
|
||||
var progressState: ProgressState {
|
||||
ProgressState(percentComplete: percentComplete, phase: activePhase.phaseDescription)
|
||||
}
|
||||
}
|
||||
|
||||
extension SSDownloadPhase {
|
||||
private extension SSDownloadPhase {
|
||||
var phaseDescription: String {
|
||||
switch phaseType {
|
||||
case downloadingPhase:
|
||||
|
@ -130,3 +134,17 @@ extension SSDownloadPhase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PurchaseDownloadObserver {
|
||||
func observeDownloadQueue(_ downloadQueue: CKDownloadQueue = CKDownloadQueue.shared()) -> Promise<Void> {
|
||||
let observerID = downloadQueue.add(self)
|
||||
|
||||
return Promise<Void> { seal in
|
||||
errorHandler = seal.reject
|
||||
completionHandler = seal.fulfill_
|
||||
}
|
||||
.ensure {
|
||||
downloadQueue.remove(observerID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,17 +75,7 @@ extension SSPurchase {
|
|||
}
|
||||
}
|
||||
.then { purchase in
|
||||
let observer = PurchaseDownloadObserver(purchase: purchase)
|
||||
let downloadQueue = CKDownloadQueue.shared()
|
||||
let observerID = downloadQueue.add(observer)
|
||||
|
||||
return Promise<Void> { seal in
|
||||
observer.errorHandler = seal.reject
|
||||
observer.completionHandler = seal.fulfill_
|
||||
}
|
||||
.ensure {
|
||||
downloadQueue.remove(observerID)
|
||||
}
|
||||
PurchaseDownloadObserver(purchase: purchase).observeDownloadQueue()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue