From 5ee18071b0ac50ccde3752669b0e0811235e21ec Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Wed, 27 Mar 2019 20:52:58 +0100 Subject: [PATCH] Add shortcut and delegate to manager --- .../ShortcutsAddViewController.swift | 16 ++++++++++-- .../ShortcutsConnectToViewController.swift | 4 --- .../ShortcutsEditViewController.swift | 25 ++++++++++++++++++- .../en.lproj/Shortcuts.storyboard | 8 +----- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Passepartout-iOS/Scenes/Organizer/ShortcutsAddViewController.swift b/Passepartout-iOS/Scenes/Organizer/ShortcutsAddViewController.swift index 805c213f..0863df67 100644 --- a/Passepartout-iOS/Scenes/Organizer/ShortcutsAddViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/ShortcutsAddViewController.swift @@ -27,8 +27,16 @@ import UIKit import IntentsUI import Passepartout_Core +@available(iOS 12, *) +protocol ShortcutsAddViewControllerDelegate: class { + func shortcutAddController(_ controller: ShortcutsAddViewController, voiceShortcut: INVoiceShortcut) + + func shortcutAddControllerDidCancel(_ controller: ShortcutsAddViewController) +} + @available(iOS 12, *) class ShortcutsAddViewController: UITableViewController, INUIAddVoiceShortcutViewControllerDelegate, TableModelHost { + weak var delegate: ShortcutsAddViewControllerDelegate? // MARK: TableModel @@ -207,10 +215,14 @@ class ShortcutsAddViewController: UITableViewController, INUIAddVoiceShortcutVie // MARK: INUIAddVoiceShortcutViewControllerDelegate func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) { - dismiss(animated: true, completion: nil) + guard let voiceShortcut = voiceShortcut else { + delegate?.shortcutAddControllerDidCancel(self) + return + } + delegate?.shortcutAddController(self, voiceShortcut: voiceShortcut) } func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) { - dismiss(animated: true, completion: nil) + delegate?.shortcutAddControllerDidCancel(self) } } diff --git a/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift b/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift index 910c16bf..195d851b 100644 --- a/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/ShortcutsConnectToViewController.swift @@ -178,10 +178,6 @@ class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewC perform(segue: StoryboardSegue.Shortcuts.pickLocationSegueIdentifier) } - @IBAction private func done() { - dismiss(animated: true, completion: nil) - } - // MARK: ProviderPoolViewControllerDelegate func providerPoolController(_: ProviderPoolViewController, didSelectPool pool: Pool) { diff --git a/Passepartout-iOS/Scenes/Organizer/ShortcutsEditViewController.swift b/Passepartout-iOS/Scenes/Organizer/ShortcutsEditViewController.swift index 3720bb2c..97ac30cd 100644 --- a/Passepartout-iOS/Scenes/Organizer/ShortcutsEditViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/ShortcutsEditViewController.swift @@ -46,7 +46,7 @@ private struct ShortcutWrapper { } @available(iOS 12, *) -class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutViewControllerDelegate, TableModelHost { +class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutViewControllerDelegate, ShortcutsAddViewControllerDelegate, TableModelHost { private var wrappers: [ShortcutWrapper]? private var editedIndexPath: IndexPath? @@ -87,6 +87,12 @@ class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutV // MARK: Actions + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if let vc = segue.destination as? ShortcutsAddViewController { + vc.delegate = self + } + } + private func addShortcut() { perform(segue: StoryboardSegue.Shortcuts.shortcutAddSegueIdentifier) } @@ -173,6 +179,23 @@ class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutV addShortcut() } } + + // MARK: ShortcutsAddViewControllerDelegate + + func shortcutAddController(_ controller: ShortcutsAddViewController, voiceShortcut: INVoiceShortcut) { + + // TODO: sort voice shortcuts by? + wrappers?.append(ShortcutWrapper.from(voiceShortcut)) + reloadModel() + tableView.reloadData() + + navigationController?.popToRootViewController(animated: false) + dismiss(animated: true, completion: nil) + } + + func shortcutAddControllerDidCancel(_ controller: ShortcutsAddViewController) { + dismiss(animated: true, completion: nil) + } // MARK: INUIEditVoiceShortcutViewControllerDelegate diff --git a/Passepartout-iOS/en.lproj/Shortcuts.storyboard b/Passepartout-iOS/en.lproj/Shortcuts.storyboard index e1561ed4..467b6b4d 100644 --- a/Passepartout-iOS/en.lproj/Shortcuts.storyboard +++ b/Passepartout-iOS/en.lproj/Shortcuts.storyboard @@ -96,13 +96,7 @@ - - - - - - - +