Delegate profile management from ConnectionService

Makes much more sense than dumb wizard notifications. Organizer is
already a ConnectionServiceDelegate.
This commit is contained in:
Davide De Rosa 2018-11-02 14:20:40 +01:00
parent a0ff420f54
commit 3f1e98f8ac
6 changed files with 37 additions and 75 deletions

View File

@ -70,10 +70,6 @@ class OrganizerViewController: UITableViewController, TableModelHost {
// MARK: UIViewController
deinit {
NotificationCenter.default.removeObserver(self)
}
override func awakeFromNib() {
super.awakeFromNib()
@ -94,8 +90,6 @@ class OrganizerViewController: UITableViewController, TableModelHost {
}
service.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(wizardDidCreate(notification:)), name: .WizardDidCreate, object: nil)
}
override func viewDidAppear(_ animated: Bool) {
@ -428,24 +422,7 @@ extension OrganizerViewController {
// MARK: -
extension OrganizerViewController: ConnectionServiceDelegate {
func connectionService(didDeactivate profile: ConnectionProfile) {
tableView.reloadData()
}
func connectionService(didActivate profile: ConnectionProfile) {
tableView.reloadData()
}
}
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)
func connectionService(didAdd profile: ConnectionProfile) {
TransientStore.shared.serialize() // add
reloadModel()
@ -467,4 +444,24 @@ extension OrganizerViewController {
}
perform(segue: StoryboardSegue.Organizer.selectProfileSegueIdentifier, sender: profile)
}
func connectionService(didRename profile: ConnectionProfile) {
TransientStore.shared.serialize() // rename
reloadModel()
tableView.reloadData()
}
func connectionService(didRemoveProfileWithKey key: ConnectionService.ProfileKey) {
reloadModel()
tableView.reloadData()
}
func connectionService(didDeactivate profile: ConnectionProfile) {
tableView.reloadData()
}
func connectionService(didActivate profile: ConnectionProfile) {
tableView.reloadData()
}
}

View File

