refactor(ios/engine): drops unused .activity, .type

This commit is contained in:
jahorton 2020-06-26 15:25:17 +07:00
parent 6245c9f267
commit ec06081223
2 changed files with 22 additions and 29 deletions

View file

@ -20,9 +20,13 @@ public class ResourceDownloadManager {
public typealias CompletionHandler<Resource: LanguageResource> = (Resource.Package?, Error?) -> Void where Resource.Package: TypedKeymanPackage<Resource>
public static let shared = ResourceDownloadManager()
internal init(session: URLSession = URLSession.shared) {
downloader = ResourceDownloadQueue(session: session)
public init() {
downloader = ResourceDownloadQueue()
}
internal init(session: URLSession, autoExecute: Bool) {
downloader = ResourceDownloadQueue(session: session, autoExecute: autoExecute)
}
// MARK: - Common functionality
@ -63,7 +67,10 @@ public class ResourceDownloadManager {
withFilename filename: String,
withOptions options: Options,
completionBlock: CompletionHandler<InstallableKeyboard>? = nil) -> DownloadBatch<InstallableKeyboard>? {
let startClosure = self.resourceDownloadStartClosure(for: keyboards)
var startClosure: (() -> Void)? = nil
if activity != .update {
startClosure = self.resourceDownloadStartClosure(for: keyboards)
}
let completionClosure = self.resourceDownloadCompletionClosure(for: keyboards, handler: completionBlock)
if let dlBatch = buildKeyboardDownloadBatch(for: keyboards[0],
withFilename: filename,
@ -112,7 +119,7 @@ public class ResourceDownloadManager {
request.destinationFile = Storage.active.cloudKeyboardURL(forID: keyboard.id).path
request.tag = 0
let keyboardTask = DownloadTask(do: request, for: [keyboard], type: .keyboard)
let keyboardTask = DownloadTask(do: request, for: [keyboard])
var batchTasks: [DownloadTask<InstallableKeyboard>] = [ keyboardTask ]
for (i, url) in fontURLs.enumerated() {
@ -120,11 +127,11 @@ public class ResourceDownloadManager {
request.destinationFile = Storage.active.fontURL(forResource: keyboard, filename: url.lastPathComponent)!.path
request.tag = i + 1
let fontTask = DownloadTask<InstallableKeyboard>(do: request, for: nil, type: nil)
let fontTask = DownloadTask<InstallableKeyboard>(do: request, for: nil)
batchTasks.append(fontTask)
}
let batch = DownloadBatch(do: batchTasks, as: activity, ofType: .keyboard, startBlock: startBlock, completionBlock: completionBlock)
let batch = DownloadBatch(do: batchTasks, startBlock: startBlock, completionBlock: completionBlock)
batchTasks.forEach { task in
task.request.userInfo[Key.downloadBatch] = batch
task.request.userInfo[Key.downloadTask] = task
@ -229,7 +236,10 @@ public class ResourceDownloadManager {
asActivity activity: DownloadActivityType,
fromPath path: URL,
completionBlock: CompletionHandler<InstallableLexicalModel>? = nil) -> DownloadBatch<InstallableLexicalModel>? {
let startClosure = self.resourceDownloadStartClosure(for: lexicalModels)
var startClosure: (() -> Void)? = nil
if activity != .update {
startClosure = self.resourceDownloadStartClosure(for: lexicalModels)
}
let completionClosure = self.resourceDownloadCompletionClosure(for: lexicalModels, handler: completionBlock)
if let dlBatch = buildLexicalModelDownloadBatch(for: lexicalModels[0],
withFilename: path,
@ -273,10 +283,10 @@ public class ResourceDownloadManager {
request.destinationFile = Storage.active.lexicalModelPackageURL(for: lexicalModel).path
request.tag = 0
let lexicalModelTask = DownloadTask(do: request, for: [lexicalModel], type: .lexicalModel)
let lexicalModelTask = DownloadTask(do: request, for: [lexicalModel])
let batchTasks: [DownloadTask<InstallableLexicalModel>] = [ lexicalModelTask ]
let batch = DownloadBatch(do: batchTasks, as: activity, ofType: .lexicalModel, startBlock: startBlock, completionBlock: completionBlock)
let batch = DownloadBatch(do: batchTasks, startBlock: startBlock, completionBlock: completionBlock)
batchTasks.forEach { task in
task.request.userInfo[Key.downloadBatch] = batch
task.request.userInfo[Key.downloadTask] = task

View file

@ -44,18 +44,15 @@ enum DownloadNode {
}
protocol AnyDownloadTask {
var type: LanguageResourceType? { get }
var request: HTTPDownloadRequest { get }
}
class DownloadTask<Resource: LanguageResource>: AnyDownloadTask {
public final var type: LanguageResourceType?
public final var resources: [Resource]?
public final var request: HTTPDownloadRequest
public init(do request: HTTPDownloadRequest, for resources: [Resource]?, type: LanguageResourceType?) {
public init(do request: HTTPDownloadRequest, for resources: [Resource]?) {
self.request = request
self.type = type
self.resources = resources
}
}
@ -65,8 +62,6 @@ enum DownloadActivityType {
}
protocol AnyDownloadBatch {
var activity: DownloadActivityType { get }
var type: LanguageResourceType? { get }
var tasks: [AnyDownloadTask] { get }
var resources: [AnyLanguageResource] { get }
@ -82,21 +77,14 @@ protocol AnyDownloadBatch {
* Represents one overall resource-related command for requests against the Keyman Cloud API.
*/
class DownloadBatch<Resource: LanguageResource>: AnyDownloadBatch where Resource.Package: TypedKeymanPackage<Resource> {
public final var activity: DownloadActivityType
public final var type: LanguageResourceType?
public final var downloadTasks: [DownloadTask<Resource>]
var errors: [Error?] // Only used by the ResourceDownloadQueue.
public final var startBlock: (() -> Void)? = nil
public final var completionBlock: ResourceDownloadManager.CompletionHandler<Resource>? = nil
public init?(do tasks: [DownloadTask<Resource>],
as activity: DownloadActivityType,
ofType type: LanguageResourceType,
startBlock: (() -> Void)? = nil,
completionBlock: ResourceDownloadManager.CompletionHandler<Resource>? = nil) {
self.activity = activity
self.type = type
self.downloadTasks = tasks
self.errors = Array(repeating: nil, count: tasks.count)
@ -494,12 +482,7 @@ class ResourceDownloadQueue: HTTPDownloadDelegate {
}
func downloadRequestStarted(_ request: HTTPDownloadRequest) {
// If we're downloading a new keyboard.
// The extra check is there to filter out other potential request types in the future.
let batch = request.userInfo[Key.downloadBatch] as! AnyDownloadBatch
// TODO: remove the != .update check - that should be handled when startBlocks are assigned.
if batch.activity != .update {
if let batch = request.userInfo[Key.downloadBatch] as? AnyDownloadBatch {
batch.startBlock?()
}
}