summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/Login/LoginContentView.swift
blob: 19265d61dac9c7f98baf8f1769a136e5c31f363e (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
//
//  LoginContentView.swift
//  MullvadVPN
//
//  Created by pronebird on 22/03/2021.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

class LoginContentView: UIView {
    private var keyboardResponder: AutomaticKeyboardResponder?

    let titleLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = .mullvadBig
        textLabel.textColor = .white
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.adjustsFontForContentSizeCategory = true
        return textLabel
    }()

    let messageLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = .mullvadTinySemiBold
        textLabel.textColor = UIColor.white.withAlphaComponent(0.6)
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.adjustsFontForContentSizeCategory = true
        textLabel.numberOfLines = 0
        return textLabel
    }()

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

    let accountInputGroupWrapper: UIView = {
        let wrapperView = UIView()
        wrapperView.translatesAutoresizingMaskIntoConstraints = false
        return wrapperView
    }()

    let statusActivityView: StatusActivityView = {
        let statusActivityView = StatusActivityView(state: .hidden)
        statusActivityView.translatesAutoresizingMaskIntoConstraints = false
        statusActivityView.clipsToBounds = true
        return statusActivityView
    }()

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

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

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

    let footerLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = .mullvadSmall
        textLabel.textColor = UIColor.white.withAlphaComponent(0.6)
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.adjustsFontForContentSizeCategory = true
        textLabel.numberOfLines = 0
        textLabel.text = NSLocalizedString("Don’t have an account number?", comment: "")
        textLabel.numberOfLines = 0
        return textLabel
    }()

    let createAccountButton: AppButton = {
        let button = AppButton(style: .default)
        button.setAccessibilityIdentifier(.createAccountButton)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(NSLocalizedString("Create account", comment: ""), for: .normal)
        return button
    }()

    private var isStatusImageVisible = false
    private var contentContainerBottomConstraint: NSLayoutConstraint?

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

        backgroundColor = .primaryColor
        directionalLayoutMargins = UIMetrics.contentLayoutMargins
        setAccessibilityIdentifier(.loginView)

        accountInputGroup.textField.setAccessibilityIdentifier(.loginTextField)

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

                self?.layoutIfNeeded()
            }
        )

        addSubviews()
    }

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

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

        contentContainer.addSubview(statusActivityView)
        contentContainer.addSubview(formContainer)

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

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

        addConstrainedSubviews([contentContainer, footerContainer]) {
            contentContainer.pinEdges(PinnableEdges([.top(0)]), to: safeAreaLayoutGuide)
            contentContainer.pinEdgesToSuperview(PinnableEdges([.leading(0), .trailing(0)]))
            contentContainerBottomConstraint

            footerContainer.pinEdgesToSuperview(.all().excluding(.top))
            footerLabel.pinEdges(.all().excluding(.bottom), to: footerContainer.layoutMarginsGuide)

            createAccountButton.topAnchor.constraint(equalToSystemSpacingBelow: footerLabel.bottomAnchor, multiplier: 1)
            createAccountButton.pinEdges(.all().excluding(.top), to: footerContainer.layoutMarginsGuide)

            statusActivityView.centerXAnchor.constraint(equalTo: contentContainer.centerXAnchor)
            statusActivityView.widthAnchor.constraint(equalToConstant: 60.0)
            statusActivityView.heightAnchor.constraint(equalTo: statusActivityView.widthAnchor, multiplier: 1.0)

            formContainer.topAnchor.constraint(equalTo: statusActivityView.bottomAnchor, constant: 30)
            formContainer.centerYAnchor.constraint(equalTo: contentContainer.centerYAnchor, constant: -20)
            formContainer.pinEdges(PinnableEdges([.leading(0), .trailing(0)]), to: contentContainer)
            formContainer.pinEdges(PinnableEdges([.bottom(0)]), to: accountInputGroupWrapper)

            titleLabel.pinEdges(.all().excluding(.bottom), to: formContainer.layoutMarginsGuide)

            messageLabel.topAnchor.constraint(equalToSystemSpacingBelow: titleLabel.bottomAnchor, multiplier: 1)
            messageLabel.pinEdges(PinnableEdges([.leading(0), .trailing(0)]), to: formContainer.layoutMarginsGuide)

            accountInputGroupWrapper.topAnchor.constraint(
                equalToSystemSpacingBelow: messageLabel.bottomAnchor,
                multiplier: 1
            )
            accountInputGroupWrapper.pinEdges(
                PinnableEdges([.leading(0), .trailing(0)]),
                to: formContainer.layoutMarginsGuide
            )
            accountInputGroupWrapper.heightAnchor.constraint(equalTo: accountInputGroup.contentView.heightAnchor)
            accountInputGroup.pinEdges(.all().excluding(.bottom), to: accountInputGroupWrapper)
        }
    }
}