summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/LoginContentView.swift
blob: 1952bb2e4eb064ef81d8daa2e361974c76c5b393 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
//  LoginContentView.swift
//  MullvadVPN
//
//  Created by pronebird on 22/03/2021.
//  Copyright © 2021 Mullvad VPN AB. All rights reserved.
//

import UIKit

class LoginContentView: UIView {

    private var keyboardResponder: AutomaticKeyboardResponder?

    let titleLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = UIFont.systemFont(ofSize: 32)
        textLabel.textColor = .white
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        return textLabel
    }()

    let messageLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = UIFont.systemFont(ofSize: 17)
        textLabel.textColor = UIColor.white.withAlphaComponent(0.6)
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.numberOfLines = 0
        return textLabel
    }()

    let accountInputGroup: AccountInputGroupView = {
        let inputGroup = AccountInputGroupView()
        inputGroup.translatesAutoresizingMaskIntoConstraints = false
        return inputGroup
    }()

    let statusImageView: StatusImageView = {
        let imageView = StatusImageView(style: .failure)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.alpha = 0
        return imageView
    }()

    let contentContainer: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layoutMargins = UIMetrics.contentLayoutMargins
        return view
    }()

    let formContainer: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layoutMargins = UIMetrics.contentLayoutMargins
        return view
    }()

    let footerContainer: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layoutMargins = UIMetrics.contentLayoutMargins
        view.backgroundColor = .secondaryColor
        return view
    }()

    let footerLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = UIFont.systemFont(ofSize: 17)
        textLabel.textColor = UIColor.white.withAlphaComponent(0.6)
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.text = NSLocalizedString(
            "CREATE_BUTTON_HEADER_LABEL",
            tableName: "Login",
            comment: ""
        )
        return textLabel
    }()

    let createAccountButton: AppButton = {
        let button = AppButton(style: .default)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(NSLocalizedString(
            "CREATE_ACCOUNT_BUTTON_LABEL",
            tableName: "Login",
            comment: ""
        ), for: .normal)
        return button
    }()

    let activityIndicator: SpinnerActivityIndicatorView = {
        let view = SpinnerActivityIndicatorView(style: .large)
        view.tintColor = .white
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    private var isStatusImageVisible = false
    private var contentContainerBottomConstraint: NSLayoutConstraint?

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .primaryColor
        layoutMargins = UIMetrics.contentLayoutMargins

        accountInputGroup.textField.accessibilityIdentifier = "LoginTextField"

        keyboardResponder = AutomaticKeyboardResponder(targetView: self, handler: { [weak self] (view, adjustment) in
            self?.contentContainerBottomConstraint?.constant = adjustment

            self?.layoutIfNeeded()
            self?.updateStatusImageVisibility(animated: false)
        })

        addSubviews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setStatusImage(style: StatusImageView.Style?, visible: Bool, animated: Bool) {
        if let style = style {
            statusImageView.style = style
        }

        isStatusImageVisible = visible
        updateStatusImageVisibility(animated: animated)
    }

    private func updateStatusImageVisibility(animated: Bool) {
        let statusImageFrame = statusImageView.convert(statusImageView.bounds, to: self)
        let shouldShow = isStatusImageVisible && safeAreaLayoutGuide.layoutFrame.contains(statusImageFrame)

        let actions = {
            // Only display the status image if it doesn't overlap the safe area layout guide.
            if shouldShow {
                self.statusImageView.alpha = 1
            } else {
                self.statusImageView.alpha = 0
            }
        }

        if animated {
            UIView.animate(withDuration: 0.25) {
                actions()
            }
        } else {
            actions()
        }
    }

    private func addSubviews() {
        formContainer.addSubview(titleLabel)
        formContainer.addSubview(messageLabel)
        formContainer.addSubview(accountInputGroup)

        contentContainer.addSubview(activityIndicator)
        contentContainer.addSubview(statusImageView)
        contentContainer.addSubview(formContainer)

        footerContainer.addSubview(footerLabel)
        footerContainer.addSubview(createAccountButton)

        addSubview(contentContainer)
        addSubview(footerContainer)

        let contentContainerBottomConstraint = bottomAnchor.constraint(equalTo: contentContainer.bottomAnchor)
        self.contentContainerBottomConstraint = contentContainerBottomConstraint

        NSLayoutConstraint.activate([
            contentContainer.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
            contentContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
            contentContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
            contentContainerBottomConstraint,

            footerContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
            footerContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
            footerContainer.bottomAnchor.constraint(equalTo: bottomAnchor),

            footerLabel.topAnchor.constraint(equalTo: footerContainer.layoutMarginsGuide.topAnchor),
            footerLabel.leadingAnchor.constraint(equalTo: footerContainer.layoutMarginsGuide.leadingAnchor),
            footerLabel.trailingAnchor.constraint(equalTo: footerContainer.layoutMarginsGuide.trailingAnchor),

            createAccountButton.topAnchor.constraint(equalToSystemSpacingBelow: footerLabel.bottomAnchor, multiplier: 1),
            createAccountButton.leadingAnchor.constraint(equalTo: footerContainer.layoutMarginsGuide.leadingAnchor),
            createAccountButton.trailingAnchor.constraint(equalTo: footerContainer.layoutMarginsGuide.trailingAnchor),
            createAccountButton.bottomAnchor.constraint(equalTo: footerContainer.layoutMarginsGuide.bottomAnchor),

            statusImageView.centerXAnchor.constraint(equalTo: contentContainer.centerXAnchor),
            formContainer.topAnchor.constraint(equalTo: statusImageView.bottomAnchor, constant: 30),
            formContainer.centerYAnchor.constraint(equalTo: contentContainer.centerYAnchor, constant: -20),
            formContainer.leadingAnchor.constraint(equalTo: contentContainer.leadingAnchor),
            formContainer.trailingAnchor.constraint(equalTo: contentContainer.trailingAnchor),

            activityIndicator.centerXAnchor.constraint(equalTo: statusImageView.centerXAnchor),
            activityIndicator.centerYAnchor.constraint(equalTo: statusImageView.centerYAnchor),

            titleLabel.topAnchor.constraint(equalTo: formContainer.topAnchor),
            titleLabel.leadingAnchor.constraint(equalTo: formContainer.layoutMarginsGuide.leadingAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: formContainer.layoutMarginsGuide.trailingAnchor),

            messageLabel.topAnchor.constraint(equalToSystemSpacingBelow: titleLabel.bottomAnchor, multiplier: 1),
            messageLabel.leadingAnchor.constraint(equalTo: formContainer.layoutMarginsGuide.leadingAnchor),
            messageLabel.trailingAnchor.constraint(equalTo: formContainer.layoutMarginsGuide.trailingAnchor),

            accountInputGroup.topAnchor.constraint(equalToSystemSpacingBelow: messageLabel.bottomAnchor, multiplier: 1),
            accountInputGroup.leadingAnchor.constraint(equalTo: formContainer.layoutMarginsGuide.leadingAnchor),
            accountInputGroup.trailingAnchor.constraint(equalTo: formContainer.layoutMarginsGuide.trailingAnchor),
            accountInputGroup.bottomAnchor.constraint(equalTo: formContainer.bottomAnchor),
        ])
    }

}