2015-08-21 13:02:36 +00:00
|
|
|
//
|
2015-08-24 18:45:54 +00:00
|
|
|
// PurchaseDownloadObserver.swift
|
2015-08-21 13:02:36 +00:00
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Andrew Naylor on 21/08/2015.
|
|
|
|
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2015-08-21 18:01:56 +00:00
|
|
|
let csi = "\u{001B}["
|
|
|
|
|
2015-09-14 08:51:09 +00:00
|
|
|
@objc class PurchaseDownloadObserver: NSObject, CKDownloadQueueObserver {
|
2015-08-24 18:45:54 +00:00
|
|
|
let purchase: SSPurchase
|
|
|
|
var completionHandler: (() -> ())?
|
2015-09-03 23:13:47 +00:00
|
|
|
var errorHandler: ((MASError) -> ())?
|
2015-08-21 18:01:56 +00:00
|
|
|
|
2015-08-24 18:45:54 +00:00
|
|
|
init(purchase: SSPurchase) {
|
|
|
|
self.purchase = purchase
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
func downloadQueue(_ queue: CKDownloadQueue, statusChangedFor download: SSDownload!) {
|
|
|
|
guard download.metadata.itemIdentifier == purchase.itemIdentifier,
|
|
|
|
let status = download.status else {
|
2015-08-21 18:01:56 +00:00
|
|
|
return
|
2015-08-21 13:02:36 +00:00
|
|
|
}
|
2016-09-17 12:58:38 +00:00
|
|
|
|
|
|
|
if status.isFailed || status.isCancelled {
|
|
|
|
queue.removeDownload(withItemIdentifier: download.metadata.itemIdentifier)
|
2015-09-03 23:13:47 +00:00
|
|
|
}
|
2015-09-20 22:10:46 +00:00
|
|
|
else if let state = status.progressState {
|
|
|
|
progress(state)
|
2015-09-03 23:13:47 +00:00
|
|
|
}
|
2015-08-21 13:02:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
func downloadQueue(_ queue: CKDownloadQueue, changedWithAddition download: SSDownload!) {
|
|
|
|
guard download.metadata.itemIdentifier == purchase.itemIdentifier else {
|
|
|
|
return
|
|
|
|
}
|
2015-09-20 18:10:13 +00:00
|
|
|
clearLine()
|
2015-09-14 08:51:09 +00:00
|
|
|
print("==> Downloading " + download.metadata.title)
|
2015-08-21 13:02:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
func downloadQueue(_ queue: CKDownloadQueue, changedWithRemoval download: SSDownload!) {
|
|
|
|
guard download.metadata.itemIdentifier == purchase.itemIdentifier,
|
|
|
|
let status = download.status else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-03 23:13:47 +00:00
|
|
|
clearLine()
|
2016-09-17 12:58:38 +00:00
|
|
|
if status.isFailed {
|
2015-09-14 08:51:09 +00:00
|
|
|
print("==> Download Failed")
|
2016-09-17 12:58:38 +00:00
|
|
|
errorHandler?(MASError(code: .downloadFailed, sourceError: status.error as NSError?))
|
2015-09-03 23:13:47 +00:00
|
|
|
}
|
2016-09-17 12:58:38 +00:00
|
|
|
else if status.isCancelled {
|
2015-09-14 08:51:09 +00:00
|
|
|
print("==> Download Cancelled")
|
2016-09-17 12:58:38 +00:00
|
|
|
errorHandler?(MASError(code: .cancelled))
|
2015-09-03 23:13:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-14 08:51:09 +00:00
|
|
|
print("==> Installed " + download.metadata.title)
|
2015-09-03 23:13:47 +00:00
|
|
|
completionHandler?()
|
2015-08-24 18:45:54 +00:00
|
|
|
}
|
2015-08-21 13:02:36 +00:00
|
|
|
}
|
2015-08-21 18:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ProgressState {
|
|
|
|
let percentComplete: Float
|
|
|
|
let phase: String
|
|
|
|
|
|
|
|
var percentage: String {
|
|
|
|
return String(format: "%.1f%%", arguments: [floor(percentComplete * 100)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
func progress(_ state: ProgressState) {
|
2015-08-21 18:11:36 +00:00
|
|
|
// Don't display the progress bar if we're not on a terminal
|
2015-09-20 21:58:58 +00:00
|
|
|
guard isatty(fileno(stdout)) != 0 else {
|
2015-08-21 18:11:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-21 18:01:56 +00:00
|
|
|
let barLength = 60
|
|
|
|
|
|
|
|
let completeLength = Int(state.percentComplete * Float(barLength))
|
|
|
|
var bar = ""
|
|
|
|
for i in 0..<barLength {
|
|
|
|
if i < completeLength {
|
|
|
|
bar += "#"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bar += "-"
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 23:13:47 +00:00
|
|
|
clearLine()
|
2015-09-14 08:51:09 +00:00
|
|
|
print("\(bar) \(state.percentage) \(state.phase)", terminator: "")
|
2015-08-21 18:14:37 +00:00
|
|
|
fflush(stdout)
|
2015-08-21 18:01:56 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 23:13:47 +00:00
|
|
|
func clearLine() {
|
2015-09-20 18:10:43 +00:00
|
|
|
guard isatty(fileno(stdout)) != 0 else {
|
|
|
|
return
|
|
|
|
}
|
2015-09-20 23:47:16 +00:00
|
|
|
print("\(csi)2K\(csi)0G", terminator: "")
|
2015-09-20 22:03:06 +00:00
|
|
|
fflush(stdout)
|
2015-09-03 23:13:47 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 18:01:56 +00:00
|
|
|
extension SSDownloadStatus {
|
2015-09-20 22:10:46 +00:00
|
|
|
var progressState: ProgressState? {
|
|
|
|
if let phase = activePhase {
|
|
|
|
return ProgressState(percentComplete: percentComplete, phase: phase.phaseDescription)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-21 18:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SSDownloadPhase {
|
|
|
|
var phaseDescription: String {
|
|
|
|
switch phaseType {
|
|
|
|
case 0:
|
|
|
|
return "Downloading"
|
|
|
|
case 1:
|
|
|
|
return "Installing"
|
|
|
|
default:
|
|
|
|
return "Waiting"
|
|
|
|
}
|
|
|
|
}
|
2016-09-17 12:58:38 +00:00
|
|
|
}
|