Observe wizard creation via notifications

Flow is too scattered to safely maintain delegation.
This commit is contained in:
Davide De Rosa 2018-10-27 20:10:59 +02:00
parent fa59b8b5f9
commit 326c5b823d
6 changed files with 32 additions and 32 deletions

View File

@ -126,14 +126,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
wizard.parsedFile = parsedFile
wizard.removesConfigurationOnCancel = true
// best effort to delegate to main vc
let split = root as? UISplitViewController
let master = split?.viewControllers.first as? UINavigationController
master?.viewControllers.forEach {
if let organizerVC = $0 as? OrganizerViewController {
wizard.delegate = organizerVC
}
}
nav.modalPresentationStyle = .formSheet
root.present(nav, animated: true, completion: nil)
}

View File

@ -34,8 +34,6 @@ class ImportedHostsViewController: UITableViewController {
private var parsedFile: ParsedFile?
weak var wizardDelegate: WizardDelegate?
override func viewDidLoad() {
super.viewDidLoad()
@ -104,7 +102,6 @@ class ImportedHostsViewController: UITableViewController {
return
}
wizard.parsedFile = parsedFile
wizard.delegate = wizardDelegate
// retain back button
wizard.navigationItem.leftBarButtonItem = nil

View File

@ -70,6 +70,10 @@ class OrganizerViewController: UITableViewController, TableModelHost {
// MARK: UIViewController
deinit {
NotificationCenter.default.removeObserver(self)
}
override func awakeFromNib() {
super.awakeFromNib()
@ -90,6 +94,8 @@ class OrganizerViewController: UITableViewController, TableModelHost {
}
service.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(wizardDidCreate(notification:)), name: .WizardDidCreate, object: nil)
}
override func viewDidAppear(_ animated: Bool) {
@ -134,14 +140,9 @@ class OrganizerViewController: UITableViewController, TableModelHost {
assert(selectedProfile != nil, "No selected profile")
vc.profile = selectedProfile
} else if let vc = destination as? Wizard {
if let providerVC = vc as? WizardProviderViewController {
} else if let providerVC = destination as? WizardProviderViewController {
providerVC.availableNames = availableProviderNames ?? []
}
vc.delegate = self
} else if let vc = destination as? ImportedHostsViewController {
vc.wizardDelegate = self
}
}
// MARK: Actions
@ -436,8 +437,14 @@ extension OrganizerViewController: ConnectionServiceDelegate {
}
}
extension OrganizerViewController: WizardDelegate {
func wizard(didCreate profile: ConnectionProfile, withCredentials credentials: Credentials) {
extension OrganizerViewController {
@objc private func wizardDidCreate(notification: Notification) {
guard let profile = notification.userInfo?[WizardCreationKey.profile] as? ConnectionProfile,
let credentials = notification.userInfo?[WizardCreationKey.credentials] as? Credentials else {
fatalError("WizardDidCreate notification must post profile and credentials")
}
service.addOrReplaceProfile(profile, credentials: credentials)
TransientStore.shared.serialize() // add

View File

@ -29,7 +29,7 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
class WizardHostViewController: UITableViewController, TableModelHost {
@IBOutlet private weak var itemNext: UIBarButtonItem!
private let existingHosts: [String] = {
@ -46,8 +46,6 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
private var createdProfile: HostConnectionProfile?
weak var delegate: WizardDelegate?
// MARK: TableModelHost
lazy var model: TableModel<SectionType, RowType> = {
@ -150,7 +148,10 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
}
dismiss(animated: true) {
self.delegate?.wizard(didCreate: profile, withCredentials: credentials)
NotificationCenter.default.post(name: .WizardDidCreate, object: nil, userInfo: [
WizardCreationKey.profile: profile,
WizardCreationKey.credentials: credentials
])
}
}

View File

@ -25,13 +25,11 @@
import UIKit
class WizardProviderViewController: UITableViewController, Wizard {
class WizardProviderViewController: UITableViewController {
var availableNames: [Infrastructure.Name] = []
private var createdProfile: ProviderConnectionProfile?
weak var delegate: WizardDelegate?
override func viewDidLoad() {
super.viewDidLoad()
@ -55,7 +53,10 @@ class WizardProviderViewController: UITableViewController, Wizard {
fatalError("No profile created?")
}
dismiss(animated: true) {
self.delegate?.wizard(didCreate: profile, withCredentials: credentials)
NotificationCenter.default.post(name: .WizardDidCreate, object: nil, userInfo: [
WizardCreationKey.profile: profile,
WizardCreationKey.credentials: credentials
])
}
}

View File

@ -25,10 +25,12 @@
import Foundation
protocol Wizard: class {
var delegate: WizardDelegate? { get set }
extension Notification.Name {
static let WizardDidCreate = Notification.Name("WizardDidCreate")
}
protocol WizardDelegate: class {
func wizard(didCreate profile: ConnectionProfile, withCredentials credentials: Credentials)
enum WizardCreationKey: String {
case profile
case credentials
}