passepartout-apple/Passepartout-iOS/Scenes/Shortcuts/ShortcutsViewController.swift

280 lines
8.9 KiB
Swift
Raw Normal View History

//
2019-03-27 21:09:08 +00:00
// ShortcutsViewController.swift
// 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
import PassepartoutCore
import Convenience
@available(iOS 12, *)
protocol ShortcutsIntentDelegate: class {
func shortcutsDidSelectIntent(intent: INIntent)
}
@available(iOS 12, *)
2019-03-27 19:55:50 +00:00
private struct ShortcutWrapper: Comparable {
let phrase: String
let intentDescription: String?
2019-03-27 22:03:19 +00:00
let original: INVoiceShortcut
static func from(_ vs: INVoiceShortcut) -> ShortcutWrapper {
return ShortcutWrapper(
phrase: vs.invocationPhrase,
2019-03-27 21:09:08 +00:00
intentDescription: vs.shortcut.intent?.intentDescription,
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
}
}
@available(iOS 12, *)
class ShortcutsViewController: UITableViewController, INUIAddVoiceShortcutViewControllerDelegate, INUIEditVoiceShortcutViewControllerDelegate, ShortcutsIntentDelegate, StrongTableHost {
private var wrappers: [ShortcutWrapper]?
2019-03-27 21:36:23 +00:00
private var pendingShortcut: INShortcut?
private var editedIndexPath: IndexPath?
// MARK: StrongTableModel
let model: StrongTableModel<SectionType, RowType> = {
let model: StrongTableModel<SectionType, RowType> = StrongTableModel()
model.add(.all)
model.setHeader(L10n.Core.Shortcuts.Edit.Sections.All.header, forSection: .all)
model.set([], forSection: .all)
return model
}()
func reloadModel() {
2019-03-27 19:28:37 +00:00
var rows = [RowType](repeating: .shortcut, count: wrappers?.count ?? 0)
rows.append(.addShortcut)
model.set(rows, forSection: .all)
}
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.Core.Organizer.Cells.SiriShortcuts.caption
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)
}
private func handleShortcutsFetchError(_ error: Error?) {
// TODO: really show it?
// let alert = UIAlertController.asAlert(
// title,
// L10n.Core.Shortcuts.Edit.message(error?.localizedDescription ?? "")
// )
// alert.addCancelAction(L10n.Core.Global.ok) {
// 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()
reloadModel()
tableView.reloadData()
}
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)
}
@IBAction private func close() {
dismiss(animated: true, completion: nil)
}
// MARK: UITableViewController
enum SectionType {
case all
}
enum RowType {
case shortcut
2019-03-27 19:28:37 +00:00
case addShortcut
}
override func numberOfSections(in tableView: UITableView) -> Int {
return model.numberOfSections
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return model.header(forSection: section)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.numberOfRows(forSection: section)
}
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)
cell.leftText = wrapper.phrase
cell.rightText = wrapper.intentDescription
2019-03-27 19:28:37 +00:00
case .addShortcut:
cell.applyAction(Theme.current)
cell.leftText = L10n.App.Shortcuts.Edit.Cells.AddShortcut.caption
2019-03-27 19:28:37 +00:00
cell.accessoryType = .none
cell.isTappable = true
}
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 {
break
}
2019-03-27 22:03:19 +00:00
let vc = INUIEditVoiceShortcutViewController(voiceShortcut: wrapper.original)
vc.modalPresentationStyle = Theme.current.modalPresentationStyle
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:52:58 +00:00
// MARK: ShortcutsIntentDelegate
2019-03-27 19:52:58 +00:00
func shortcutsDidSelectIntent(intent: INIntent) {
2019-03-27 21:36:23 +00:00
pendingShortcut = INShortcut(intent: intent)
dismiss(animated: true) {
self.finishAddingPendingShortcut()
}
}
// 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 19:52:58 +00:00
dismiss(animated: true, completion: nil)
}
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
2019-03-27 19:52:58 +00:00
dismiss(animated: true, completion: nil)
}
// MARK: INUIEditVoiceShortcutViewControllerDelegate
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:59:16 +00:00
dismiss(animated: true)
}
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)
}
}
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
editedIndexPath = nil
dismiss(animated: true, completion: nil)
}
}