refactor(ios/engine): download start notifications

This commit is contained in:
jahorton 2020-06-23 12:33:13 +07:00
parent 6e3886fca5
commit fd69ef04ae
2 changed files with 21 additions and 17 deletions

View file

@ -115,8 +115,9 @@ public class ResourceDownloadManager {
let fontTask = DownloadTask<InstallableKeyboard>(do: request, for: nil, type: nil)
batchTasks.append(fontTask)
}
let batch = DownloadBatch(do: batchTasks, as: activity, ofType: .keyboard, completionBlock: completionBlock)
let startHandler = { self.defaultKeyboardStartBlock(for: keyboard) }
let batch = DownloadBatch(do: batchTasks, as: activity, ofType: .keyboard, startBlock: startHandler, completionBlock: completionBlock)
batchTasks.forEach { task in
task.request.userInfo[Key.downloadBatch] = batch
task.request.userInfo[Key.downloadTask] = task
@ -125,6 +126,12 @@ public class ResourceDownloadManager {
return batch
}
internal func defaultKeyboardStartBlock(for keyboard: InstallableKeyboard) {
NotificationCenter.default.post(name: Notifications.keyboardDownloadStarted,
object: self,
value: [keyboard])
}
/// Asynchronously fetches the .js file for the keyboard with given IDs.
/// See `Notifications` for notification on success/failiure.
/// - Parameters:
@ -257,8 +264,9 @@ public class ResourceDownloadManager {
let lexicalModelTask = DownloadTask(do: request, for: [lexicalModel], type: .lexicalModel)
let batchTasks: [DownloadTask<InstallableLexicalModel>] = [ lexicalModelTask ]
let batch = DownloadBatch(do: batchTasks, as: activity, ofType: .lexicalModel, completionBlock: completionBlock)
let startHandler = { self.defaultLexicalModelStartBlock(for: lexicalModel) }
let batch = DownloadBatch(do: batchTasks, as: activity, ofType: .lexicalModel, startBlock: startHandler, completionBlock: completionBlock)
batchTasks.forEach { task in
task.request.userInfo[Key.downloadBatch] = batch
task.request.userInfo[Key.downloadTask] = task
@ -266,6 +274,12 @@ public class ResourceDownloadManager {
return batch
}
internal func defaultLexicalModelStartBlock(for lexicalModel: InstallableLexicalModel) {
NotificationCenter.default.post(name: Notifications.lexicalModelDownloadStarted,
object: self,
value: [lexicalModel])
}
// Can be called by the cloud keyboard downloader and utilized.

View file

@ -100,6 +100,7 @@ class DownloadBatch<Resource: LanguageResource>: AnyDownloadBatch {
self.downloadTasks = tasks
self.errors = Array(repeating: nil, count: tasks.count)
self.startBlock = startBlock
self.completionBlock = completionBlock
}
@ -546,19 +547,8 @@ class ResourceDownloadQueue: HTTPDownloadDelegate {
// The extra check is there to filter out other potential request types in the future.
let batch = request.userInfo[Key.downloadBatch] as! AnyDownloadBatch
if request.tag == 0 && batch.activity != .update {
let task = request.userInfo[Key.downloadTask] as! AnyDownloadTask
if task.type == .keyboard {
let task = task as! DownloadTask<InstallableKeyboard>
NotificationCenter.default.post(name: Notifications.keyboardDownloadStarted,
object: self,
value: task.resources!)
} else if task.type == .lexicalModel {
let task = task as! DownloadTask<InstallableLexicalModel>
NotificationCenter.default.post(name: Notifications.lexicalModelDownloadStarted,
object: self,
value: task.resources!)
}
if batch.activity != .update {
batch.startBlock?()
}
}