2019-03-27 19:23:53 +00:00
|
|
|
//
|
2019-03-27 21:09:08 +00:00
|
|
|
// ShortcutsViewController.swift
|
2019-03-27 19:23:53 +00:00
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/27/19.
|
|
|
|
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Intents
|
|
|
|
import IntentsUI
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2019-03-27 19:23:53 +00:00
|
|
|
|
2019-03-27 21:18:41 +00:00
|
|
|
@available(iOS 12, *)
|
|
|
|
protocol ShortcutsIntentDelegate: class {
|
|
|
|
func shortcutsDidSelectIntent(intent: INIntent)
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
@available(iOS 12, *)
|
2019-03-27 19:55:50 +00:00
|
|
|
private struct ShortcutWrapper: Comparable {
|
2019-03-27 19:23:53 +00:00
|
|
|
let phrase: String
|
|
|
|
|
|
|
|
let intentDescription: String?
|
|
|
|
|
2019-03-27 22:03:19 +00:00
|
|
|
let original: INVoiceShortcut
|
2019-03-27 19:23:53 +00:00
|
|
|
|
|
|
|
static func from(_ vs: INVoiceShortcut) -> ShortcutWrapper {
|
|
|
|
return ShortcutWrapper(
|
|
|
|
phrase: vs.invocationPhrase,
|
2019-03-27 21:09:08 +00:00
|
|
|
intentDescription: vs.shortcut.intent?.intentDescription,
|
2019-03-27 19:23:53 +00:00
|
|
|
original: vs
|
|
|
|
)
|
|
|
|
}
|
2019-03-27 19:55:50 +00:00
|
|
|
|
|
|
|
// MARK: Equatable
|
|
|
|
|
|
|
|
static func ==(lhs: ShortcutWrapper, rhs: ShortcutWrapper) -> Bool {
|
|
|
|
return lhs.phrase == rhs.phrase
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Comparable
|
|
|
|
|
|
|
|
static func <(lhs: ShortcutWrapper, rhs: ShortcutWrapper) -> Bool {
|
|
|
|
return lhs.phrase < rhs.phrase
|
|
|
|
}
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
@available(iOS 12, *)
|
2019-10-11 08:56:23 +00:00
|
|
|
class ShortcutsViewController: UITableViewController, INUIAddVoiceShortcutViewControllerDelegate, INUIEditVoiceShortcutViewControllerDelegate, ShortcutsIntentDelegate, StrongTableHost {
|
2019-03-27 19:23:53 +00:00
|
|
|
private var wrappers: [ShortcutWrapper]?
|
|
|
|
|
2019-03-27 21:36:23 +00:00
|
|
|
private var pendingShortcut: INShortcut?
|
|
|
|
|
2019-03-27 19:23:53 +00:00
|
|
|
private var editedIndexPath: IndexPath?
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
// MARK: StrongTableModel
|
2019-03-27 19:23:53 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
let model: StrongTableModel<SectionType, RowType> = {
|
|
|
|
let model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
2019-03-27 19:23:53 +00:00
|
|
|
model.add(.all)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.setHeader(L10n.Core.Shortcuts.Edit.Sections.All.header, forSection: .all)
|
|
|
|
model.set([], forSection: .all)
|
2019-03-27 19:23:53 +00:00
|
|
|
return model
|
|
|
|
}()
|
|
|
|
|
|
|
|
func reloadModel() {
|
2019-03-27 19:28:37 +00:00
|
|
|
var rows = [RowType](repeating: .shortcut, count: wrappers?.count ?? 0)
|
|
|
|
rows.append(.addShortcut)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set(rows, forSection: .all)
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.Core.Organizer.Cells.SiriShortcuts.caption
|
2019-03-27 19:23:53 +00:00
|
|
|
|
|
|
|
INVoiceShortcutCenter.shared.getAllVoiceShortcuts { [weak self] (shortcuts, error) in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard let shortcuts = shortcuts else {
|
|
|
|
self?.handleShortcutsFetchError(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self?.handleShortcuts(shortcuts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 19:28:37 +00:00
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
2019-03-27 19:52:58 +00:00
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
2019-03-28 07:24:39 +00:00
|
|
|
if let nav = segue.destination as? UINavigationController, let vc = nav.topViewController as? ShortcutsAddViewController {
|
2019-03-27 19:52:58 +00:00
|
|
|
vc.delegate = self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:28:37 +00:00
|
|
|
private func addShortcut() {
|
|
|
|
perform(segue: StoryboardSegue.Shortcuts.shortcutAddSegueIdentifier)
|
|
|
|
}
|
2019-03-27 19:23:53 +00:00
|
|
|
|
|
|
|
private func handleShortcutsFetchError(_ error: Error?) {
|
|
|
|
|
|
|
|
// TODO: really show it?
|
2019-10-11 08:56:23 +00:00
|
|
|
// let alert = UIAlertController.asAlert(
|
2019-03-27 19:23:53 +00:00
|
|
|
// title,
|
2019-06-21 21:15:43 +00:00
|
|
|
// L10n.Core.Shortcuts.Edit.message(error?.localizedDescription ?? "")
|
2019-03-27 19:23:53 +00:00
|
|
|
// )
|
2019-06-21 21:15:43 +00:00
|
|
|
// alert.addCancelAction(L10n.Core.Global.ok) {
|
2019-03-27 19:23:53 +00:00
|
|
|
// self.close()
|
|
|
|
// }
|
|
|
|
// present(alert, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func handleShortcuts(_ shortcuts: [INVoiceShortcut]) {
|
|
|
|
wrappers = shortcuts.map { ShortcutWrapper.from($0) }
|
2019-03-27 19:55:50 +00:00
|
|
|
wrappers?.sort()
|
2019-03-27 19:23:53 +00:00
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
2019-04-09 08:34:19 +00:00
|
|
|
|
|
|
|
private func finishAddingPendingShortcut() {
|
|
|
|
guard let shortcut = pendingShortcut else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if let ip = tableView.indexPathForSelectedRow {
|
|
|
|
tableView.deselectRow(at: ip, animated: true)
|
|
|
|
}
|
|
|
|
pendingShortcut = nil
|
|
|
|
let vc = INUIAddVoiceShortcutViewController(shortcut: shortcut)
|
|
|
|
vc.modalPresentationStyle = Theme.current.modalPresentationStyle
|
|
|
|
vc.delegate = self
|
|
|
|
present(vc, animated: true, completion: nil)
|
|
|
|
}
|
2019-03-27 19:23:53 +00:00
|
|
|
|
|
|
|
@IBAction private func close() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
// MARK: UITableViewController
|
|
|
|
|
2019-03-27 19:23:53 +00:00
|
|
|
enum SectionType {
|
|
|
|
case all
|
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType {
|
|
|
|
case shortcut
|
2019-03-27 19:28:37 +00:00
|
|
|
|
|
|
|
case addShortcut
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfSections
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.header(forSection: section)
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfRows(forSection: section)
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .shortcut:
|
|
|
|
guard let wrapper = wrappers?[indexPath.row] else {
|
|
|
|
break
|
|
|
|
}
|
2019-03-27 19:28:37 +00:00
|
|
|
cell.apply(Theme.current)
|
2019-03-27 19:23:53 +00:00
|
|
|
cell.leftText = wrapper.phrase
|
|
|
|
cell.rightText = wrapper.intentDescription
|
2019-03-27 19:28:37 +00:00
|
|
|
|
|
|
|
case .addShortcut:
|
|
|
|
cell.applyAction(Theme.current)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.App.Shortcuts.Edit.Cells.AddShortcut.caption
|
2019-03-27 19:28:37 +00:00
|
|
|
cell.accessoryType = .none
|
|
|
|
cell.isTappable = true
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .shortcut:
|
2019-03-27 22:03:19 +00:00
|
|
|
guard let wrapper = wrappers?[indexPath.row] else {
|
2019-03-27 19:23:53 +00:00
|
|
|
break
|
|
|
|
}
|
2019-03-27 22:03:19 +00:00
|
|
|
let vc = INUIEditVoiceShortcutViewController(voiceShortcut: wrapper.original)
|
2019-04-09 08:34:19 +00:00
|
|
|
vc.modalPresentationStyle = Theme.current.modalPresentationStyle
|
2019-03-27 19:23:53 +00:00
|
|
|
vc.delegate = self
|
|
|
|
editedIndexPath = indexPath
|
|
|
|
present(vc, animated: true, completion: nil)
|
2019-03-27 19:28:37 +00:00
|
|
|
|
|
|
|
case .addShortcut:
|
|
|
|
addShortcut()
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 19:52:58 +00:00
|
|
|
|
2019-03-27 21:18:41 +00:00
|
|
|
// MARK: ShortcutsIntentDelegate
|
2019-03-27 19:52:58 +00:00
|
|
|
|
2019-03-27 21:18:41 +00:00
|
|
|
func shortcutsDidSelectIntent(intent: INIntent) {
|
2019-03-27 21:36:23 +00:00
|
|
|
pendingShortcut = INShortcut(intent: intent)
|
2019-04-09 08:34:19 +00:00
|
|
|
dismiss(animated: true) {
|
|
|
|
self.finishAddingPendingShortcut()
|
|
|
|
}
|
2019-03-27 21:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: INUIAddVoiceShortcutViewControllerDelegate
|
|
|
|
|
|
|
|
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
|
|
|
|
guard let voiceShortcut = voiceShortcut else {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:52:58 +00:00
|
|
|
wrappers?.append(ShortcutWrapper.from(voiceShortcut))
|
2019-03-27 19:55:50 +00:00
|
|
|
wrappers?.sort()
|
2019-03-27 19:52:58 +00:00
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
2019-03-27 21:18:41 +00:00
|
|
|
|
2019-03-27 19:52:58 +00:00
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
2019-03-27 21:18:41 +00:00
|
|
|
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
|
2019-03-27 19:52:58 +00:00
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2019-03-27 19:23:53 +00:00
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
// MARK: INUIEditVoiceShortcutViewControllerDelegate
|
|
|
|
|
2019-03-27 19:23:53 +00:00
|
|
|
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didUpdate voiceShortcut: INVoiceShortcut?, error: Error?) {
|
|
|
|
guard let indexPath = editedIndexPath, let voiceShortcut = voiceShortcut else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
editedIndexPath = nil
|
|
|
|
wrappers?[indexPath.row] = ShortcutWrapper.from(voiceShortcut)
|
2019-03-27 19:55:50 +00:00
|
|
|
wrappers?.sort()
|
2019-03-27 19:59:16 +00:00
|
|
|
tableView.reloadData()
|
2019-03-27 19:23:53 +00:00
|
|
|
|
2019-03-27 19:59:16 +00:00
|
|
|
dismiss(animated: true)
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) {
|
|
|
|
guard let indexPath = editedIndexPath else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
editedIndexPath = nil
|
|
|
|
wrappers?.remove(at: indexPath.row)
|
|
|
|
reloadModel()
|
|
|
|
|
|
|
|
dismiss(animated: true) {
|
|
|
|
self.tableView.deleteRows(at: [indexPath], with: .automatic)
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 21:18:41 +00:00
|
|
|
|
|
|
|
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
|
|
|
|
editedIndexPath = nil
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2019-03-27 19:23:53 +00:00
|
|
|
}
|