Server+UI.swift 2.67 KB
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
//
//  Server+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
import AlamofireImage

extension Server: CustomStringConvertible {
    func name(forStatus status: VPNStatus) -> String {
        switch status {
//        case .connecting, .changingServer, .connected:
17
        case .connected:
Davide De Rosa's avatar
Davide De Rosa committed
18 19 20 21 22 23
            guard !isAutomatic else {
                let effectiveServer = Client.providers.vpnProvider.profileServer ?? Client.providers.serverProvider.targetServer
                return "\(name) (\(effectiveServer.name))"
            }
            return name
            
24 25 26 27 28
        case .connecting:
            return L10n.Dashboard.Vpn.connecting
        case .disconnecting:
            return L10n.Dashboard.Vpn.disconnecting
        case .disconnected:
Davide De Rosa's avatar
Davide De Rosa committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
            return name
        }
    }
    
    func flagServer(forStatus status: VPNStatus) -> Server {
        switch status {
//        case .connecting, .changingServer, .connected:
        case .connecting, .connected:
            guard !isAutomatic else {
                return Client.providers.vpnProvider.profileServer ?? Client.providers.serverProvider.targetServer
            }
            return self
            
        default:
            return self
        }
    }

    public var description: String {
        return "\(name) [\(country)], \(hostname)"
    }
}

extension UIImageView {
    func setImage(fromServer server: Server) {
        let imageName = "flag-\(server.country.lowercased())"
        guard let image = UIImage(named: imageName) else {
            af_setImage(withURL: server.flagURL, placeholderImage: Asset.Flags.flagUniversal.image)
            return
        }
        self.image = image.withRenderingMode(.alwaysOriginal)
    }
}
Jose Blaya's avatar
Jose Blaya committed
62 63 64 65 66 67 68 69

extension UIButton {
    func setImage(fromServer server: Server) {
        let imageName = "flag-\(server.country.lowercased())"
        guard let image = UIImage(named: imageName) else {
            af_setImage(for: .normal, url: server.flagURL, placeholderImage: Asset.Flags.flagUniversal.image)
            return
        }
70 71 72 73 74 75 76 77 78 79 80 81 82 83
        let original = image.withRenderingMode(.alwaysOriginal)
        self.setImage(original.image(alpha: 0.7), for: .normal)
        self.setImage(image.withRenderingMode(.alwaysOriginal), for: .highlighted)
        
    }
}

extension UIImage {
    func image(alpha: CGFloat) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        draw(at: .zero, blendMode: .normal, alpha: alpha)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
Jose Blaya's avatar
Jose Blaya committed
84 85
    }
}