mirror of
https://github.com/keymanapp/keyman.git
synced 2026-10-20 04:47:42 +00:00
2067 lines
75 KiB
Swift
2067 lines
75 KiB
Swift
//
|
|
// Manager.swift
|
|
// KeymanEngine
|
|
//
|
|
// Created by Gabriel Wong on 2017-10-06.
|
|
// Copyright © 2017 SIL International. All rights reserved.
|
|
//
|
|
|
|
import CoreText
|
|
import UIKit
|
|
import WebKit
|
|
|
|
typealias FetchKeyboardsBlock = ([String: Any]?) -> Void
|
|
|
|
// MARK: - Constants
|
|
|
|
// Possible states that a keyboard can be in
|
|
public enum KeyboardState {
|
|
case needsDownload
|
|
case needsUpdate
|
|
case upToDate
|
|
case downloading
|
|
case none
|
|
}
|
|
|
|
// TODO: Use a struct
|
|
|
|
// Strings
|
|
private let keyboardChangeHelpText = "Tap here to change keyboard"
|
|
|
|
// URLs and Filenames
|
|
private let apiBaseURL = "https://r.keymanweb.com/api/4.0/"
|
|
private let apiRemoteURL = "https://r.keymanweb.com/api/2.0/remote?url="
|
|
private let kmwFileName = "keyboard"
|
|
private let kmwFileExtension = "html"
|
|
private let kmwFullFileName = "\(kmwFileName).\(kmwFileExtension)"
|
|
private let iOSCodeFileName = "keymanios.js"
|
|
private let keymanHostName = "r.keymanweb.com"
|
|
|
|
// UI In-App Keyboard Constants
|
|
private let phonePortraitInAppKeyboardHeight: CGFloat = 183.0
|
|
private let phoneLandscapeInAppKeyboardHeight: CGFloat = 183.0
|
|
private let padPortraitInAppKeyboardHeight: CGFloat = 385.0
|
|
private let padLandscapeInAppKeyboardHeight: CGFloat = 385.0
|
|
|
|
// UI System Keyboard Constants
|
|
private let phonePortraitSystemKeyboardHeight: CGFloat = 216.0
|
|
private let phoneLandscapeSystemKeyboardHeight: CGFloat = 162.0
|
|
private let padPortraitSystemKeyboardHeight: CGFloat = 264.0
|
|
private let padLandscapeSystemKeyboardHeight: CGFloat = 352.0
|
|
|
|
public class Manager: NSObject, HTTPDownloadDelegate, UIGestureRecognizerDelegate, KeymanWebDelegate {
|
|
/// Application group identifier for shared container. Set this before accessing the shared manager.
|
|
public static var applicationGroupIdentifier: String?
|
|
|
|
public static let shared = Manager()
|
|
|
|
public var isDebugPrintingOn = false
|
|
|
|
/// Display the help bubble on first use.
|
|
public var isKeymanHelpOn = true
|
|
|
|
// TODO: Change API to not disable removing as well
|
|
/// Allow users to add new keyboards in the keyboard picker.
|
|
/// - Default value is true.
|
|
/// - Setting this to false will also disable keyboard removal. To enable keyboard removal you should set
|
|
/// canRemoveKeyboards to true.
|
|
/// - If set to false, calling fetchKeyboardList() is unnecessary and should be avoided unless you want to use auto
|
|
/// keyboard update check feature of the keyboard picker.
|
|
public var canAddNewKeyboards: Bool {
|
|
get {
|
|
return _canAddNewKeyboards
|
|
}
|
|
set(canAddNewKeyboards) {
|
|
_canAddNewKeyboards = canAddNewKeyboards
|
|
if !canAddNewKeyboards {
|
|
canRemoveKeyboards = false
|
|
}
|
|
}
|
|
}
|
|
private var _canAddNewKeyboards = true
|
|
|
|
/// Allow users to remove keyboards.
|
|
/// - Default value is true.
|
|
/// - The default keyboard is additionally prevented from being removed by canRemoveDefaultkeyboard.
|
|
public var canRemoveKeyboards = true
|
|
|
|
/// Allow the default keyboard to be removed.
|
|
/// The last keyboard cannot be removed, so the default keyboard cannot be removed if it
|
|
/// is the only keyboard in the list, regardless of the value of this property.
|
|
/// The default value is false.
|
|
public var canRemoveDefaultKeyboard = false
|
|
|
|
// TODO: Use a struct instead of dictionaries with fixed keys
|
|
/// The list of Keyman languages once they have been fetched.
|
|
/// - Each language is an NSDictionary with a name, id, and a list of keyboards
|
|
/// - Each keyboard is itself an NSDictionary with id, name, etc
|
|
/// - All you should ever require from this list are names and IDs
|
|
/// - If you find yourself using other info (like the URI), there is probably a Manager method that does what you
|
|
/// want already
|
|
/// - This list won't be available until .languagesUpdated has been broadcasted
|
|
public private(set) var languages: [Language] = []
|
|
|
|
/// Dictionary of Keyman keyboards to store language and keyboard names etc.
|
|
///
|
|
/// The key format is $languageID_$keyboardID. For example, "eng_european2" returns the English EuroLatin2 keyboard
|
|
public private(set) var keyboardsDictionary: [String: InstallableKeyboard] = [:]
|
|
|
|
/// Dictionary of available Keyman keyboard fonts keyed by font filename
|
|
public private(set) var keymanFonts: [String: RegisteredFont] = [:]
|
|
|
|
/// In keyboard extensions (system keyboard), `UIApplication.openURL(_:)` is unavailable. The API is not called in
|
|
/// the system keyboard since `KeyboardInfoViewController` is never used. `openURL(:_)` is only used in applications,
|
|
/// where it is safe. However, the entire Keyman Engine framework must be compiled with extension-safe APIs.
|
|
///
|
|
/// Set this to `UIApplication.shared.openURL` in your application.
|
|
public var openURL: ((URL) -> Bool)?
|
|
|
|
var keyboardID: String?
|
|
var languageID: String?
|
|
weak var keymanWebDelegate: KeymanWebDelegate?
|
|
var currentRequest: HTTPDownloadRequest?
|
|
var keyboardsInfo: [String: Keyboard]?
|
|
var shouldReloadKeyboard = false
|
|
var keymanWeb: KeymanWebViewController! = nil
|
|
|
|
private var downloadQueue: HTTPDownloader?
|
|
private var sharedQueue: HTTPDownloader!
|
|
private var reachability: Reachability!
|
|
private var didSynchronize = false
|
|
private var didResizeToOrientation = false
|
|
private var lastKeyboardSize: CGSize = .zero
|
|
private var specialOSKFont: String?
|
|
|
|
private let subKeyColor = #colorLiteral(red: 244.0 / 255.0, green: 244.0 / 255.0, blue: 244.0 / 255.0, alpha: 1.0)
|
|
private let subKeyColorHighlighted = #colorLiteral(red: 136.0 / 255.0, green: 136.0 / 255.0, blue: 1.0, alpha: 1.0)
|
|
|
|
// Views
|
|
private var helpBubbleView: PopoverView?
|
|
private var keyPreviewView: KeyPreviewView?
|
|
private var subKeysView: SubKeysView?
|
|
private var keyboardMenuView: KeyboardMenuView?
|
|
|
|
// Arrays
|
|
private var subKeyIDs: [String] = []
|
|
private var subKeyTexts: [String] = []
|
|
private var subKeys: [UIButton] = []
|
|
|
|
// Key frames
|
|
private var keyFrame = CGRect.zero
|
|
private var menuKeyFrame = CGRect.zero
|
|
|
|
// Dictionary of Keyman options
|
|
var options: Options?
|
|
|
|
// MARK: - Object Admin
|
|
deinit {
|
|
NotificationCenter.default.removeObserver(self)
|
|
// FIXME: Likely unneeded unless a reference exists to currentRequest outside of Manager
|
|
if let currentRequest = currentRequest {
|
|
currentRequest.userInfo["completionBlock"] = nil
|
|
}
|
|
}
|
|
|
|
private override init() {
|
|
super.init()
|
|
|
|
URLProtocol.registerClass(KeymanURLProtocol.self)
|
|
|
|
if !Util.isSystemKeyboard {
|
|
copyUserDefaultsToSharedContainer()
|
|
copyKeymanFilesToSharedContainer()
|
|
let userData = activeUserDefaults()
|
|
let isKPDisplayed = userData.bool(forKey: Key.keyboardPickerDisplayed)
|
|
if isKPDisplayed {
|
|
isKeymanHelpOn = false
|
|
}
|
|
} else {
|
|
isKeymanHelpOn = false
|
|
}
|
|
|
|
copyWebFilesToLibrary()
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow),
|
|
name: .UIKeyboardWillShow, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide),
|
|
name: .UIKeyboardWillHide, object: nil)
|
|
|
|
let kbVersion = latestKeyboardFileVersion(withID: Defaults.keyboard.id)
|
|
updateKeyboardVersion(forID: Defaults.keyboard.id, newKeyboardVersion: kbVersion!)
|
|
|
|
keymanWeb = KeymanWebViewController(nibName: nil, bundle: nil)
|
|
keymanWeb.frame = CGRect(origin: .zero, size: keyboardSize)
|
|
keymanWeb.delegate = self
|
|
reloadKeyboard(in: keymanWeb)
|
|
|
|
// Set UILongPressGestureRecognizer to show sub keys
|
|
// TODO: Move to KeymanWebViewController
|
|
let hold = UILongPressGestureRecognizer(target: self, action: #selector(self.holdAction))
|
|
hold.minimumPressDuration = 0.5
|
|
hold.delegate = self
|
|
keymanWeb.view.addGestureRecognizer(hold)
|
|
|
|
reachability = Reachability(hostName: keymanHostName)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged),
|
|
name: .reachabilityChanged, object: reachability)
|
|
reachability.startNotifier()
|
|
|
|
/* HTTPDownloader only uses this for its delegate methods. So long as we don't
|
|
* set the queue running, this should be perfectly fine.
|
|
*/
|
|
sharedQueue = HTTPDownloader.init(self)
|
|
registerCustomFonts()
|
|
}
|
|
|
|
// MARK: - Keyboard management
|
|
|
|
/// Sets the current keyboard, querying from the user's list of keyboards.
|
|
///
|
|
/// - Precondition:
|
|
/// - The keyboard must be added with `addKeyboard()`.
|
|
///
|
|
/// - SeeAlso:
|
|
/// - addKeyboard()
|
|
/// - Returns: Whether the keyboard was set successfully
|
|
public func setKeyboard(withID keyboardID: String, languageID: String) -> Bool {
|
|
if let keyboard = activeUserDefaults().userKeyboard(withID: keyboardID, languageID: languageID) {
|
|
return setKeyboard(keyboard)
|
|
}
|
|
return false
|
|
}
|
|
|
|
/// Set the current keyboard.
|
|
///
|
|
/// - Returns: Whether the keyboard was set successfully
|
|
public func setKeyboard(_ kb: InstallableKeyboard) -> Bool {
|
|
if kb.languageID == self.languageID && kb.id == self.keyboardID {
|
|
kmLog("Keyboard unchanged: \(kb.languageID)_\(kb.id)", checkDebugPrinting: true)
|
|
return false
|
|
}
|
|
|
|
kmLog("Setting language: \(kb.languageID)_\(kb.id)", checkDebugPrinting: true)
|
|
if usingTempFolder {
|
|
if !copyKeymanFilesToTemp() {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// FIXME: kb.version is not respected. Ideally we should be able to trust that the version number in UserDefaults
|
|
// is-to-date but it is sometimes not updated.
|
|
guard let kbVersion = latestKeyboardFileVersion(withID: kb.id) else {
|
|
kmLog("Could not set keyboardID to \(kb.id) because the keyboard file does not exist",
|
|
checkDebugPrinting: false)
|
|
// Fallback to default keyboard if no keyboard is currently set.
|
|
if (self.keyboardID == nil || self.languageID == nil) && kb.id != Defaults.keyboard.id {
|
|
_ = setKeyboard(Defaults.keyboard)
|
|
}
|
|
return false
|
|
}
|
|
|
|
self.languageID = kb.languageID
|
|
self.keyboardID = kb.id
|
|
|
|
let jsFont = self.jsFont(fromFont: kb.font) ?? "undefined"
|
|
let jsOskFont: String
|
|
if let oskFont = kb.oskFont {
|
|
jsOskFont = self.jsFont(fromFont: oskFont) ?? "undefined"
|
|
} else {
|
|
jsOskFont = jsFont
|
|
}
|
|
|
|
keymanWeb.setKeyboard(id: kb.id, name: kb.name, languageID: kb.languageID, languageName: kb.languageName,
|
|
version: kbVersion, font: jsFont, oskFont: jsOskFont)
|
|
|
|
let userData = Util.isSystemKeyboard ? UserDefaults.standard : activeUserDefaults()
|
|
|
|
userData.currentKeyboard = kb
|
|
userData.synchronize()
|
|
|
|
if isKeymanHelpOn {
|
|
helpBubbleView?.removeFromSuperview()
|
|
let showHelpBubble = #selector(self.showHelpBubble as () -> Void)
|
|
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: showHelpBubble, object: nil)
|
|
perform(showHelpBubble, with: nil, afterDelay: 1.5)
|
|
}
|
|
|
|
NotificationCenter.default.post(name: Notifications.keyboardChanged,
|
|
object: self,
|
|
value: kb)
|
|
return true
|
|
}
|
|
|
|
/// Adds a new keyboard to the list in the keyboard picker if it doesn't already exist.
|
|
/// The keyboard must be downloaded (see `downloadKeyboard()`) or preloaded (see `preloadLanguageFile()`)
|
|
///
|
|
/// - Parameters:
|
|
/// - isRTL: The writing direction is right to left
|
|
/// - isCustom: The keyboard is not provided by Keyman
|
|
/// - font: Custom font for text views as a JSON String (see keyboardsDictionary)
|
|
/// - oskFont: Font for the on-screen keyboard
|
|
public func addKeyboard(_ keyboard: InstallableKeyboard) {
|
|
if !keyboardFileExists(withID: keyboard.id, version: keyboard.version) {
|
|
kmLog("Could not add keyboard with ID: \(keyboard.id) because the keyboard file does not exist",
|
|
checkDebugPrinting: false)
|
|
return
|
|
}
|
|
|
|
// Get keyboards list if it exists in user defaults, otherwise create a new one
|
|
let userDefaults = activeUserDefaults()
|
|
var userKeyboards = userDefaults.userKeyboards ?? []
|
|
|
|
// Update keyboard if it exists
|
|
if let index = userKeyboards.index(where: { $0.id == keyboard.id && $0.languageID == keyboard.languageID }) {
|
|
userKeyboards[index] = keyboard
|
|
} else {
|
|
userKeyboards.append(keyboard)
|
|
}
|
|
|
|
userDefaults.userKeyboards = userKeyboards
|
|
userDefaults.set([Date()], forKey: Key.synchronizeSWKeyboard)
|
|
userDefaults.synchronize()
|
|
}
|
|
|
|
/// Removes a keyboard from the list in the keyboard picker if it exists.
|
|
/// - Returns: The keyboard exists and was removed
|
|
public func removeKeyboard(withID keyboardID: String, languageID: String) -> Bool {
|
|
// Remove keyboard from the list if it exists
|
|
let index = activeUserDefaults().userKeyboards?.index { $0.id == keyboardID && $0.languageID == languageID }
|
|
if let index = index {
|
|
return removeKeyboard(at: index)
|
|
}
|
|
return false
|
|
}
|
|
|
|
/// Removes the keyboard at index from the keyboards list if it exists.
|
|
/// - Returns: The keyboard exists and was removed
|
|
public func removeKeyboard(at index: Int) -> Bool {
|
|
let userData = activeUserDefaults()
|
|
|
|
// If user defaults for keyboards list does not exist, do nothing.
|
|
guard var userKeyboards = userData.userKeyboards else {
|
|
return false
|
|
}
|
|
|
|
guard index < userKeyboards.count else {
|
|
return false
|
|
}
|
|
|
|
let kb = userKeyboards[index]
|
|
userKeyboards.remove(at: index)
|
|
userData.userKeyboards = userKeyboards
|
|
userData.set([Date()], forKey: Key.synchronizeSWKeyboard)
|
|
userData.synchronize()
|
|
|
|
// Set a new keyboard if deleting the current one
|
|
if kb.id == keyboardID && kb.languageID == languageID {
|
|
setKeyboard(userKeyboards[0])
|
|
}
|
|
|
|
NotificationCenter.default.post(name: Notifications.keyboardRemoved, object: self, value: kb)
|
|
return true
|
|
}
|
|
|
|
public func repositoryKeyboard(withID keyboardID: String, languageID: String) -> InstallableKeyboard? {
|
|
return keyboardsDictionary["\(languageID)_\(keyboardID)"]
|
|
}
|
|
|
|
/// - Returns: Info for the current keyboard, if a keyboard is set
|
|
public var currentKeyboardInfo: InstallableKeyboard? {
|
|
guard let keyboardID = keyboardID, let languageID = languageID else {
|
|
return nil
|
|
}
|
|
return activeUserDefaults().userKeyboard(withID: keyboardID, languageID: languageID)
|
|
}
|
|
|
|
/// Switch to the next keyboard.
|
|
/// - Returns: Index of the newly selected keyboard.
|
|
public func switchToNextKeyboard() -> Int? {
|
|
let userDefaults = activeUserDefaults()
|
|
guard let userKeyboards = userDefaults.userKeyboards,
|
|
let index = userKeyboards.index(where: { isCurrentKeyboard($0) }) else {
|
|
return nil
|
|
}
|
|
let newIndex = (index + 1) % userKeyboards.count
|
|
setKeyboard(userKeyboards[newIndex])
|
|
return newIndex
|
|
}
|
|
|
|
func isCurrentKeyboard(withID keyboardID: String?, languageID: String?) -> Bool {
|
|
return self.keyboardID == keyboardID && self.languageID == languageID
|
|
}
|
|
|
|
func isCurrentKeyboard(_ keyboard: InstallableKeyboard) -> Bool {
|
|
return keyboard.id == self.keyboardID && keyboard.languageID == self.languageID
|
|
}
|
|
|
|
/// - Returns: The font name for the given keyboard ID and languageID, or returns nil if
|
|
/// - The keyboard doesn't have a font
|
|
/// - The keyboard info is not available in the user keyboards list or in keyboardsDictionary
|
|
public func fontNameForKeyboard(withID keyboardID: String, languageID: String) -> String? {
|
|
let kb = activeUserDefaults().userKeyboard(withID: keyboardID, languageID: languageID)
|
|
?? repositoryKeyboard(withID: keyboardID, languageID: languageID)
|
|
if let filename = kb?.font?.source.first(where: { $0.hasFontExtension }) {
|
|
return keymanFonts[filename]?.name
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/// - Returns: the OSK font name for the given keyboard ID and languageID, or returns nil if
|
|
/// - The keyboard doesn't have an OSK font
|
|
/// - The keyboard info is not available in the user keyboards list or in keyboardsDictionary
|
|
func oskFontNameForKeyboard(withID keyboardID: String, languageID: String) -> String? {
|
|
let kb = activeUserDefaults().userKeyboard(withID: keyboardID, languageID: languageID)
|
|
?? repositoryKeyboard(withID: keyboardID, languageID: languageID)
|
|
if let filename = kb?.oskFont?.source.first(where: { $0.hasFontExtension }) {
|
|
return keymanFonts[filename]?.name
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func isRTLKeyboard(withID keyboardID: String, languageID: String) -> Bool? {
|
|
let kb = activeUserDefaults().userKeyboard(withID: keyboardID, languageID: languageID)
|
|
?? repositoryKeyboard(withID: keyboardID, languageID: languageID)
|
|
return kb?.isRTL
|
|
}
|
|
|
|
func jsFont(fromFont font: Font?) -> String? {
|
|
guard let font = font else {
|
|
return jsFont(fromFontDictionary: nil)
|
|
}
|
|
return jsFont(fromFontDictionary: [
|
|
Key.fontFamily: font.family,
|
|
Key.fontSource: font.source,
|
|
"size": font.size
|
|
])
|
|
}
|
|
|
|
func jsFont(fromFontDictionary fontDict: [AnyHashable: Any]?) -> String? {
|
|
guard let fontDict = fontDict, !fontDict.isEmpty else {
|
|
return nil
|
|
}
|
|
|
|
let data: Data
|
|
do {
|
|
data = try JSONSerialization.data(withJSONObject: fontDict, options: [])
|
|
} catch {
|
|
kmLog("Failed to encode font dictionary as JSON: \(String(describing: fontDict))", checkDebugPrinting: false)
|
|
return nil
|
|
}
|
|
|
|
return String(data: data, encoding: .ascii)!
|
|
.replacingOccurrences(of: Key.fontFilename, with: Key.fontFiles)
|
|
.replacingOccurrences(of: Key.fontSource, with: Key.fontFiles)
|
|
}
|
|
|
|
// MARK: - Downloading keyboards
|
|
|
|
/// Asynchronously fetches the dictionary of possible languages/keyboards to be displayed in the keyboard picker.
|
|
/// If not called before the picker is shown, the dictionary will be fetched automatically.
|
|
/// This method allows you to fetch the info in advance at a time that's appropriate for your app.
|
|
/// See `Notifications` for a list of relevant notifications.
|
|
///
|
|
/// To save bandwidth, a cached version is used if:
|
|
/// - the Keyman server is unreachable
|
|
/// - the list has been recently fetched
|
|
public func fetchKeyboardsList() {
|
|
// TODO: Merge with this function
|
|
fetchKeyboards(completionBlock: nil)
|
|
}
|
|
|
|
// This function appears to fetch the keyboard metadata from r.keymanweb.com.
|
|
func fetchKeyboards(completionBlock: FetchKeyboardsBlock? = nil) {
|
|
if currentRequest != nil {
|
|
return
|
|
}
|
|
|
|
let deviceType = (UIDevice.current.userInterfaceIdiom == .phone) ? "iphone" : "ipad"
|
|
let url = URL(string: "\(apiBaseURL)languages?dateformat=seconds&device=\(deviceType)")!
|
|
let userData = completionBlock.map { ["completionBlock": $0] } ?? [:]
|
|
|
|
let request = HTTPDownloadRequest(url: url, downloadType: .downloadCachedData, userInfo: userData)
|
|
currentRequest = request
|
|
sharedQueue.addRequest(request)
|
|
sharedQueue.run()
|
|
}
|
|
|
|
/// Asynchronously fetches the .js file for the keyboard with given IDs.
|
|
/// See `Notifications` for notification on success/failiure.
|
|
/// - Parameters:
|
|
/// - isUpdate: Keep the keyboard files on failure
|
|
public func downloadKeyboard(withID keyboardID: String, languageID: String, isUpdate: Bool) {
|
|
guard let keyboardsInfo = keyboardsInfo else {
|
|
let message = "Keyboard info has not yet been fetched. Call fetchKeyboardsList() first."
|
|
let error = NSError(domain: "Keyman", code: 0, userInfo: [NSLocalizedDescriptionKey: message])
|
|
downloadFailed(forKeyboards: [], error: error)
|
|
return
|
|
}
|
|
|
|
guard let keyboard = repositoryKeyboard(withID: keyboardID, languageID: languageID) else {
|
|
let message = "Keyboard not found with id: \(keyboardID), languageID: \(languageID)"
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: message])
|
|
downloadFailed(forKeyboards: [], error: error)
|
|
return
|
|
}
|
|
|
|
guard downloadQueue == nil else {
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "Download queue is busy"])
|
|
downloadFailed(forKeyboards: [keyboard], error: error)
|
|
return
|
|
}
|
|
|
|
guard reachability.currentReachabilityStatus() != NotReachable else {
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "No internet connection"])
|
|
downloadFailed(forKeyboards: [keyboard], error: error)
|
|
return
|
|
}
|
|
|
|
let filename = keyboardsInfo[keyboardID]!.filename
|
|
let keyboardURL = options!.keyboardBaseURL.appendingPathComponent(filename)
|
|
|
|
let fontURLs = Array(Set(keyboardFontURLs(forFont: keyboard.font, options: options!) +
|
|
keyboardFontURLs(forFont: keyboard.oskFont, options: options!)))
|
|
|
|
// TODO: Better typing
|
|
downloadQueue = HTTPDownloader(self)
|
|
let commonUserData: [String: Any] = [
|
|
Key.keyboardInfo: [keyboard],
|
|
Key.update: isUpdate
|
|
]
|
|
downloadQueue!.userInfo = commonUserData
|
|
|
|
var request = HTTPDownloadRequest(url: keyboardURL, userInfo: commonUserData)
|
|
request.destinationFile = keyboardPath(forID: keyboardID, keyboardVersion: keyboard.version)?.path
|
|
request.tag = 0
|
|
downloadQueue!.addRequest(request)
|
|
|
|
for (i, url) in fontURLs.enumerated() {
|
|
request = HTTPDownloadRequest(url: url, userInfo: commonUserData)
|
|
request.destinationFile = fontPath(forFilename: url.lastPathComponent)?.path
|
|
request.tag = i + 1
|
|
downloadQueue!.addRequest(request)
|
|
}
|
|
downloadQueue!.run()
|
|
}
|
|
|
|
private func keyboardFontURLs(forFont font: Font?, options: Options) -> [URL] {
|
|
guard let font = font else {
|
|
return []
|
|
}
|
|
return font.source.filter({ $0.hasFontExtension })
|
|
.map({ options.fontBaseURL.appendingPathComponent($0) })
|
|
}
|
|
|
|
/// Downloads a custom keyboard from the URL
|
|
/// - Parameters:
|
|
/// - url: URL to a JSON description of the keyboard
|
|
public func downloadKeyboard(from url: URL) {
|
|
guard reachability.currentReachabilityStatus() != NotReachable else {
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "No connection"])
|
|
downloadFailed(forKeyboards: [], error: error)
|
|
return
|
|
}
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "Failed to fetch JSON file"])
|
|
downloadFailed(forKeyboards: [], error: error)
|
|
return
|
|
}
|
|
|
|
let decoder = JSONDecoder()
|
|
decoder.dateDecodingStrategy = .ios8601WithFallback
|
|
do {
|
|
let keyboard = try decoder.decode(KeyboardAPICall.self, from: data)
|
|
return downloadKeyboard(keyboard)
|
|
} catch {
|
|
downloadFailed(forKeyboards: [], error: error)
|
|
return
|
|
}
|
|
}
|
|
|
|
/// Assumes that Keyboard has font and oskFont set and ignores fonts contained in Language.
|
|
private func downloadKeyboard(_ keyboardAPI: KeyboardAPICall) {
|
|
let keyboard = keyboardAPI.keyboard
|
|
let installableKeyboards = keyboard.languages!.map { language in
|
|
InstallableKeyboard(keyboard: keyboard, language: language)
|
|
}
|
|
|
|
let filename = keyboard.filename
|
|
let keyboardURL = keyboardAPI.options.keyboardBaseURL.appendingPathComponent(filename)
|
|
|
|
let fontURLs = Array(Set(keyboardFontURLs(forFont: keyboard.font, options: keyboardAPI.options) +
|
|
keyboardFontURLs(forFont: keyboard.oskFont, options: keyboardAPI.options)))
|
|
|
|
if downloadQueue != nil {
|
|
// Download queue is active.
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "Download queue is busy"])
|
|
downloadFailed(forKeyboards: installableKeyboards, error: error)
|
|
return
|
|
}
|
|
|
|
if reachability.currentReachabilityStatus() == NotReachable {
|
|
let error = NSError(domain: "Keyman", code: 0, userInfo: [NSLocalizedDescriptionKey: "No internet connection"])
|
|
downloadFailed(forKeyboards: installableKeyboards, error: error)
|
|
return
|
|
}
|
|
|
|
let keyboardLocalPath = self.keyboardPath(forID: keyboard.id, keyboardVersion: keyboard.version)!
|
|
let isUpdate = latestKeyboardFileVersion(withID: keyboard.id) != nil
|
|
|
|
downloadQueue = HTTPDownloader.init(self)
|
|
let commonUserData: [String: Any] = [
|
|
Key.keyboardInfo: installableKeyboards,
|
|
Key.update: isUpdate
|
|
]
|
|
downloadQueue!.userInfo = commonUserData
|
|
|
|
var request = HTTPDownloadRequest(url: keyboardURL, userInfo: commonUserData)
|
|
request.destinationFile = keyboardLocalPath.path
|
|
request.tag = 0
|
|
|
|
downloadQueue!.addRequest(request)
|
|
for (i, url) in fontURLs.enumerated() {
|
|
let fontPath = self.fontPath(forFilename: url.lastPathComponent)!
|
|
request = HTTPDownloadRequest(url: url, userInfo: commonUserData)
|
|
request.destinationFile = fontPath.path
|
|
request.tag = i + 1
|
|
downloadQueue!.addRequest(request)
|
|
}
|
|
downloadQueue!.run()
|
|
}
|
|
|
|
/// - Returns: The current state for a keyboard
|
|
public func stateForKeyboard(withID keyboardID: String) -> KeyboardState {
|
|
if keyboardIdForCurrentRequest() == keyboardID {
|
|
return .downloading
|
|
}
|
|
guard let latestDownloadedVersion = latestKeyboardFileVersion(withID: keyboardID) else {
|
|
return .needsDownload
|
|
}
|
|
|
|
// Check version
|
|
if let latestRepositoryVersion = keyboardsInfo?[keyboardID]?.version,
|
|
compareVersions(latestDownloadedVersion, latestRepositoryVersion) == .orderedAscending {
|
|
return .needsUpdate
|
|
}
|
|
return .upToDate
|
|
}
|
|
|
|
/// - Precondition: `languages` is set.
|
|
private func createKeyboardsInfo() {
|
|
let keyboardsWithID = languages.flatMap { language in
|
|
language.keyboards!.map { kb in (kb.id, kb) }
|
|
}
|
|
keyboardsInfo = Dictionary(keyboardsWithID, uniquingKeysWith: { (old, _) in old })
|
|
let keyboardsWithLanguage = languages.flatMap { language -> [(String, InstallableKeyboard)] in
|
|
language.keyboards!.map { kb in
|
|
return ("\(language.id)_\(kb.id)", InstallableKeyboard(keyboard: kb, language: language))
|
|
}
|
|
}
|
|
keyboardsDictionary = Dictionary(uniqueKeysWithValues: keyboardsWithLanguage)
|
|
updateUserKeyboardsList()
|
|
}
|
|
|
|
private func updateUserKeyboardsList() {
|
|
if keyboardsDictionary.isEmpty {
|
|
return
|
|
}
|
|
let userData = activeUserDefaults()
|
|
|
|
let lastVersion = userData.string(forKey: Key.engineVersion) ?? "1.0"
|
|
if compareVersions(lastVersion, Util.sdkVersion) == .orderedSame {
|
|
return
|
|
}
|
|
userData.set(Util.sdkVersion, forKey: Key.engineVersion)
|
|
|
|
guard var userKbList = userData.userKeyboards else {
|
|
kmLog("No user keyboards to update", checkDebugPrinting: true)
|
|
return
|
|
}
|
|
|
|
for i in userKbList.indices {
|
|
let kbID = userKbList[i].id
|
|
let langID = userKbList[i].languageID
|
|
if var kb = repositoryKeyboard(withID: kbID, languageID: langID) {
|
|
kb.version = latestKeyboardFileVersion(withID: kbID)!
|
|
kb.isCustom = false
|
|
userKbList[i] = kb
|
|
} else {
|
|
var kb = userKbList[i]
|
|
kb.isCustom = true
|
|
userKbList[i] = kb
|
|
}
|
|
}
|
|
userData.userKeyboards = userKbList
|
|
userData.synchronize()
|
|
}
|
|
|
|
func keyboardIdForCurrentRequest() -> String? {
|
|
if let currentRequest = currentRequest {
|
|
let tmpStr = currentRequest.url.lastPathComponent
|
|
if tmpStr.hasJavaScriptExtension {
|
|
return String(tmpStr.dropLast(3))
|
|
}
|
|
} else if let downloadQueue = downloadQueue {
|
|
let kbInfo = downloadQueue.userInfo[Key.keyboardInfo]
|
|
if let keyboards = kbInfo as? [InstallableKeyboard], let keyboard = keyboards.first {
|
|
return keyboard.id
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
@objc func reachabilityChanged(_ notification: Notification) {
|
|
if isDebugPrintingOn {
|
|
var reachStr = "Not Reachable"
|
|
let status: NetworkStatus = reachability.currentReachabilityStatus()
|
|
if status == ReachableViaWiFi {
|
|
reachStr = "Reachable Via WiFi"
|
|
}
|
|
if status == ReachableViaWWAN {
|
|
reachStr = "Reachable Via WWan"
|
|
}
|
|
kmLog("Reachability changed to '\(reachStr)'", checkDebugPrinting: true)
|
|
}
|
|
}
|
|
|
|
// MARK: - HTTPDownloadDelegate methods
|
|
|
|
func downloadQueueFinished(_ queue: HTTPDownloader) {
|
|
if isDebugPrintingOn {
|
|
if let fontDir = activeFontDirectory()?.path {
|
|
let contents = try? FileManager.default.contentsOfDirectory(atPath: fontDir)
|
|
kmLog("Font Directory contents: \(String(describing: contents))", checkDebugPrinting: true)
|
|
}
|
|
if let langDir = activeLanguageDirectory()?.path {
|
|
let contents = try? FileManager.default.contentsOfDirectory(atPath: langDir)
|
|
kmLog("Language Directory contents: \(String(describing: contents))", checkDebugPrinting: true)
|
|
}
|
|
}
|
|
}
|
|
|
|
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.
|
|
if request.tag == 0 && request.typeCode == .downloadFile {
|
|
NotificationCenter.default.post(name: Notifications.keyboardDownloadStarted,
|
|
object: self,
|
|
value: request.userInfo[Key.keyboardInfo] as! [InstallableKeyboard])
|
|
}
|
|
}
|
|
|
|
func downloadRequestFinished(_ request: HTTPDownloadRequest) {
|
|
switch request.typeCode {
|
|
case .downloadFile:
|
|
let keyboards = request.userInfo[Key.keyboardInfo] as! [InstallableKeyboard]
|
|
let keyboard = keyboards[0]
|
|
let isUpdate = request.userInfo[Key.update] as! Bool
|
|
|
|
if let statusCode = request.responseStatusCode, statusCode == 200 {
|
|
// The request has succeeded.
|
|
if downloadQueue!.requestsCount == 0 {
|
|
// Download queue finished.
|
|
downloadQueue = nil
|
|
registerCustomFonts()
|
|
kmLog("Downloaded keyboard: \(keyboard.id).", checkDebugPrinting: true)
|
|
|
|
NotificationCenter.default.post(name: Notifications.keyboardDownloadCompleted,
|
|
object: self,
|
|
value: keyboards)
|
|
if isUpdate {
|
|
shouldReloadKeyboard = true
|
|
reloadKeyboard(in: keymanWeb)
|
|
}
|
|
let userData = activeUserDefaults()
|
|
userData.set([Date()], forKey: Key.synchronizeSWKeyboard)
|
|
userData.synchronize()
|
|
}
|
|
} else { // Possible request error (400 Bad Request, 404 Not Found, etc.)
|
|
downloadQueue!.cancelAllOperations()
|
|
downloadQueue = nil
|
|
|
|
let errorMessage = "\(request.responseStatusMessage ?? ""): \(request.url)"
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: errorMessage])
|
|
kmLog("Keyboard download failed: \(error).", checkDebugPrinting: true)
|
|
|
|
if !isUpdate {
|
|
// Clean up keyboard file if anything fails
|
|
// TODO: Also clean up remaining fonts
|
|
if let kbPath = keyboardPath(forID: keyboard.id, keyboardVersion: keyboard.version) {
|
|
try? FileManager.default.removeItem(at: kbPath)
|
|
}
|
|
}
|
|
downloadFailed(forKeyboards: keyboards, error: error)
|
|
}
|
|
case .downloadCachedData:
|
|
if request == currentRequest {
|
|
let decoder = JSONDecoder()
|
|
decoder.dateDecodingStrategy = .secondsSince1970
|
|
let result: LanguagesAPICall
|
|
do {
|
|
result = try decoder.decode(LanguagesAPICall.self, from: request.rawResponseData!)
|
|
} catch {
|
|
kmLog("Failed: \(error).", checkDebugPrinting: true)
|
|
let error = NSError(domain: "Keyman", code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: error.localizedDescription])
|
|
NotificationCenter.default.post(name: Notifications.languagesDownloadFailed, object: self, value: error)
|
|
return
|
|
}
|
|
|
|
options = result.options
|
|
languages = result.languages.sorted { a, b -> Bool in
|
|
a.name.localizedCaseInsensitiveCompare(b.name) == .orderedAscending
|
|
}
|
|
|
|
createKeyboardsInfo()
|
|
kmLog("Request completed -- \(languages.count) languages.", checkDebugPrinting: true)
|
|
currentRequest = nil
|
|
|
|
if let completionBlock = request.userInfo["completionBlock"] as? FetchKeyboardsBlock {
|
|
completionBlock(nil)
|
|
}
|
|
|
|
NotificationCenter.default.post(name: Notifications.languagesUpdated, object: self, value: ())
|
|
}
|
|
}
|
|
}
|
|
|
|
func downloadRequestFailed(_ request: HTTPDownloadRequest) {
|
|
switch request.typeCode {
|
|
case .downloadFile:
|
|
downloadQueue = nil
|
|
let error = request.error!
|
|
kmLog("Keyboard download failed: \(error).", checkDebugPrinting: true)
|
|
|
|
let keyboards = request.userInfo[Key.keyboardInfo] as! [InstallableKeyboard]
|
|
let keyboard = keyboards[0]
|
|
let isUpdate = request.userInfo[Key.update] as! Bool
|
|
|
|
if !isUpdate {
|
|
// Clean up keyboard file if anything fails
|
|
// TODO: Also clean up remaining fonts
|
|
if let kbPath = keyboardPath(forID: keyboard.id, keyboardVersion: keyboard.version) {
|
|
try? FileManager.default.removeItem(at: kbPath)
|
|
}
|
|
}
|
|
downloadFailed(forKeyboards: keyboards, error: error as NSError)
|
|
case .downloadCachedData:
|
|
if request == currentRequest {
|
|
let error = request.error!
|
|
kmLog("Failed: \(error).", checkDebugPrinting: true)
|
|
|
|
currentRequest = nil
|
|
|
|
if let completionBlock = request.userInfo["completionBlock"] as? FetchKeyboardsBlock {
|
|
completionBlock([NSUnderlyingErrorKey: error])
|
|
}
|
|
NotificationCenter.default.post(name: Notifications.languagesDownloadFailed, object: self, value: error)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func downloadFailed(forKeyboards keyboards: [InstallableKeyboard], error: Error) {
|
|
let notification = KeyboardDownloadFailedNotification(keyboards: keyboards, error: error)
|
|
NotificationCenter.default.post(name: Notifications.keyboardDownloadFailed,
|
|
object: self,
|
|
value: notification)
|
|
}
|
|
|
|
// MARK: - Loading custom keyboards
|
|
|
|
private func preloadFile(srcUrl: URL, dstDir dirUrl: URL, shouldOverwrite: Bool) {
|
|
let dstUrl = dirUrl.appendingPathComponent(srcUrl.lastPathComponent)
|
|
do {
|
|
if !FileManager.default.fileExists(atPath: dstUrl.path) {
|
|
try FileManager.default.copyItem(at: srcUrl, to: dstUrl)
|
|
} else if shouldOverwrite {
|
|
try FileManager.default.removeItem(at: dstUrl)
|
|
try FileManager.default.copyItem(at: srcUrl, to: dstUrl)
|
|
} else {
|
|
kmLog("File already exists at \(dstUrl) and not overwriting", checkDebugPrinting: true)
|
|
return
|
|
}
|
|
addSkipBackupAttribute(to: dstUrl)
|
|
} catch {
|
|
kmLog("Error copying file: \(error)", checkDebugPrinting: false)
|
|
}
|
|
}
|
|
|
|
/// Preloads a .js file for a language so that the keyboard is available without downloading.
|
|
/// - Precondition:
|
|
/// - The .js filename must remain the same as when obtained from Keyman.
|
|
/// - The .js file must be bundled in your application.
|
|
public func preloadLanguageFile(atPath languagePath: String, shouldOverwrite: Bool) {
|
|
guard let languageDir = activeLanguageDirectory() else {
|
|
kmLog("Could not find/create the Keyman language directory", checkDebugPrinting: false)
|
|
return
|
|
}
|
|
preloadFile(srcUrl: URL.init(fileURLWithPath: languagePath),
|
|
dstDir: languageDir,
|
|
shouldOverwrite: shouldOverwrite)
|
|
}
|
|
|
|
/// Preloads a .ttf or .otf file to be available without downloading.
|
|
/// - Precondition:
|
|
/// - The font file must be bundled in your application.
|
|
/// - SeeAlso: `registerCustomFonts()`
|
|
public func preloadFontFile(atPath fontPath: String, shouldOverwrite: Bool) {
|
|
guard let fontDir = activeFontDirectory() else {
|
|
kmLog("Could not find/create the Keyman font directory", checkDebugPrinting: false)
|
|
return
|
|
}
|
|
preloadFile(srcUrl: URL.init(fileURLWithPath: fontPath),
|
|
dstDir: fontDir,
|
|
shouldOverwrite: shouldOverwrite)
|
|
}
|
|
|
|
/// Registers all new fonts found in the font path. Call this after you have preloaded all your font files
|
|
/// with `preloadFontFile(atPath:shouldOverwrite:)`
|
|
public func registerCustomFonts() {
|
|
let directoryContents: [String]
|
|
do {
|
|
directoryContents = try FileManager.default.contentsOfDirectory(atPath: activeFontDirectory().path)
|
|
} catch {
|
|
kmLog("Failed to list font dir contents: \(error)", checkDebugPrinting: false)
|
|
return
|
|
}
|
|
|
|
for fontFilename in directoryContents where fontFilename.hasFontExtension {
|
|
if let fontInfo = keymanFonts[fontFilename] {
|
|
if !fontInfo.isRegistered {
|
|
if let newFontInfo = registerFont(withFilename: fontFilename) {
|
|
keymanFonts[fontFilename] = newFontInfo
|
|
}
|
|
}
|
|
} else if let fontInfo = registerFont(withFilename: fontFilename) {
|
|
keymanFonts[fontFilename] = fontInfo
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Unregisters all registered fonts in the font path.
|
|
public func unregisterCustomFonts() {
|
|
let directoryContents: [String]
|
|
do {
|
|
directoryContents = try FileManager.default.contentsOfDirectory(atPath: activeFontDirectory().path)
|
|
} catch {
|
|
kmLog("Failed to list font dir contents: \(error)", checkDebugPrinting: false)
|
|
return
|
|
}
|
|
|
|
for fontFilename in directoryContents where fontFilename.hasFontExtension {
|
|
if var fontInfo = keymanFonts[fontFilename], fontInfo.isRegistered {
|
|
if unregisterFont(withFilename: fontFilename) {
|
|
fontInfo.isRegistered = false
|
|
keymanFonts[fontFilename] = fontInfo
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func registerFont(withFilename fontFilename: String) -> RegisteredFont? {
|
|
guard let fontURL = activeFontDirectory()?.appendingPathComponent(fontFilename),
|
|
FileManager.default.fileExists(atPath: fontURL.path) else {
|
|
return nil
|
|
}
|
|
|
|
guard let provider = CGDataProvider(url: fontURL as CFURL) else {
|
|
kmLog("Failed to open \(fontURL)", checkDebugPrinting: false)
|
|
return nil
|
|
}
|
|
guard let font = CGFont(provider),
|
|
let cfFontName = font.postScriptName else {
|
|
kmLog("Failed to read font at \(fontURL)", checkDebugPrinting: false)
|
|
return nil
|
|
}
|
|
|
|
var didRegister = false
|
|
let fontName = cfFontName as String
|
|
if !fontExists(fontName) {
|
|
var errorRef: Unmanaged<CFError>?
|
|
didRegister = CTFontManagerRegisterFontsForURL(fontURL as CFURL, .none, &errorRef)
|
|
let error = errorRef?.takeRetainedValue() // Releases errorRef
|
|
if !didRegister {
|
|
kmLog("Failed to register font: \(fontURL) reason: \(error!.localizedDescription)",
|
|
checkDebugPrinting: false)
|
|
} else {
|
|
kmLog("Registered font: \(fontURL)", checkDebugPrinting: true)
|
|
}
|
|
}
|
|
return RegisteredFont(name: fontName, isRegistered: didRegister)
|
|
}
|
|
|
|
private func unregisterFont(withFilename fontFilename: String) -> Bool {
|
|
guard let fontURL = activeFontDirectory()?.appendingPathComponent(fontFilename),
|
|
FileManager.default.fileExists(atPath: fontURL.path) else {
|
|
return false
|
|
}
|
|
var errorRef: Unmanaged<CFError>?
|
|
let didUnregister = CTFontManagerUnregisterFontsForURL(fontURL as CFURL, .none, &errorRef)
|
|
let error = errorRef?.takeRetainedValue() // Releases errorRef
|
|
if !didUnregister {
|
|
kmLog("Failed to unregister font: \(fontURL) reason: \(error!.localizedDescription)", checkDebugPrinting: false)
|
|
} else {
|
|
kmLog("Unregistered font: \(fontFilename)", checkDebugPrinting: true)
|
|
}
|
|
return didUnregister
|
|
}
|
|
|
|
private func fontExists(_ fontName: String) -> Bool {
|
|
return UIFont.familyNames.contains { familyName in
|
|
UIFont.fontNames(forFamilyName: familyName).contains(fontName)
|
|
}
|
|
}
|
|
|
|
// TODO: Use a logging library or have more than 2 log levels
|
|
// Facilitates KeymanEngine internal logging.
|
|
public func kmLog(_ logStr: String, checkDebugPrinting: Bool) {
|
|
if checkDebugPrinting && !isDebugPrintingOn {
|
|
return
|
|
}
|
|
NSLog("%@", logStr)
|
|
}
|
|
|
|
// MARK: - File system and UserData management
|
|
|
|
// Local file storage
|
|
private func copyWebFilesToLibrary() {
|
|
guard let libraryDirectory = activeKeymanDirectory() else {
|
|
kmLog("Could not locate library directory! Could not copy Keyman files.", checkDebugPrinting: false)
|
|
return
|
|
}
|
|
|
|
do {
|
|
try copyFromBundle(resourceName: kmwFileName,
|
|
resourceExtension: kmwFileExtension,
|
|
dstDir: libraryDirectory)
|
|
try copyFromBundle(resourceName: iOSCodeFileName,
|
|
resourceExtension: nil,
|
|
dstDir: libraryDirectory)
|
|
try copyFromBundle(resourceName: "\(Defaults.keyboard.id)-1.6",
|
|
resourceExtension: "js",
|
|
dstDir: activeLanguageDirectory())
|
|
try copyFromBundle(resourceName: "DejaVuSans",
|
|
resourceExtension: "ttf",
|
|
dstDir: activeFontDirectory())
|
|
try copyFromBundle(resourceName: "kmwosk",
|
|
resourceExtension: "css",
|
|
dstDir: libraryDirectory)
|
|
try copyFromBundle(resourceName: "keymanweb-osk",
|
|
resourceExtension: "ttf",
|
|
dstDir: libraryDirectory)
|
|
} catch {
|
|
kmLog("copyWebFilesToLibrary: \(error)", checkDebugPrinting: false)
|
|
}
|
|
}
|
|
|
|
private func copyFromBundle(resourceName: String, resourceExtension: String?, dstDir: URL?) throws {
|
|
let filenameForLog = "\(resourceName)\(resourceExtension.map { ".\($0)" } ?? "")"
|
|
guard let srcUrl = Resources.bundle.url(forResource: resourceName, withExtension: resourceExtension) else {
|
|
let message = "Could not locate \(filenameForLog) in the Keyman bundle for copying."
|
|
throw NSError(domain: "Keyman", code: 0, userInfo: [NSLocalizedDescriptionKey: message])
|
|
}
|
|
guard let dstDir = dstDir else {
|
|
let message = "Destination directory for \(filenameForLog) is nil"
|
|
throw NSError(domain: "Keyman", code: 0, userInfo: [NSLocalizedDescriptionKey: message])
|
|
}
|
|
let dstUrl = dstDir.appendingPathComponent(srcUrl.lastPathComponent)
|
|
|
|
// FIXME: FileManager exceptions are swallowed.
|
|
copyAndExcludeFromBackup(at: srcUrl, to: dstUrl)
|
|
}
|
|
|
|
private func compareFileModDates(_ a: String, _ b: String) -> ComparisonResult? {
|
|
guard let aAttrs = try? FileManager.default.attributesOfItem(atPath: a),
|
|
let bAttrs = try? FileManager.default.attributesOfItem(atPath: b),
|
|
let aModDate = aAttrs[.modificationDate] as? Date,
|
|
let bModDate = bAttrs[.modificationDate] as? Date else {
|
|
return nil
|
|
}
|
|
if aModDate > bModDate {
|
|
return .orderedDescending
|
|
}
|
|
if aModDate < bModDate {
|
|
return .orderedAscending
|
|
}
|
|
return .orderedSame
|
|
}
|
|
|
|
// TODO: Consider making these lazy vars
|
|
// FIXME: Check for errors when creating directory
|
|
private func createSubdirectory(baseDir: URL?, name: String) -> URL? {
|
|
guard let baseDir = baseDir else {
|
|
return nil
|
|
}
|
|
let newDir = baseDir.appendingPathComponent(name)
|
|
try? FileManager.default.createDirectory(at: newDir,
|
|
withIntermediateDirectories: true,
|
|
attributes: nil)
|
|
return newDir
|
|
}
|
|
|
|
private func defaultKeymanDirectory() -> URL? {
|
|
let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
|
|
if paths.isEmpty {
|
|
return nil
|
|
}
|
|
return createSubdirectory(baseDir: URL(fileURLWithPath: paths[0]), name: "keyman")
|
|
}
|
|
|
|
private func defaultLanguageDirectory() -> URL? {
|
|
return createSubdirectory(baseDir: defaultKeymanDirectory(), name: "languages")
|
|
}
|
|
|
|
private func defaultFontDirectory() -> URL? {
|
|
return createSubdirectory(baseDir: defaultKeymanDirectory(), name: "fonts")
|
|
}
|
|
|
|
var sharedContainerURL: URL? {
|
|
guard let groupID = Manager.applicationGroupIdentifier else {
|
|
kmLog("applicationGroupIdentifier is unset", checkDebugPrinting: false)
|
|
return nil
|
|
}
|
|
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID)
|
|
}
|
|
|
|
func sharedKeymanDirectory() -> URL? {
|
|
return createSubdirectory(baseDir: sharedContainerURL, name: "keyman")
|
|
}
|
|
|
|
func sharedLanguageDirectory() -> URL? {
|
|
return createSubdirectory(baseDir: sharedKeymanDirectory(), name: "languages")
|
|
}
|
|
|
|
func sharedFontDirectory() -> URL? {
|
|
return createSubdirectory(baseDir: sharedKeymanDirectory(), name: "fonts")
|
|
}
|
|
|
|
func activeKeymanDirectory() -> URL! {
|
|
return canAccessSharedContainer() ? sharedKeymanDirectory() : defaultKeymanDirectory()
|
|
}
|
|
|
|
func activeLanguageDirectory() -> URL! {
|
|
return canAccessSharedContainer() ? sharedLanguageDirectory() : defaultLanguageDirectory()
|
|
}
|
|
|
|
func activeFontDirectory() -> URL! {
|
|
return canAccessSharedContainer() ? sharedFontDirectory() : defaultFontDirectory()
|
|
}
|
|
|
|
func activeUserDefaults() -> UserDefaults {
|
|
return canAccessSharedContainer() ? sharedUserDefaults! : UserDefaults.standard
|
|
}
|
|
|
|
var sharedUserDefaults: UserDefaults? {
|
|
guard let groupID = Manager.applicationGroupIdentifier else {
|
|
kmLog("applicationGroupIdentifier is unset", checkDebugPrinting: false)
|
|
return nil
|
|
}
|
|
return UserDefaults(suiteName: groupID)
|
|
}
|
|
|
|
func canAccessSharedContainer() -> Bool {
|
|
guard let sharedKeymanDir = sharedKeymanDirectory() else {
|
|
return false
|
|
}
|
|
if !Util.isSystemKeyboard {
|
|
return true
|
|
}
|
|
let keymanFile = sharedKeymanDir.appendingPathComponent(kmwFullFileName)
|
|
return FileManager.default.fileExists(atPath: keymanFile.path)
|
|
}
|
|
|
|
private func copyUserDefaultsToSharedContainer() {
|
|
guard let sharedUserData = sharedUserDefaults else {
|
|
return
|
|
}
|
|
let defaultUserData = UserDefaults.standard
|
|
let keysToCopy = [Key.userKeyboardsList, Key.userCurrentKeyboard,
|
|
Key.engineVersion, Key.keyboardPickerDisplayed]
|
|
for key in keysToCopy {
|
|
if sharedUserData.object(forKey: key) == nil {
|
|
sharedUserData.set(defaultUserData.object(forKey: key), forKey: key)
|
|
}
|
|
}
|
|
sharedUserData.synchronize()
|
|
}
|
|
|
|
private func copyUserDefaultsFromSharedContainer() {
|
|
guard let sharedUserData = sharedUserDefaults else {
|
|
return
|
|
}
|
|
let defaultUserData = UserDefaults.standard
|
|
let keysToCopy = [Key.userKeyboardsList, Key.engineVersion]
|
|
for key in keysToCopy {
|
|
if sharedUserData.object(forKey: key) != nil {
|
|
defaultUserData.set(sharedUserData.object(forKey: key), forKey: key)
|
|
}
|
|
}
|
|
defaultUserData.synchronize()
|
|
}
|
|
|
|
private func addSkipBackupAttribute(to url: URL) -> Bool {
|
|
var url = url
|
|
assert(FileManager.default.fileExists(atPath: url.path))
|
|
var resourceValues = URLResourceValues()
|
|
resourceValues.isExcludedFromBackup = true
|
|
do {
|
|
// Writes values to the backing store. It is not only mutating the URL in memory.
|
|
try url.setResourceValues(resourceValues)
|
|
return true
|
|
} catch {
|
|
kmLog("Error excluding \(url) from backup \(error)", checkDebugPrinting: false)
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func copyAndExcludeFromBackup(at src: URL, to dst: URL) -> Bool {
|
|
let fm = FileManager.default
|
|
|
|
var isDirectory: ObjCBool = false
|
|
let fileExists = fm.fileExists(atPath: src.path, isDirectory: &isDirectory)
|
|
|
|
if !fileExists || isDirectory.boolValue {
|
|
return false
|
|
}
|
|
|
|
// copy if destination does not exist or replace if source is newer
|
|
do {
|
|
if !fm.fileExists(atPath: dst.path) {
|
|
try fm.copyItem(at: src, to: dst)
|
|
} else if compareFileModDates(src.path, dst.path) == .orderedDescending {
|
|
try fm.removeItem(at: dst)
|
|
try fm.copyItem(at: src, to: dst)
|
|
} else {
|
|
return false
|
|
}
|
|
} catch {
|
|
kmLog("copyAndExcludeFromBackup: \(error)", checkDebugPrinting: false)
|
|
return false
|
|
}
|
|
|
|
addSkipBackupAttribute(to: dst)
|
|
return true
|
|
}
|
|
|
|
private func copyDirectoryContents(at srcDir: URL?, to dstDir: URL?) throws {
|
|
guard let srcDir = srcDir,
|
|
let dstDir = dstDir else {
|
|
return
|
|
}
|
|
let srcContents = try FileManager.default.contentsOfDirectory(at: srcDir, includingPropertiesForKeys: [])
|
|
for srcFile in srcContents {
|
|
copyAndExcludeFromBackup(at: srcFile, to: dstDir.appendingPathComponent(srcFile.lastPathComponent))
|
|
}
|
|
}
|
|
|
|
private func copyKeymanFilesToSharedContainer() -> Bool {
|
|
do {
|
|
try copyDirectoryContents(at: defaultKeymanDirectory(), to: sharedKeymanDirectory())
|
|
try copyDirectoryContents(at: defaultLanguageDirectory(), to: sharedLanguageDirectory())
|
|
try copyDirectoryContents(at: defaultFontDirectory(), to: sharedFontDirectory())
|
|
return true
|
|
} catch {
|
|
kmLog("copyKeymanFilesToSharedContainer(): \(error)", checkDebugPrinting: false)
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func copyKeymanFilesFromSharedContainer() -> Bool {
|
|
do {
|
|
try copyDirectoryContents(at: sharedKeymanDirectory(), to: defaultKeymanDirectory())
|
|
try copyDirectoryContents(at: sharedLanguageDirectory(), to: defaultLanguageDirectory())
|
|
try copyDirectoryContents(at: sharedFontDirectory(), to: defaultFontDirectory())
|
|
} catch {
|
|
kmLog("copyKeymanFilesFromSharedContainer(): \(error)", checkDebugPrinting: false)
|
|
return false
|
|
}
|
|
registerCustomFonts()
|
|
return true
|
|
}
|
|
|
|
func copyKeymanFilesToTemp() -> Bool {
|
|
let tempKeymanDir = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("keyman")
|
|
let tempLangDir = tempKeymanDir.appendingPathComponent("languages")
|
|
let tempFontDir = tempKeymanDir.appendingPathComponent("fonts")
|
|
|
|
do {
|
|
try FileManager.default.createDirectory(at: tempKeymanDir, withIntermediateDirectories: true, attributes: nil)
|
|
try FileManager.default.createDirectory(at: tempLangDir, withIntermediateDirectories: true, attributes: nil)
|
|
try FileManager.default.createDirectory(at: tempFontDir, withIntermediateDirectories: true, attributes: nil)
|
|
try copyDirectoryContents(at: activeKeymanDirectory(), to: tempKeymanDir)
|
|
try copyDirectoryContents(at: activeLanguageDirectory(), to: tempLangDir)
|
|
try copyDirectoryContents(at: activeFontDirectory(), to: tempFontDir)
|
|
} catch {
|
|
kmLog("copyKeymanFilesToTemp(): \(error)", checkDebugPrinting: false)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
private var usingTempFolder: Bool {
|
|
if #available(iOS 9.0, *) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// FIXME: The check for empty filename, etc was removed. Check whether that needs to be added back.
|
|
private func keyboardPath(forID keyboardID: String, keyboardVersion: String?) -> URL? {
|
|
var keyboardVersion = keyboardVersion
|
|
if keyboardVersion == nil {
|
|
keyboardVersion = latestKeyboardFileVersion(withID: keyboardID)
|
|
}
|
|
guard let version = keyboardVersion else {
|
|
return nil
|
|
}
|
|
return activeLanguageDirectory()?.appendingPathComponent("\(keyboardID)-\(version).js")
|
|
}
|
|
|
|
func fontPath(forFilename filename: String) -> URL? {
|
|
return activeFontDirectory()?.appendingPathComponent(filename)
|
|
}
|
|
|
|
func keyboardFileExists(withID keyboardID: String, version: String) -> Bool {
|
|
guard let langDir = activeLanguageDirectory() else {
|
|
return false
|
|
}
|
|
let path = langDir.appendingPathComponent("\(keyboardID)-\(version).js").path
|
|
return FileManager.default.fileExists(atPath: path)
|
|
}
|
|
|
|
func latestKeyboardFileVersion(withID keyboardID: String) -> String? {
|
|
guard let langDirPath = activeLanguageDirectory()?.path else {
|
|
return nil
|
|
}
|
|
guard let dirContents = try? FileManager.default.contentsOfDirectory(atPath: langDirPath) else {
|
|
return nil
|
|
}
|
|
|
|
var latestVersion: String?
|
|
for filename in dirContents where filename.hasPrefix("\(keyboardID)-") && filename.hasJavaScriptExtension {
|
|
let dashRange = filename.range(of: "-", options: .backwards)!
|
|
let extensionRange = filename.range(of: ".js", options: .backwards)!
|
|
let version = String(filename[dashRange.upperBound..<extensionRange.lowerBound])
|
|
|
|
if let previousMax = latestVersion {
|
|
if compareVersions(previousMax, version) == .orderedAscending {
|
|
latestVersion = version
|
|
}
|
|
} else if compareVersions(version, version) != nil { // Ensure that the version number is valid
|
|
latestVersion = version
|
|
}
|
|
}
|
|
return latestVersion
|
|
}
|
|
|
|
/// Compares version numbers in dotted numberic format.
|
|
/// - Returns: ComparisonResult if both version numbers are valid.
|
|
func compareVersions(_ v1: String, _ v2: String) -> ComparisonResult? {
|
|
if v1.isEmpty || v2.isEmpty {
|
|
return nil
|
|
}
|
|
let components1 = v1.components(separatedBy: ".")
|
|
let components2 = v2.components(separatedBy: ".")
|
|
|
|
let len = max(components1.count, components2.count)
|
|
for i in 0..<len {
|
|
// Shorter version number padded with trailing zero components
|
|
let component1 = components1[safe: i] ?? "0"
|
|
let component2 = components2[safe: i] ?? "0"
|
|
guard let val1 = Int(component1), val1 >= 0 else {
|
|
return nil
|
|
}
|
|
guard let val2 = Int(component2), val2 >= 0 else {
|
|
return nil
|
|
}
|
|
if val1 < val2 {
|
|
return .orderedAscending
|
|
}
|
|
if val1 > val2 {
|
|
return .orderedDescending
|
|
}
|
|
}
|
|
return .orderedSame
|
|
}
|
|
|
|
func updateKeyboardVersion(forID kbID: String, newKeyboardVersion kbVersion: String) {
|
|
let userData = activeUserDefaults()
|
|
guard var userKeyboards = userData.userKeyboards else {
|
|
return
|
|
}
|
|
|
|
// Set version in user keyboards list
|
|
for i in userKeyboards.indices {
|
|
var kb = userKeyboards[i]
|
|
if kbID == kb.id {
|
|
kb.version = kbVersion
|
|
userKeyboards[i] = kb
|
|
}
|
|
}
|
|
userData.userKeyboards = userKeyboards
|
|
userData.synchronize()
|
|
|
|
// Set version for current keyboard
|
|
// TODO: Move this UserDefaults into a function
|
|
let currentUserData = Util.isSystemKeyboard ? UserDefaults.standard : activeUserDefaults()
|
|
if var userKb = currentUserData.currentKeyboard {
|
|
if kbID == userKb.id {
|
|
userKb.version = kbVersion
|
|
currentUserData.currentKeyboard = userKb
|
|
currentUserData.synchronize()
|
|
}
|
|
}
|
|
}
|
|
|
|
func synchronizeSWKeyboard() {
|
|
copyUserDefaultsFromSharedContainer()
|
|
copyKeymanFilesFromSharedContainer()
|
|
}
|
|
|
|
// MARK: - View management
|
|
|
|
public var keyboardHeight: CGFloat {
|
|
if Util.isSystemKeyboard {
|
|
return keyboardHeight(isPortrait: InputViewController.isPortrait)
|
|
} else {
|
|
return keyboardHeight(isPortrait: UIDevice.current.orientation.isPortrait)
|
|
}
|
|
}
|
|
|
|
func keyboardHeight(with orientation: UIInterfaceOrientation) -> CGFloat {
|
|
return keyboardHeight(isPortrait: orientation.isPortrait)
|
|
}
|
|
|
|
func keyboardHeight(isPortrait: Bool) -> CGFloat {
|
|
if UIDevice.current.userInterfaceIdiom == .pad {
|
|
if isPortrait {
|
|
return Util.isSystemKeyboard ? padPortraitSystemKeyboardHeight : padPortraitInAppKeyboardHeight
|
|
} else {
|
|
return Util.isSystemKeyboard ? padLandscapeSystemKeyboardHeight : padLandscapeInAppKeyboardHeight
|
|
}
|
|
} else {
|
|
if isPortrait {
|
|
return Util.isSystemKeyboard ? phonePortraitSystemKeyboardHeight : phonePortraitInAppKeyboardHeight
|
|
} else {
|
|
return Util.isSystemKeyboard ? phoneLandscapeSystemKeyboardHeight : phoneLandscapeInAppKeyboardHeight
|
|
}
|
|
}
|
|
}
|
|
|
|
var keyboardWidth: CGFloat {
|
|
return UIScreen.main.bounds.width
|
|
}
|
|
|
|
var keyboardSize: CGSize {
|
|
return CGSize(width: keyboardWidth, height: keyboardHeight)
|
|
}
|
|
|
|
// Keyman interaction
|
|
private func resizeKeyboard() {
|
|
let newSize = keyboardSize
|
|
if didResizeToOrientation && Util.isSystemKeyboard && lastKeyboardSize == newSize {
|
|
didResizeToOrientation = false
|
|
return
|
|
}
|
|
lastKeyboardSize = newSize
|
|
|
|
keymanWeb.frame = CGRect(origin: .zero, size: newSize)
|
|
|
|
// Workaround for WKWebView bug with landscape orientation
|
|
// TODO: Check if still necessary and if there's a better solution
|
|
if Util.isSystemKeyboard {
|
|
perform(#selector(self.resizeDelay), with: self, afterDelay: 1.0)
|
|
}
|
|
|
|
var oskHeight = Int(newSize.height)
|
|
oskHeight -= oskHeight % (Util.isSystemKeyboard ? 10 : 20)
|
|
|
|
keymanWeb.setOskWidth(Int(newSize.width))
|
|
keymanWeb.setOskHeight(oskHeight)
|
|
}
|
|
|
|
private var keymanScrollView: UIScrollView {
|
|
return keymanWeb.webView.scrollView
|
|
}
|
|
|
|
@objc func clearSubKeyArrays() {
|
|
if subKeysView == nil {
|
|
subKeys.removeAll()
|
|
subKeyIDs.removeAll()
|
|
subKeyTexts.removeAll()
|
|
}
|
|
}
|
|
|
|
@objc func dismissHelpBubble() {
|
|
if let view = helpBubbleView {
|
|
view.removeFromSuperview()
|
|
helpBubbleView = nil
|
|
}
|
|
}
|
|
|
|
@objc func dismissKeyPreview() {
|
|
if let view = keyPreviewView {
|
|
view.removeFromSuperview()
|
|
keyPreviewView = nil
|
|
}
|
|
}
|
|
|
|
var isSubKeysMenuVisible: Bool {
|
|
return subKeysView != nil
|
|
}
|
|
|
|
private func dismissSubKeys() {
|
|
if let subKeysView = subKeysView {
|
|
subKeysView.removeFromSuperview()
|
|
subKeysView.subviews.forEach { $0.removeFromSuperview() }
|
|
self.subKeysView = nil
|
|
keymanWeb.setPopupVisible(false)
|
|
}
|
|
subKeys.removeAll()
|
|
subKeyIDs.removeAll()
|
|
subKeyTexts.removeAll()
|
|
}
|
|
|
|
/// Displays a list of available keyboards and allows a user to add/download new keyboards
|
|
/// or remove existing ones.
|
|
///
|
|
/// - Parameters:
|
|
/// - in: The current UIViewController (recommended) or the navigation controller
|
|
/// - shouldAddKeyboard: Whether to immediately open the view to add a new keyboard
|
|
/// - SeeAlso:
|
|
/// TextView/TextField to enable/disable the keyboard picker
|
|
public func showKeyboardPicker(in viewController: UIViewController, shouldAddKeyboard: Bool) {
|
|
let vc = KeyboardPickerViewController()
|
|
let nc = UINavigationController(rootViewController: vc)
|
|
nc.modalTransitionStyle = .coverVertical
|
|
nc.modalPresentationStyle = .pageSheet
|
|
viewController.present(nc, animated: true) {() -> Void in
|
|
if shouldAddKeyboard {
|
|
vc.showAddKeyboard()
|
|
} else {
|
|
let userData = self.activeUserDefaults()
|
|
userData.set(true, forKey: Key.keyboardPickerDisplayed)
|
|
userData.synchronize()
|
|
self.isKeymanHelpOn = false
|
|
}
|
|
}
|
|
}
|
|
|
|
public func dismissKeyboardPicker(_ viewController: UIViewController) {
|
|
viewController.dismiss(animated: true)
|
|
if shouldReloadKeyboard {
|
|
reloadKeyboard(in: keymanWeb)
|
|
}
|
|
NotificationCenter.default.post(name: Notifications.keyboardPickerDismissed, object: self, value: ())
|
|
}
|
|
|
|
private func reloadKeyboard(in keymanWeb: KeymanWebViewController) {
|
|
if #available(iOS 9.0, *) {
|
|
guard let codeURL = activeKeymanDirectory()?.appendingPathComponent(kmwFullFileName) else {
|
|
return
|
|
}
|
|
keymanWeb.webView.loadFileURL(codeURL, allowingReadAccessTo: codeURL.deletingLastPathComponent())
|
|
} else {
|
|
// WKWebView in iOS < 9 is missing loadFileURL().
|
|
// The files need to be copied to a temporary directory and loaded from there.
|
|
if copyKeymanFilesToTemp() {
|
|
let codeURL = URL(fileURLWithPath: NSTemporaryDirectory())
|
|
.appendingPathComponent("keyman")
|
|
.appendingPathComponent(kmwFullFileName)
|
|
keymanWeb.webView.load(URLRequest(url: codeURL, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 60.0))
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func resetKeyboard() {
|
|
let keyboard = currentKeyboardInfo
|
|
keyboardID = nil
|
|
languageID = nil
|
|
|
|
if let keyboard = keyboard {
|
|
setKeyboard(keyboard)
|
|
} else if let keyboard = activeUserDefaults().userKeyboards?[safe: 0] {
|
|
setKeyboard(keyboard)
|
|
} else {
|
|
setKeyboard(Defaults.keyboard)
|
|
}
|
|
}
|
|
|
|
@objc func showHelpBubble() {
|
|
// Help bubble is always disabled for system-wide keyboard
|
|
if Util.isSystemKeyboard || keyboardMenuView != nil {
|
|
return
|
|
}
|
|
|
|
keymanWeb.languageMenuPosition { keyFrame in
|
|
self.showHelpBubble(at: keyFrame.origin)
|
|
}
|
|
}
|
|
|
|
// TODO: The bulk of this should be moved to PopoverView
|
|
func showHelpBubble(at point: CGPoint) {
|
|
self.helpBubbleView?.removeFromSuperview()
|
|
let helpBubbleView = PopoverView(frame: CGRect.zero)
|
|
self.helpBubbleView = helpBubbleView
|
|
helpBubbleView.backgroundColor = UIColor(red: 253.0 / 255.0, green: 244.0 / 255.0,
|
|
blue: 196.0 / 255.0, alpha: 1.0)
|
|
helpBubbleView.backgroundColor2 = UIColor(red: 233.0 / 255.0, green: 224.0 / 255.0,
|
|
blue: 176.0 / 255.0, alpha: 1.0)
|
|
helpBubbleView.borderColor = UIColor(red: 0.5, green: 0.25, blue: 0.25, alpha: 1.0)
|
|
|
|
let isPad = UIDevice.current.userInterfaceIdiom == .pad
|
|
let sizeMultiplier = CGFloat(isPad ? 1.5 : 1.0)
|
|
let frameWidth = 90.0 * sizeMultiplier
|
|
let frameHeight = (40.0 + helpBubbleView.arrowHeight) * sizeMultiplier
|
|
let fontSize = 10.0 * sizeMultiplier
|
|
|
|
let inputViewFrame = keymanWeb.view.frame
|
|
let screenWidth = inputViewFrame.size.width
|
|
|
|
// TODO: Refactor this out
|
|
let isPortrait: Bool
|
|
if Util.isSystemKeyboard {
|
|
isPortrait = InputViewController.isPortrait
|
|
} else {
|
|
isPortrait = UIDevice.current.orientation.isPortrait
|
|
}
|
|
|
|
let adjY: CGFloat
|
|
if isPortrait {
|
|
adjY = Util.isSystemKeyboard ? 9.0 : 4.0
|
|
} else {
|
|
adjY = Util.isSystemKeyboard ? 3.0 : 4.0
|
|
}
|
|
let px = point.x
|
|
let py = point.y + adjY + (isPad ? 2.0 : 1.0)
|
|
var x = px - frameWidth / 2
|
|
let y = py - frameHeight
|
|
if x < 0 {
|
|
x = 0
|
|
} else if x + frameWidth > screenWidth {
|
|
x = screenWidth - frameWidth
|
|
}
|
|
|
|
helpBubbleView.frame = CGRect(x: x, y: y, width: frameWidth, height: frameHeight)
|
|
if x == 0 {
|
|
helpBubbleView.arrowPosX = px
|
|
} else if x == screenWidth - frameWidth {
|
|
helpBubbleView.arrowPosX = (px - x)
|
|
} else {
|
|
helpBubbleView.arrowPosX = frameWidth / 2
|
|
}
|
|
|
|
let helpText = UILabel(frame: CGRect(x: 5, y: 0,
|
|
width: frameWidth - 10, height: frameHeight - helpBubbleView.arrowHeight))
|
|
helpText.backgroundColor = UIColor.clear
|
|
helpText.font = helpText.font.withSize(fontSize)
|
|
helpText.textAlignment = .center
|
|
helpText.textColor = UIColor.darkText
|
|
helpText.lineBreakMode = .byWordWrapping
|
|
helpText.numberOfLines = 0
|
|
helpText.text = keyboardChangeHelpText
|
|
helpBubbleView.addSubview(helpText)
|
|
keymanWeb.view.addSubview(helpBubbleView)
|
|
}
|
|
|
|
@objc func resizeDelay() {
|
|
// + 1000 to work around iOS bug with resizing on landscape orientation. Technically we only
|
|
// need this for landscape but it doesn't hurt to do it with both. 1000 is a big number that
|
|
// should hopefully work on all devices.
|
|
let kbWidth = keyboardWidth
|
|
let kbHeight = keyboardHeight
|
|
keymanWeb.frame = CGRect(x: 0.0, y: 0.0, width: kbWidth, height: kbHeight + 1000)
|
|
}
|
|
|
|
func resizeKeyboardIfNeeded() {
|
|
// TODO: Check if necessary since resizeKeyboard() checks old size
|
|
let newSize = keyboardSize
|
|
if newSize != lastKeyboardSize {
|
|
resizeKeyboard()
|
|
lastKeyboardSize = newSize
|
|
}
|
|
}
|
|
|
|
func resizeKeyboard(with orientation: UIInterfaceOrientation) {
|
|
// TODO: Update to use new size instead of orientation since viewWillRotate() is deprecated
|
|
// TODO: Refactor to use resizeKeyboard()
|
|
let kbWidth = keyboardWidth
|
|
let kbHeight = keyboardHeight(with: orientation)
|
|
keymanWeb.frame = CGRect(x: 0.0, y: 0.0, width: kbWidth, height: kbHeight)
|
|
|
|
var oskHeight = Int(kbHeight)
|
|
oskHeight -= oskHeight % (Util.isSystemKeyboard ? 10 : 20)
|
|
|
|
keymanWeb.setOskWidth(Int(kbWidth))
|
|
keymanWeb.setOskHeight(oskHeight)
|
|
}
|
|
|
|
// MARK: - Text
|
|
|
|
// TODO: Switch from NSRange
|
|
func setSelectionRange(_ range: NSRange, manually: Bool) {
|
|
if range.location != NSNotFound {
|
|
keymanWeb.setCursorRange(range)
|
|
}
|
|
}
|
|
|
|
func clearText() {
|
|
setText(nil)
|
|
setSelectionRange(NSRange(location: 0, length: 0), manually: true)
|
|
kmLog("Cleared text.", checkDebugPrinting: true)
|
|
}
|
|
|
|
func setText(_ text: String?) {
|
|
keymanWeb.setText(text)
|
|
}
|
|
|
|
// MARK: - Keyboard Notifications
|
|
@objc func keyboardWillShow(_ notification: Notification) {
|
|
dismissSubKeys()
|
|
dismissKeyPreview()
|
|
resizeKeyboard()
|
|
|
|
if isKeymanHelpOn {
|
|
helpBubbleView?.removeFromSuperview()
|
|
let showHelpBubble = #selector(self.showHelpBubble as () -> Void)
|
|
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: showHelpBubble, object: nil)
|
|
perform(showHelpBubble, with: nil, afterDelay: 1.5)
|
|
}
|
|
}
|
|
|
|
@objc func keyboardWillHide(_ notification: Notification) {
|
|
dismissHelpBubble()
|
|
dismissSubKeys()
|
|
dismissKeyPreview()
|
|
}
|
|
|
|
// MARK: - KeymanWebViewDelegate methods
|
|
func keyboardLoaded(_ keymanWeb: KeymanWebViewController) {
|
|
keymanWebDelegate?.keyboardLoaded(keymanWeb)
|
|
|
|
kmLog("Loaded keyboard.", checkDebugPrinting: true)
|
|
resizeKeyboard()
|
|
keymanWeb.setDeviceType(UIDevice.current.userInterfaceIdiom)
|
|
|
|
var newKb = Defaults.keyboard
|
|
if (keyboardID == nil || languageID == nil) && !shouldReloadKeyboard {
|
|
let userData = Util.isSystemKeyboard ? UserDefaults.standard : activeUserDefaults()
|
|
if let currentKb = userData.currentKeyboard {
|
|
let kbID = currentKb.id
|
|
let langID = currentKb.languageID
|
|
if activeUserDefaults().userKeyboard(withID: kbID, languageID: langID) != nil {
|
|
newKb = currentKb
|
|
}
|
|
} else if let userKbs = activeUserDefaults().userKeyboards, !userKbs.isEmpty {
|
|
newKb = userKbs[0]
|
|
}
|
|
setKeyboard(newKb)
|
|
}
|
|
|
|
NotificationCenter.default.post(name: Notifications.keyboardLoaded, object: self, value: newKb)
|
|
if shouldReloadKeyboard {
|
|
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.resetKeyboard), object: nil)
|
|
perform(#selector(self.resetKeyboard), with: nil, afterDelay: 0.25)
|
|
shouldReloadKeyboard = false
|
|
}
|
|
}
|
|
|
|
func insertText(_ keymanWeb: KeymanWebViewController, numCharsToDelete: Int, newText: String) {
|
|
keymanWebDelegate?.insertText(keymanWeb, numCharsToDelete: numCharsToDelete, newText: newText)
|
|
|
|
dismissHelpBubble()
|
|
isKeymanHelpOn = false
|
|
}
|
|
|
|
func showKeyPreview(_ keymanWeb: KeymanWebViewController, keyFrame: CGRect, preview: String) {
|
|
keymanWebDelegate?.showKeyPreview(keymanWeb, keyFrame: keyFrame, preview: preview)
|
|
|
|
if UIDevice.current.userInterfaceIdiom == .pad
|
|
|| (Util.isSystemKeyboard && !isSystemKeyboardTopBarEnabled)
|
|
|| subKeysView != nil {
|
|
return
|
|
}
|
|
|
|
dismissKeyPreview()
|
|
clearSubKeyArrays()
|
|
|
|
keyPreviewView = KeyPreviewView(frame: keyFrame)
|
|
|
|
keyPreviewView!.setLabelText(preview)
|
|
var oskFontName = oskFontNameForKeyboard(withID: keyboardID!, languageID: languageID!)
|
|
oskFontName = oskFontName ?? fontNameForKeyboard(withID: keyboardID!, languageID: languageID!)
|
|
keyPreviewView!.setLabelFont(oskFontName)
|
|
keymanWeb.view.addSubview(keyPreviewView!)
|
|
}
|
|
|
|
func dismissKeyPreview(_ keymanWeb: KeymanWebViewController) {
|
|
keymanWebDelegate?.dismissKeyPreview(keymanWeb)
|
|
|
|
if UIDevice.current.userInterfaceIdiom == .pad || keyPreviewView == nil {
|
|
return
|
|
}
|
|
|
|
let dismissKeyPreview = #selector(self.dismissKeyPreview as () -> Void)
|
|
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: dismissKeyPreview, object: nil)
|
|
perform(dismissKeyPreview, with: nil, afterDelay: 0.1)
|
|
clearSubKeyArrays()
|
|
}
|
|
|
|
func showSubkeys(_ keymanWeb: KeymanWebViewController,
|
|
keyFrame: CGRect,
|
|
subkeyIDs: [String],
|
|
subkeyTexts: [String],
|
|
useSpecialFont: Bool) {
|
|
keymanWebDelegate?.showSubkeys(keymanWeb,
|
|
keyFrame: keyFrame,
|
|
subkeyIDs: subkeyIDs,
|
|
subkeyTexts: subkeyTexts,
|
|
useSpecialFont: useSpecialFont)
|
|
|
|
dismissHelpBubble()
|
|
isKeymanHelpOn = false
|
|
dismissSubKeys()
|
|
dismissKeyboardMenu()
|
|
|
|
self.keyFrame = keyFrame
|
|
subKeyIDs = subkeyIDs
|
|
subKeyTexts = subkeyTexts
|
|
}
|
|
|
|
func menuKeyDown(_ keymanWeb: KeymanWebViewController) {
|
|
keymanWebDelegate?.menuKeyDown(keymanWeb)
|
|
}
|
|
|
|
func menuKeyUp(_ keymanWeb: KeymanWebViewController) {
|
|
keymanWebDelegate?.menuKeyUp(keymanWeb)
|
|
|
|
dismissHelpBubble()
|
|
isKeymanHelpOn = false
|
|
if Util.isSystemKeyboard {
|
|
let userData = UserDefaults.standard
|
|
userData.set(true, forKey: Key.keyboardPickerDisplayed)
|
|
userData.synchronize()
|
|
}
|
|
}
|
|
|
|
func hideKeyboard(_ keymanWeb: KeymanWebViewController) {
|
|
keymanWebDelegate?.hideKeyboard(keymanWeb)
|
|
|
|
dismissHelpBubble()
|
|
dismissSubKeys()
|
|
dismissKeyboardMenu()
|
|
}
|
|
|
|
// MARK: - UIGestureRecognizer
|
|
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
|
|
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
|
|
return true
|
|
}
|
|
|
|
// UILongPressGestureRecognizer implementation to show sub keys in a subview
|
|
@objc func holdAction(_ sender: UILongPressGestureRecognizer) {
|
|
switch sender.state {
|
|
case .ended:
|
|
// Touch Ended
|
|
if let subKeysView = subKeysView {
|
|
subKeysView.removeFromSuperview()
|
|
subKeysView.subviews.forEach { $0.removeFromSuperview() }
|
|
self.subKeysView = nil
|
|
keymanWeb.setPopupVisible(false)
|
|
}
|
|
var buttonClicked = false
|
|
for button in subKeys where button.isHighlighted {
|
|
button.isHighlighted = false
|
|
button.backgroundColor = subKeyColor
|
|
button.isEnabled = false
|
|
button.sendActions(for: .touchUpInside)
|
|
buttonClicked = true
|
|
break
|
|
}
|
|
if !buttonClicked {
|
|
clearSubKeyArrays()
|
|
}
|
|
case .began:
|
|
// Touch & Hold Began
|
|
let touchPoint = sender.location(in: sender.view)
|
|
// Check if touch was for language menu button
|
|
keymanWeb.languageMenuPosition { keyFrame in
|
|
self.menuKeyFrame = keyFrame
|
|
if keyFrame.contains(touchPoint) {
|
|
self.keymanWebDelegate?.menuKeyHeld(self.keymanWeb)
|
|
return
|
|
}
|
|
self.touchHoldBegan()
|
|
}
|
|
default:
|
|
// Hold & Move
|
|
guard let subKeysView = subKeysView else {
|
|
kmLog("Unexpected hold and move while subKeysView = nil", checkDebugPrinting: false)
|
|
return
|
|
}
|
|
let touchPoint = sender.location(in: subKeysView.containerView)
|
|
for button in subKeys {
|
|
if button.frame.contains(touchPoint) {
|
|
button.isEnabled = true
|
|
button.isHighlighted = true
|
|
button.backgroundColor = subKeyColorHighlighted
|
|
} else {
|
|
button.isHighlighted = false
|
|
button.isEnabled = false
|
|
button.backgroundColor = subKeyColor
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func touchHoldBegan() {
|
|
let isPad = UIDevice.current.userInterfaceIdiom == .pad
|
|
let fontSize = isPad ? UIFont.buttonFontSize * 2 : UIFont.buttonFontSize
|
|
|
|
var oskFontName = oskFontNameForKeyboard(withID: keyboardID!, languageID: languageID!)
|
|
if oskFontName == nil {
|
|
oskFontName = fontNameForKeyboard(withID: keyboardID!, languageID: languageID!)
|
|
}
|
|
|
|
if subKeyIDs.isEmpty {
|
|
subKeys = []
|
|
return
|
|
}
|
|
|
|
subKeys = subKeyTexts.enumerated().map { i, subKeyText in
|
|
let button = UIButton(type: .custom)
|
|
button.tag = i
|
|
button.backgroundColor = subKeyColor
|
|
button.setRoundedBorder(withRadius: 4.0, borderWidth: 1.0, color: .gray)
|
|
button.setTitleColor(.black, for: .disabled)
|
|
button.setTitleColor(.black, for: .highlighted)
|
|
|
|
if let oskFontName = oskFontName {
|
|
button.titleLabel?.font = UIFont(name: oskFontName, size: fontSize)
|
|
} else {
|
|
button.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
|
|
}
|
|
|
|
if specialOSKFont != nil {
|
|
button.titleLabel?.font = UIFont(name: "KeymanwebOsk", size: fontSize)
|
|
button.setTitleColor(.gray, for: .disabled)
|
|
}
|
|
|
|
button.addTarget(self, action: #selector(subKeyButtonClick), for: .touchUpInside)
|
|
button.setTitle(subKeyText, for: .normal)
|
|
button.tintColor = UIColor(red: 181.0 / 255.0, green: 181.0 / 255.0, blue: 181.0 / 255.0, alpha: 1.0)
|
|
button.isEnabled = false
|
|
return button
|
|
}
|
|
|
|
dismissKeyPreview()
|
|
subKeysView = SubKeysView(keyFrame: keyFrame, subKeys: subKeys)
|
|
keymanWeb.view.addSubview(subKeysView!)
|
|
keymanWeb.setPopupVisible(true)
|
|
}
|
|
|
|
@objc func subKeyButtonClick(_ sender: UIButton) {
|
|
let keyIndex = sender.tag
|
|
if keyIndex < subKeyIDs.count && keyIndex < subKeyTexts.count {
|
|
let subKeyID = subKeyIDs[keyIndex]
|
|
let subKeyText = subKeyTexts[keyIndex]
|
|
keymanWeb.executePopupKey(id: subKeyID, text: subKeyText)
|
|
}
|
|
subKeys.removeAll()
|
|
subKeyIDs.removeAll()
|
|
subKeyTexts.removeAll()
|
|
}
|
|
|
|
// MARK: - InputViewController methods
|
|
// TODO: Manager should not have InputViewController methods. Move this into InputViewController.
|
|
func updateViewConstraints() {
|
|
dismissSubKeys()
|
|
dismissKeyPreview()
|
|
dismissKeyboardMenu()
|
|
resizeKeyboardIfNeeded()
|
|
}
|
|
|
|
func inputViewDidLoad() {
|
|
dismissSubKeys()
|
|
dismissKeyPreview()
|
|
dismissKeyboardMenu()
|
|
resizeKeyboard()
|
|
|
|
let activeUserDef = activeUserDefaults()
|
|
let standardUserDef = UserDefaults.standard
|
|
let activeDate = (activeUserDef.object(forKey: Key.synchronizeSWKeyboard) as? [Date])?[safe: 0]
|
|
let standardDate = (standardUserDef.object(forKey: Key.synchronizeSWKeyboard) as? [Date])?[safe: 0]
|
|
|
|
let shouldSynchronize: Bool
|
|
if let standardDate = standardDate,
|
|
let activeDate = activeDate {
|
|
shouldSynchronize = standardDate != activeDate
|
|
} else if activeDate == nil {
|
|
shouldSynchronize = false
|
|
} else {
|
|
shouldSynchronize = true
|
|
}
|
|
|
|
if (!didSynchronize || shouldSynchronize) && canAccessSharedContainer() {
|
|
synchronizeSWKeyboard()
|
|
if keyboardID != nil && languageID != nil {
|
|
shouldReloadKeyboard = true
|
|
reloadKeyboard(in: keymanWeb)
|
|
}
|
|
didSynchronize = true
|
|
standardUserDef.set(activeUserDef.object(forKey: Key.synchronizeSWKeyboard),
|
|
forKey: Key.synchronizeSWKeyboard)
|
|
standardUserDef.synchronize()
|
|
}
|
|
}
|
|
|
|
// FIXME: This is deprecated. Use inputViewWillTransition()
|
|
func inputViewWillRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
|
|
dismissSubKeys()
|
|
dismissKeyPreview()
|
|
dismissKeyboardMenu()
|
|
resizeKeyboard(with: toInterfaceOrientation)
|
|
if isKeymanHelpOn {
|
|
helpBubbleView?.removeFromSuperview()
|
|
let showHelpBubble = #selector(self.showHelpBubble as () -> Void)
|
|
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: showHelpBubble, object: nil)
|
|
perform(showHelpBubble, with: nil, afterDelay: 1.5)
|
|
}
|
|
didResizeToOrientation = true
|
|
}
|
|
|
|
func showKeyboardMenu(_ ic: InputViewController, closeButtonTitle: String?) {
|
|
let parentView = ic.view ?? keymanWeb.view
|
|
keymanWeb.languageMenuPosition { keyFrame in
|
|
self.menuKeyFrame = keyFrame
|
|
if keyFrame != .zero {
|
|
self.keyboardMenuView?.removeFromSuperview()
|
|
self.keyboardMenuView = KeyboardMenuView(keyFrame: self.menuKeyFrame, inputViewController: ic,
|
|
closeButtonTitle: closeButtonTitle)
|
|
parentView?.addSubview(self.keyboardMenuView!)
|
|
}
|
|
}
|
|
}
|
|
|
|
func dismissKeyboardMenu() {
|
|
if let keyboardMenuView = keyboardMenuView {
|
|
keyboardMenuView.removeFromSuperview()
|
|
self.keyboardMenuView = nil
|
|
}
|
|
}
|
|
|
|
var isKeyboardMenuVisible: Bool {
|
|
return keyboardMenuView != nil
|
|
}
|
|
|
|
var isSystemKeyboardTopBarEnabled: Bool {
|
|
return true
|
|
}
|
|
}
|