spiegel-keyman/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/KeyboardInfoViewController.swift
2017-12-07 14:37:24 +07:00

157 lines
4.5 KiB
Swift

//
// KeyboardInfoViewController.swift
// KeymanEngine
//
// Created by Gabriel Wong on 2017-09-12.
// Copyright © 2017 SIL International. All rights reserved.
//
import UIKit
class KeyboardInfoViewController: UITableViewController, UIAlertViewDelegate {
var keyboardCount: Int = 0
var keyboardIndex: Int = 0
var keyboardID: String = ""
var languageID: String = ""
var keyboardVersion: String = ""
var keyboardCopyright: String = ""
var isCustomKeyboard: Bool = false
private var infoArray = [[String: String]]()
override func viewDidLoad() {
super.viewDidLoad()
infoArray = [[String: String]]()
infoArray.append([
"title": "Keyboard version",
"subtitle": keyboardVersion
])
if !isCustomKeyboard {
infoArray.append([
"title": "Help link",
"subtitle": ""
])
}
infoArray.append([
"title": "Delete",
"subtitle": ""
])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.setToolbarHidden(true, animated: true)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isCustomKeyboard {
return 2
} else {
return 3
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) {
return cell
}
return UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if !isCustomKeyboard {
if indexPath.row == 1 {
let url = URL(string: "http://help.keyman.com/keyboard/\(keyboardID)/\(keyboardVersion)/")!
if let openURL = Manager.shared.openURL {
openURL(url)
} else {
log.error("openURL not set in Manager. Failed to open \(url)")
}
} else if indexPath.row == 2 {
showDeleteKeyboard()
}
} else if indexPath.row == 1 {
showDeleteKeyboard()
}
}
private func fetchedKeyboardData(_ data: Data) {
guard let json = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [AnyHashable: Any] else {
return
}
let keyboards = json[Key.language] as? [Any]
let kbDict = keyboards?[0] as? [AnyHashable: Any]
var info = infoArray[1]
let copyright = kbDict?[Key.keyboardCopyright] as? String ?? "Unknown"
info["subtitle"] = copyright
infoArray[1] = info
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.selectionStyle = .none
cell.accessoryType = .none
cell.textLabel?.text = infoArray[indexPath.row]["title"]
cell.detailTextLabel?.text = infoArray[indexPath.row]["subtitle"]
cell.tag = indexPath.row
if !isCustomKeyboard {
if indexPath.row == 1 {
cell.accessoryType = .disclosureIndicator
} else if indexPath.row == 2 && !canDeleteKeyboard {
cell.isUserInteractionEnabled = false
cell.textLabel?.isEnabled = false
cell.detailTextLabel?.isEnabled = false
}
} else if indexPath.row == 1 && !canDeleteKeyboard {
cell.isUserInteractionEnabled = false
cell.textLabel?.isEnabled = false
cell.detailTextLabel?.isEnabled = false
}
}
private var canDeleteKeyboard: Bool {
if !Manager.shared.canRemoveKeyboards {
return false
}
if !Manager.shared.canRemoveDefaultKeyboard {
return keyboardIndex != 0
}
if keyboardIndex > 0 {
return true
}
return keyboardCount > 1
}
private func showDeleteKeyboard() {
let alert = UIAlertView(title: title ?? "",
message: "Would you like to delete this keyboard?",
delegate: self,
cancelButtonTitle: "Cancel",
otherButtonTitles: "Delete")
alert.tag = 1
alert.show()
}
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
if buttonIndex == alertView.cancelButtonIndex {
return
}
if alertView.tag == 1 {
if Manager.shared.removeKeyboard(at: keyboardIndex) {
navigationController?.popToRootViewController(animated: true)
}
}
}
}