Create shortcuts from manager

Delegate Intents callbacks to a single place.
This commit is contained in:
Davide De Rosa 2019-03-27 22:18:41 +01:00
parent ae88fdfad6
commit c9d170768a
3 changed files with 42 additions and 67 deletions

View File

@ -24,19 +24,12 @@
//
import UIKit
import IntentsUI
import Intents
import Passepartout_Core
@available(iOS 12, *)
protocol ShortcutsAddViewControllerDelegate: class {
func shortcutAddController(_ controller: UIViewController?, voiceShortcut: INVoiceShortcut)
func shortcutAddControllerDidCancel(_ controller: UIViewController?)
}
@available(iOS 12, *)
class ShortcutsAddViewController: UITableViewController, INUIAddVoiceShortcutViewControllerDelegate, TableModelHost {
weak var delegate: ShortcutsAddViewControllerDelegate?
class ShortcutsAddViewController: UITableViewController, TableModelHost {
weak var delegate: ShortcutsIntentDelegate?
// MARK: TableModel
@ -206,29 +199,6 @@ class ShortcutsAddViewController: UITableViewController, INUIAddVoiceShortcutVie
}
private func addShortcut(with intent: INIntent) {
guard let shortcut = INShortcut(intent: intent) else {
return
}
let vc = INUIAddVoiceShortcutViewController(shortcut: shortcut)
vc.delegate = self
present(vc, animated: true, completion: nil)
}
@IBAction private func close() {
dismiss(animated: true, completion: nil)
}
// MARK: INUIAddVoiceShortcutViewControllerDelegate
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
guard let voiceShortcut = voiceShortcut else {
delegate?.shortcutAddControllerDidCancel(self)
return
}
delegate?.shortcutAddController(self, voiceShortcut: voiceShortcut)
}
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
delegate?.shortcutAddControllerDidCancel(self)
delegate?.shortcutsDidSelectIntent(intent: intent)
}
}

View File

@ -29,7 +29,7 @@ import IntentsUI
import Passepartout_Core
@available(iOS 12, *)
class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewControllerDelegate, INUIAddVoiceShortcutViewControllerDelegate, TableModelHost {
class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewControllerDelegate, TableModelHost {
private let service = TransientStore.shared.service
private var providers: [String] = []
@ -38,7 +38,7 @@ class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewC
private var selectedProfile: ConnectionProfile?
weak var delegate: ShortcutsAddViewControllerDelegate?
weak var delegate: ShortcutsIntentDelegate?
// MARK: TableModelHost
@ -168,12 +168,7 @@ class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewC
}
private func addShortcut(with intent: INIntent) {
guard let shortcut = INShortcut(intent: intent) else {
return
}
let vc = INUIAddVoiceShortcutViewController(shortcut: shortcut)
vc.delegate = self
present(vc, animated: true, completion: nil)
delegate?.shortcutsDidSelectIntent(intent: intent)
}
private func pickProviderLocation() {
@ -185,18 +180,4 @@ class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewC
func providerPoolController(_: ProviderPoolViewController, didSelectPool pool: Pool) {
addMoveToLocation(pool: pool)
}
// MARK: INUIAddVoiceShortcutViewControllerDelegate
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
guard let voiceShortcut = voiceShortcut else {
delegate?.shortcutAddControllerDidCancel(nil)
return
}
delegate?.shortcutAddController(nil, voiceShortcut: voiceShortcut)
}
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
delegate?.shortcutAddControllerDidCancel(nil)
}
}

View File

@ -28,6 +28,11 @@ import Intents
import IntentsUI
import Passepartout_Core
@available(iOS 12, *)
protocol ShortcutsIntentDelegate: class {
func shortcutsDidSelectIntent(intent: INIntent)
}
@available(iOS 12, *)
private struct ShortcutWrapper: Comparable {
let phrase: String
@ -58,7 +63,7 @@ private struct ShortcutWrapper: Comparable {
}
@available(iOS 12, *)
class ShortcutsViewController: UITableViewController, INUIEditVoiceShortcutViewControllerDelegate, ShortcutsAddViewControllerDelegate, TableModelHost {
class ShortcutsViewController: UITableViewController, INUIAddVoiceShortcutViewControllerDelegate, INUIEditVoiceShortcutViewControllerDelegate, ShortcutsIntentDelegate, TableModelHost {
private var wrappers: [ShortcutWrapper]?
private var editedIndexPath: IndexPath?
@ -193,29 +198,43 @@ class ShortcutsViewController: UITableViewController, INUIEditVoiceShortcutViewC
}
}
// MARK: ShortcutsAddViewControllerDelegate
// MARK: ShortcutsIntentDelegate
func shortcutAddController(_ controller: UIViewController?, voiceShortcut: INVoiceShortcut) {
func shortcutsDidSelectIntent(intent: INIntent) {
guard let shortcut = INShortcut(intent: intent) else {
return
}
navigationController?.popToRootViewController(animated: true)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
let vc = INUIAddVoiceShortcutViewController(shortcut: shortcut)
vc.delegate = self
self.present(vc, animated: true, completion: nil)
}
}
// MARK: INUIAddVoiceShortcutViewControllerDelegate
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
guard let voiceShortcut = voiceShortcut else {
dismiss(animated: true, completion: nil)
return
}
wrappers?.append(ShortcutWrapper.from(voiceShortcut))
wrappers?.sort()
reloadModel()
tableView.reloadData()
navigationController?.popToRootViewController(animated: false)
dismiss(animated: true, completion: nil)
}
func shortcutAddControllerDidCancel(_ controller: UIViewController?) {
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
dismiss(animated: true, completion: nil)
}
// MARK: INUIEditVoiceShortcutViewControllerDelegate
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
editedIndexPath = nil
dismiss(animated: true, completion: nil)
}
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didUpdate voiceShortcut: INVoiceShortcut?, error: Error?) {
guard let indexPath = editedIndexPath, let voiceShortcut = voiceShortcut else {
return
@ -240,4 +259,9 @@ class ShortcutsViewController: UITableViewController, INUIEditVoiceShortcutViewC
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
editedIndexPath = nil
dismiss(animated: true, completion: nil)
}
}