ServerProvider+UI.swift 984 Bytes
Newer Older
Davide De Rosa's avatar
Davide De Rosa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
//
//  ServerProvider+UI.swift
//  PIA VPN
//
//  Created by Davide De Rosa on 12/11/17.
//  Copyright © 2017 London Trust Media. All rights reserved.
//

import Foundation
import PIALibrary

extension Client.Preferences {

    // treat nil preferredServer as automatic (app defined)
    var displayedServer: Server {
        get {
            return preferredServer ?? .automatic
        }
        set {
            let ed = editable()
            if newValue.isAutomatic {
                ed.preferredServer = nil
            } else {
                ed.preferredServer = newValue
            }
            let action = ed.requiredVPNAction()
            ed.commit()

            action?.execute { (error) in
                let vpn = Client.providers.vpnProvider
                if (vpn.vpnStatus != .disconnected) {
                    vpn.reconnect(after: nil, nil)
33 34
                } else {
                    vpn.connect(nil)
Davide De Rosa's avatar
Davide De Rosa committed
35 36 37 38 39
                }
            }
        }
    }
}