summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/AccountViewController.swift
blob: e081b88858097ef698292e3b469411a3f3a342f1 (plain)
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
82
83
84
//
//  AccountViewController.swift
//  MullvadVPN
//
//  Created by pronebird on 20/03/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import Combine
import UIKit

class AccountViewController: UIViewController {

    @IBOutlet var accountTokenButton: UIButton!
    @IBOutlet var expiryLabel: UILabel!

    private var logoutSubscriber: AnyCancellable?
    private var copyToPasteboardSubscriber: AnyCancellable?

    override func viewDidLoad() {
        super.viewDidLoad()

        accountTokenButton.setTitle(Account.shared.token, for: .normal)

        if let expiryDate = Account.shared.expiry {
            let accountExpiry = AccountExpiry(date: expiryDate)

            if accountExpiry.isExpired {
                expiryLabel.text = NSLocalizedString("OUT OF TIME", comment: "")
                expiryLabel.textColor = .dangerColor
            } else {
                expiryLabel.text = accountExpiry.formattedDate
                expiryLabel.textColor = .white
            }
        }
    }

    // MARK: - Actions

    @IBAction func doBuyCredits() {
        UIApplication.shared.open(WebLinks.purchaseURL, options: [:])
    }

    @IBAction func doLogout() {
        let message = NSLocalizedString("Logging out. Please wait...",
                                        comment: "A modal message displayed during log out")

        let alertController = UIAlertController(
            title: nil,
            message: message,
            preferredStyle: .alert)

        present(alertController, animated: true) {
            self.logoutSubscriber = Account.shared.logout()
                .delay(for: .seconds(1), scheduler: DispatchQueue.main)
                .sink(receiveCompletion: { (completion) in
                    switch completion {
                    case .failure(let error):
                        alertController.dismiss(animated: true) {
                            self.presentError(error, preferredStyle: .alert)
                        }

                    case .finished:
                        self.performSegue(withIdentifier: SegueIdentifier.Account.logout.rawValue, sender: self)
                    }
                })
        }
    }

    @IBAction func copyAccountToken() {
        UIPasteboard.general.string = Account.shared.token

        accountTokenButton.setTitle(
            NSLocalizedString("COPIED TO PASTEBOARD!", comment: ""),
            for: .normal)

        copyToPasteboardSubscriber =
            Just(()).delay(for: .seconds(3), scheduler: DispatchQueue.main)
                .sink(receiveValue: { _ in
                    self.accountTokenButton.setTitle(Account.shared.token, for: .normal)
                })
    }

}