From ec06081223fc6aaa61d21892f7d90cd4e4db90b9 Mon Sep 17 00:00:00 2001 From: jahorton Date: Fri, 26 Jun 2020 15:25:17 +0700 Subject: [PATCH] refactor(ios/engine): drops unused .activity, .type --- .../ResourceDownloadManager.swift | 30 ++++++++++++------- .../ResourceDownloadQueue.swift | 21 ++----------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadManager.swift b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadManager.swift index 78690c7367..5f60c05bf9 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadManager.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadManager.swift @@ -20,9 +20,13 @@ public class ResourceDownloadManager { public typealias CompletionHandler = (Resource.Package?, Error?) -> Void where Resource.Package: TypedKeymanPackage 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? = nil) -> DownloadBatch? { - 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] = [ 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(do: request, for: nil, type: nil) + let fontTask = DownloadTask(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? = nil) -> DownloadBatch? { - 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] = [ 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 diff --git a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadQueue.swift b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadQueue.swift index 5bde36237c..ffce91c2c2 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadQueue.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceDownloadQueue.swift @@ -44,18 +44,15 @@ enum DownloadNode { } protocol AnyDownloadTask { - var type: LanguageResourceType? { get } var request: HTTPDownloadRequest { get } } class DownloadTask: 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: AnyDownloadBatch where Resource.Package: TypedKeymanPackage { - public final var activity: DownloadActivityType - public final var type: LanguageResourceType? - public final var downloadTasks: [DownloadTask] var errors: [Error?] // Only used by the ResourceDownloadQueue. public final var startBlock: (() -> Void)? = nil public final var completionBlock: ResourceDownloadManager.CompletionHandler? = nil public init?(do tasks: [DownloadTask], - as activity: DownloadActivityType, - ofType type: LanguageResourceType, startBlock: (() -> Void)? = nil, completionBlock: ResourceDownloadManager.CompletionHandler? = 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?() } }