2019-06-21 11:20:03 -07:00
|
|
|
//
|
|
|
|
|
// OperationGroup.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 6/20/19.
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
|
|
import AltSign
|
|
|
|
|
|
|
|
|
|
class OperationGroup
|
|
|
|
|
{
|
|
|
|
|
let progress = Progress.discreteProgress(totalUnitCount: 0)
|
|
|
|
|
|
|
|
|
|
var completionHandler: ((Result<[String: Result<InstalledApp, Error>], Error>) -> Void)?
|
|
|
|
|
var beginInstallationHandler: ((InstalledApp) -> Void)?
|
|
|
|
|
|
2019-11-18 14:49:17 -08:00
|
|
|
var session: ALTAppleAPISession?
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
var server: Server?
|
|
|
|
|
var signer: ALTSigner?
|
|
|
|
|
|
|
|
|
|
var error: Error?
|
|
|
|
|
|
|
|
|
|
var results = [String: Result<InstalledApp, Error>]()
|
|
|
|
|
|
2019-07-28 15:08:13 -07:00
|
|
|
private var progressByBundleIdentifier = [String: Progress]()
|
2019-07-19 16:10:30 -07:00
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
private let operationQueue = OperationQueue()
|
|
|
|
|
private let installOperationQueue = OperationQueue()
|
|
|
|
|
|
|
|
|
|
init()
|
|
|
|
|
{
|
|
|
|
|
// Enforce only one installation at a time.
|
|
|
|
|
self.installOperationQueue.maxConcurrentOperationCount = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cancel()
|
|
|
|
|
{
|
|
|
|
|
self.operationQueue.cancelAllOperations()
|
|
|
|
|
self.installOperationQueue.cancelAllOperations()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addOperations(_ operations: [Operation])
|
|
|
|
|
{
|
|
|
|
|
for operation in operations
|
|
|
|
|
{
|
|
|
|
|
if let installOperation = operation as? InstallAppOperation
|
|
|
|
|
{
|
|
|
|
|
if let previousOperation = self.installOperationQueue.operations.last
|
|
|
|
|
{
|
|
|
|
|
// Ensures they execute in the order they're added, since isReady is still false at this point.
|
|
|
|
|
installOperation.addDependency(previousOperation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.installOperationQueue.addOperation(installOperation)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self.operationQueue.addOperation(operation)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-19 16:10:30 -07:00
|
|
|
|
2019-07-28 15:08:13 -07:00
|
|
|
func set(_ progress: Progress, for app: AppProtocol)
|
2019-07-19 16:10:30 -07:00
|
|
|
{
|
2019-07-28 15:08:13 -07:00
|
|
|
self.progressByBundleIdentifier[app.bundleIdentifier] = progress
|
2019-07-19 16:10:30 -07:00
|
|
|
|
|
|
|
|
self.progress.totalUnitCount += 1
|
|
|
|
|
self.progress.addChild(progress, withPendingUnitCount: 1)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 15:08:13 -07:00
|
|
|
func progress(for app: AppProtocol) -> Progress?
|
2019-07-19 16:10:30 -07:00
|
|
|
{
|
2019-10-28 13:16:55 -07:00
|
|
|
return self.progress(forAppWithBundleIdentifier: app.bundleIdentifier)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func progress(forAppWithBundleIdentifier bundleIdentifier: String) -> Progress?
|
|
|
|
|
{
|
|
|
|
|
let progress = self.progressByBundleIdentifier[bundleIdentifier]
|
2019-07-19 16:10:30 -07:00
|
|
|
return progress
|
|
|
|
|
}
|
2019-06-21 11:20:03 -07:00
|
|
|
}
|