@ -130,9 +130,10 @@ class WizardHostViewController: UITableViewController, TableModelHost {
guard let profile = createdProfile else {
fatalError("No profile created?")
}
let service = TransientStore.shared.service
if let url = parsedFile?.url {
do {
let savedURL = try TransientStore.shared.service.save(configurationURL: url, for: profile)
let savedURL = try service.save(configurationURL: url, for: profile)
log.debug("Associated .ovpn configuration file to profile '\(profile.id)': \(savedURL)")
// can now delete imported file
@ -141,12 +142,8 @@ class WizardHostViewController: UITableViewController, TableModelHost {
log.error("Could not associate .ovpn configuration file to profile: \(e)")
}
}
dismiss(animated: true) {
NotificationCenter.default.post(name: .WizardDidCreate, object: nil, userInfo: [
WizardCreationKey.profile: profile,
WizardCreationKey.credentials: credentials
])
service.addOrReplaceProfile(profile, credentials: credentials)
}
}

View File

@ -52,11 +52,9 @@ class WizardProviderViewController: UITableViewController {
guard let profile = createdProfile else {
fatalError("No profile created?")
}
let service = TransientStore.shared.service
dismiss(animated: true) {
NotificationCenter.default.post(name: .WizardDidCreate, object: nil, userInfo: [
WizardCreationKey.profile: profile,
WizardCreationKey.credentials: credentials
])
service.addOrReplaceProfile(profile, credentials: credentials)
}
}

View File

@ -43,7 +43,6 @@
0E89DFC5213DF7AE00741BA1 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E89DFC4213DF7AE00741BA1 /* Preferences.swift */; };
0E89DFC8213E8FC500741BA1 /* SessionProxy+Communication.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E89DFC7213E8FC500741BA1 /* SessionProxy+Communication.swift */; };
0E89DFCE213EEDFA00741BA1 /* WizardProviderViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E89DFCD213EEDFA00741BA1 /* WizardProviderViewController.swift */; };
0E89DFD0213F223400741BA1 /* Wizard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E89DFCF213F223400741BA1 /* Wizard.swift */; };
0E8D97E221388B52006FB4A0 /* InfrastructurePreset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E8D97E121388B52006FB4A0 /* InfrastructurePreset.swift */; };
0E8D97E521389277006FB4A0 /* pia.json in Resources */ = {isa = PBXBuildFile; fileRef = 0E8D97E421389276006FB4A0 /* pia.json */; };
0EA068F4218475F800C320AD /* ParsedFile+Alerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EA068F3218475F800C320AD /* ParsedFile+Alerts.swift */; };
@ -166,7 +165,6 @@
0E89DFC4213DF7AE00741BA1 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
0E89DFC7213E8FC500741BA1 /* SessionProxy+Communication.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SessionProxy+Communication.swift"; sourceTree = "<group>"; };
0E89DFCD213EEDFA00741BA1 /* WizardProviderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WizardProviderViewController.swift; sourceTree = "<group>"; };
0E89DFCF213F223400741BA1 /* Wizard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Wizard.swift; sourceTree = "<group>"; };
0E8D97E121388B52006FB4A0 /* InfrastructurePreset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfrastructurePreset.swift; sourceTree = "<group>"; };
0E8D97E421389276006FB4A0 /* pia.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = pia.json; sourceTree = "<group>"; };
0EA068F3218475F800C320AD /* ParsedFile+Alerts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ParsedFile+Alerts.swift"; sourceTree = "<group>"; };
@ -399,7 +397,6 @@
0E2B494120FD16540094784C /* TransientStore.swift */,
0E4C9CB820DB9BC600A0C59C /* TrustedNetworks.swift */,
0EBE3A8F213C6F4000BFA2F5 /* TrustPolicy.swift */,
0E89DFCF213F223400741BA1 /* Wizard.swift */,
);
path = Model;
sourceTree = "<group>";
@ -845,7 +842,6 @@
0E05C5D520D1645F006EE732 /* SettingTableViewCell.swift in Sources */,
0EBE3A84213C6ADE00BFA2F5 /* InfrastructureFactory.swift in Sources */,
0E4FD7DE20D3E49A002221FF /* StandardVPNProvider.swift in Sources */,
0E89DFD0213F223400741BA1 /* Wizard.swift in Sources */,
0E89DFCE213EEDFA00741BA1 /* WizardProviderViewController.swift in Sources */,
0EBE3AA1213DC1A100BFA2F5 /* ConnectionService.swift in Sources */,
0E1D72B2213BFFCF00BA1586 /* ProviderPresetViewController.swift in Sources */,

View File

@ -31,6 +31,12 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
protocol ConnectionServiceDelegate: class {
func connectionService(didAdd profile: ConnectionProfile)
func connectionService(didRename profile: ConnectionProfile)
func connectionService(didRemoveProfileWithKey key: ConnectionService.ProfileKey)
func connectionService(didActivate profile: ConnectionProfile)
func connectionService(didDeactivate profile: ConnectionProfile)
@ -338,6 +344,8 @@ class ConnectionService: Codable {
// serialize immediately
try? saveProfiles()
delegate?.connectionService(didAdd: profile)
}
func removeProfile(_ key: ProfileKey) {
@ -349,6 +357,8 @@ class ConnectionService: Codable {
if cache.isEmpty {
activeProfileKey = nil
}
delegate?.connectionService(didRemoveProfileWithKey: key)
}
func containsProfile(_ key: ProfileKey) -> Bool {

View File

@ -1,36 +0,0 @@
//
// Wizard.swift
// Passepartout
//
// Created by Davide De Rosa on 9/4/18.
// Copyright (c) 2018 Davide De Rosa. All rights reserved.
//
// https://github.com/keeshux
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
extension Notification.Name {
static let WizardDidCreate = Notification.Name("WizardDidCreate")
}
enum WizardCreationKey: String {
case profile
case credentials
}