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

View File

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

View File

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