diff --git a/ios/engine/KMEI/KeymanEngine/Classes/KeymanPackage.swift b/ios/engine/KMEI/KeymanEngine/Classes/KeymanPackage.swift index dd94769db0..9698777dce 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/KeymanPackage.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/KeymanPackage.swift @@ -472,6 +472,7 @@ public class KeymanPackage { */ public class TypedKeymanPackage: KeymanPackage { public private(set) var installables: [[TypedLanguageResource]] = [] + typealias Resource = TypedLanguageResource internal func setInstallableResourceSets(for kmpResources: [Resource]) where TypedLanguageResource: KMPInitializableLanguageResource { diff --git a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/PackageInstallViewController.swift b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/PackageInstallViewController.swift index c5c2ec0b0e..7c80a6e096 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/PackageInstallViewController.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/PackageInstallViewController.swift @@ -9,15 +9,15 @@ import Foundation import WebKit -public class PackageInstallViewController: UIViewController { - public typealias CompletionHandler = (Error?) -> Void +public class PackageInstallViewController: UIViewController { + public typealias CompletionHandler = ([Resource.FullID]?) -> Void - let package: KeymanPackage + let package: Resource.Package var wkWebView: WKWebView? let completionHandler: CompletionHandler let isCustom: Bool - public init(for package: KeymanPackage, isCustom: Bool, completionHandler: @escaping CompletionHandler) { + public init(for package: Resource.Package, isCustom: Bool, completionHandler: @escaping CompletionHandler) { self.package = package self.completionHandler = completionHandler self.isCustom = isCustom @@ -59,16 +59,20 @@ public class PackageInstallViewController: UIViewController { } @objc func cancelBtnHandler() { - dismiss(animated: true, completion: nil) + dismiss(animated: true, completion: { + self.completionHandler(nil) + }) } @objc func installBtnHandler() { dismiss(animated: true, completion: { - do { - try ResourceFileManager.shared.finalizePackageInstall(self.package, isCustom: self.isCustom) - self.completionHandler(nil) - } catch { - self.completionHandler(error) + // A stop-gap pending further changes. + let defaultResource = self.package.installableResourceSets[0] as! [Resource] + switch self.package.resourceType() { + case .keyboard: + self.completionHandler([defaultResource[0].typedFullID]) + case .lexicalModel: + self.completionHandler(defaultResource.map { $0.typedFullID }) } }) } diff --git a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceFileManager.swift b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceFileManager.swift index 6666a84631..060c777afa 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceFileManager.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/Resource Management/ResourceFileManager.swift @@ -199,26 +199,52 @@ public class ResourceFileManager { } } - public func promptPackageInstall(of package: KeymanPackage, + internal func typedPromptForPackageInstall>( + of package: Package, in rootVC: UIViewController, isCustom: Bool, - successHandler: ((KeymanPackage) -> Void)? = nil) { - let vc = PackageInstallViewController(for: package, isCustom: isCustom, completionHandler: { error in - if let err = error { - if let kmpError = err as? KMPError { - let alert = self.buildKMPError(kmpError) - rootVC.present(alert, animated: true, completion: nil) + successHandler: ((Package) -> Void)? = nil) -> PackageInstallViewController + where Package == Resource.Package { + return PackageInstallViewController(for: package, isCustom: isCustom, completionHandler: { fullIDs in + + if let fullIDs = fullIDs { + do { + //try ResourceFileManager.shared.finalizePackageInstall(self.package, isCustom: self.isCustom) + try ResourceFileManager.shared.install(resourcesWithIDs: fullIDs, from: package) + } catch { + if let kmpError = error as? KMPError { + let alert = self.buildKMPError(kmpError) + rootVC.present(alert, animated: true, completion: nil) + } } - } else { + let alert = self.buildSimpleAlert(title: "Success", message: "Installed successfully.", completionHandler: { successHandler?(package) }) rootVC.present(alert, animated: true, completion: nil) } }) + } - let nvc = UINavigationController.init(rootViewController: vc) - rootVC.present(nvc, animated: true, completion: nil) + public func promptPackageInstall(of package: KeymanPackage, + in rootVC: UIViewController, + isCustom: Bool, + successHandler: ((KeymanPackage) -> Void)? = nil) { + var vc: UIViewController? = nil + if let kbdPackage = package as? KeyboardKeymanPackage { + vc = typedPromptForPackageInstall(of: kbdPackage, in: rootVC, isCustom: isCustom) { package in + successHandler?(package) + } + } else if let lmPackage = package as? LexicalModelKeymanPackage { + vc = typedPromptForPackageInstall(of: lmPackage, in: rootVC, isCustom: isCustom) { package in + successHandler?(package) + } + } + + if let vc = vc { + let nvc = UINavigationController.init(rootViewController: vc) + rootVC.present(nvc, animated: true, completion: nil) + } } public func buildKMPError(_ error: KMPError) -> UIAlertController {