aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/ViewControllers/SetttingsTableViewController.swift
blob: 042181553ae7d6fd47ed9fa5fcea63ca7c603e6c (plain) (blame)
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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
//  Copyright © 2018 WireGuard LLC. All rights reserved.
//

import UIKit
import PromiseKit

enum GoVersionError: Error {
    case noDelegate
}

protocol SettingsTableViewControllerDelegate: class {
    func exportTunnels(settingsTableViewController: SettingsTableViewController, sourceView: UIView)
    func goVersionInformation() -> Promise<String>
}

class SettingsTableViewController: UITableViewController {

    weak var delegate: SettingsTableViewControllerDelegate?
    @IBOutlet weak var versionInfoCell: UITableViewCell!
    @IBOutlet weak var goVersionInfoCell: UITableViewCell!
    @IBOutlet weak var exportCell: UITableViewCell!

    @IBOutlet weak var versionInfoLabel: UILabel!
    @IBOutlet weak var goVersionInfoLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        versionInfoLabel.text = versionInformation
        _ = firstly { () -> Promise<String> in
            self.goVersionInfoLabel.text = NSLocalizedString("Loading...", comment: "")
            return goVersionInformation()
        }.then { (goVersion: String) -> Guarantee<Void> in
            if let label = self.goVersionInfoLabel {
                label.text = goVersion
            }
            return Guarantee.value(())
            }.recover({ (_) in
                self.goVersionInfoLabel.text = NSLocalizedString("Unknown", comment: "")
            })
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            switch cell {
            case versionInfoCell, goVersionInfoCell:
                UIPasteboard.general.string = ["WireGuard for iOS:", versionInformation, "Go userspace backend:", goVersionInfoLabel.text ?? ""].joined(separator: "\n")
                showCopyConfirmation()
            case exportCell:
                delegate?.exportTunnels(settingsTableViewController: self, sourceView: exportCell)
            default:
                ()
            }
            tableView.deselectRow(at: indexPath, animated: true)
        }
    }

    var versionInformation: String {
        var versionElements: [String] = []
        if let appBuildNumber = Bundle.main.infoDictionary!["CFBundleVersion"] as? String, let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String {
            versionElements.append(appVersion)
            versionElements.append("(\(appBuildNumber))")
        }

        return versionElements.joined(separator: " ")
    }

    func goVersionInformation() -> Promise<String> {
        return self.delegate?.goVersionInformation() ?? Promise(error: GoVersionError.noDelegate)
    }

    private func showCopyConfirmation() {
        let confirmationAlertController = UIAlertController(title: NSLocalizedString("Copied version information", comment: ""), message: UIPasteboard.general.string, preferredStyle: .alert)
        confirmationAlertController.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: "Generic OK button"), style: .default, handler: nil))

        present(confirmationAlertController, animated: true, completion: nil)

    }
}

extension SettingsTableViewController: Identifyable {}