Pack Siri controllers to simplify @available

This commit is contained in:
Davide De Rosa 2019-03-27 20:36:20 +01:00
parent e1638d1c73
commit 0d619ffd62
3 changed files with 23 additions and 46 deletions

View File

@ -27,7 +27,8 @@ import UIKit
import IntentsUI import IntentsUI
import Passepartout_Core import Passepartout_Core
class ShortcutsAddViewController: UITableViewController, TableModelHost { @available(iOS 12, *)
class ShortcutsAddViewController: UITableViewController, INUIAddVoiceShortcutViewControllerDelegate, TableModelHost {
// MARK: TableModel // MARK: TableModel
@ -55,9 +56,9 @@ class ShortcutsAddViewController: UITableViewController, TableModelHost {
title = L10n.Shortcuts.Add.title title = L10n.Shortcuts.Add.title
} }
}
extension ShortcutsAddViewController { // MARK: UITableViewController
enum SectionType { enum SectionType {
case vpn case vpn
@ -123,9 +124,6 @@ extension ShortcutsAddViewController {
} }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard #available(iOS 12, *) else {
return
}
switch model.row(at: indexPath) { switch model.row(at: indexPath) {
case .connect: case .connect:
addConnect() addConnect()
@ -149,12 +147,9 @@ extension ShortcutsAddViewController {
addUntrustCellular() addUntrustCellular()
} }
} }
}
// MARK: Actions // MARK: Actions
@available(iOS 12, *)
extension ShortcutsAddViewController {
private func addConnect() { private func addConnect() {
guard TransientStore.shared.service.hasProfiles() else { guard TransientStore.shared.service.hasProfiles() else {
let alert = Macros.alert( let alert = Macros.alert(
@ -208,12 +203,10 @@ extension ShortcutsAddViewController {
@IBAction private func close() { @IBAction private func close() {
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)
} }
}
@available(iOS 12, *) // MARK: INUIAddVoiceShortcutViewControllerDelegate
extension ShortcutsAddViewController: INUIAddVoiceShortcutViewControllerDelegate {
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) { func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
tableView.reloadData()
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)
} }

View File

@ -28,7 +28,8 @@ import Intents
import IntentsUI import IntentsUI
import Passepartout_Core import Passepartout_Core
class ShortcutsConnectToViewController: UITableViewController, TableModelHost { @available(iOS 12, *)
class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewControllerDelegate, INUIAddVoiceShortcutViewControllerDelegate, TableModelHost {
private let service = TransientStore.shared.service private let service = TransientStore.shared.service
private var providers: [String] = [] private var providers: [String] = []
@ -89,9 +90,9 @@ class ShortcutsConnectToViewController: UITableViewController, TableModelHost {
vc.pools = provider.sortedPools() vc.pools = provider.sortedPools()
vc.delegate = self vc.delegate = self
} }
}
extension ShortcutsConnectToViewController { // MARK: UITableViewController
enum SectionType { enum SectionType {
case providers case providers
@ -130,9 +131,6 @@ extension ShortcutsConnectToViewController {
} }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard #available(iOS 12, *) else {
return
}
switch model.row(at: indexPath) { switch model.row(at: indexPath) {
case .providerShortcut: case .providerShortcut:
selectedProfile = service.profile(withContext: .provider, id: providers[indexPath.row]) selectedProfile = service.profile(withContext: .provider, id: providers[indexPath.row])
@ -143,12 +141,9 @@ extension ShortcutsConnectToViewController {
addConnect() addConnect()
} }
} }
}
// MARK: Actions // MARK: Actions
@available(iOS 12, *)
extension ShortcutsConnectToViewController {
private func addConnect() { private func addConnect() {
guard let host = selectedProfile as? HostConnectionProfile else { guard let host = selectedProfile as? HostConnectionProfile else {
fatalError("Not a HostConnectionProfile") fatalError("Not a HostConnectionProfile")
@ -186,19 +181,15 @@ extension ShortcutsConnectToViewController {
@IBAction private func done() { @IBAction private func done() {
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)
} }
}
extension ShortcutsConnectToViewController: ProviderPoolViewControllerDelegate { // MARK: ProviderPoolViewControllerDelegate
func providerPoolController(_: ProviderPoolViewController, didSelectPool pool: Pool) { func providerPoolController(_: ProviderPoolViewController, didSelectPool pool: Pool) {
guard #available(iOS 12, *) else {
return
}
addMoveToLocation(pool: pool) addMoveToLocation(pool: pool)
} }
}
// MARK: INUIAddVoiceShortcutViewControllerDelegate
@available(iOS 12, *)
extension ShortcutsConnectToViewController: INUIAddVoiceShortcutViewControllerDelegate {
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) { func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
navigationController?.popViewController(animated: true) navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)

View File

@ -28,6 +28,7 @@ import Intents
import IntentsUI import IntentsUI
import Passepartout_Core import Passepartout_Core
@available(iOS 12, *)
private struct ShortcutWrapper { private struct ShortcutWrapper {
let phrase: String let phrase: String
@ -35,7 +36,6 @@ private struct ShortcutWrapper {
let original: Any? let original: Any?
@available(iOS 12, *)
static func from(_ vs: INVoiceShortcut) -> ShortcutWrapper { static func from(_ vs: INVoiceShortcut) -> ShortcutWrapper {
return ShortcutWrapper( return ShortcutWrapper(
phrase: vs.invocationPhrase, phrase: vs.invocationPhrase,
@ -45,7 +45,8 @@ private struct ShortcutWrapper {
} }
} }
class ShortcutsEditViewController: UITableViewController, TableModelHost { @available(iOS 12, *)
class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutViewControllerDelegate, TableModelHost {
private var wrappers: [ShortcutWrapper]? private var wrappers: [ShortcutWrapper]?
private var editedIndexPath: IndexPath? private var editedIndexPath: IndexPath?
@ -73,9 +74,6 @@ class ShortcutsEditViewController: UITableViewController, TableModelHost {
title = L10n.Organizer.Cells.SiriShortcuts.caption title = L10n.Organizer.Cells.SiriShortcuts.caption
guard #available(iOS 12, *) else {
return
}
INVoiceShortcutCenter.shared.getAllVoiceShortcuts { [weak self] (shortcuts, error) in INVoiceShortcutCenter.shared.getAllVoiceShortcuts { [weak self] (shortcuts, error) in
DispatchQueue.main.async { DispatchQueue.main.async {
guard let shortcuts = shortcuts else { guard let shortcuts = shortcuts else {
@ -106,7 +104,6 @@ class ShortcutsEditViewController: UITableViewController, TableModelHost {
// present(alert, animated: true, completion: nil) // present(alert, animated: true, completion: nil)
} }
@available(iOS 12, *)
private func handleShortcuts(_ shortcuts: [INVoiceShortcut]) { private func handleShortcuts(_ shortcuts: [INVoiceShortcut]) {
wrappers = shortcuts.map { ShortcutWrapper.from($0) } wrappers = shortcuts.map { ShortcutWrapper.from($0) }
reloadModel() reloadModel()
@ -116,9 +113,9 @@ class ShortcutsEditViewController: UITableViewController, TableModelHost {
@IBAction private func close() { @IBAction private func close() {
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)
} }
}
extension ShortcutsEditViewController { // MARK: UITableViewController
enum SectionType { enum SectionType {
case all case all
} }
@ -162,9 +159,6 @@ extension ShortcutsEditViewController {
} }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard #available(iOS 12, *) else {
return
}
switch model.row(at: indexPath) { switch model.row(at: indexPath) {
case .shortcut: case .shortcut:
guard let wrapper = wrappers?[indexPath.row], let shortcut = wrapper.original as? INVoiceShortcut else { guard let wrapper = wrappers?[indexPath.row], let shortcut = wrapper.original as? INVoiceShortcut else {
@ -179,10 +173,9 @@ extension ShortcutsEditViewController {
addShortcut() addShortcut()
} }
} }
}
@available(iOS 12, *) // MARK: INUIEditVoiceShortcutViewControllerDelegate
extension ShortcutsEditViewController: INUIEditVoiceShortcutViewControllerDelegate {
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) { func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
editedIndexPath = nil editedIndexPath = nil
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)