LoginViewController.swift 10 KB
Newer Older
domenicw's avatar
domenicw committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
//  LoginViewController.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 18.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class LoginViewController: UIViewController {
    
    // MARK: - Variables
    
domenicw's avatar
domenicw committed
16
17
18
19
    public var delegate: LoginViewControllerDelegate?
    
    private let infoLabelText: String = "Login in using your ETHZ username (email address) and your ETHZ password."
    
domenicw's avatar
domenicw committed
20
21
22
23
    // MARK: - View variables
    
    public private(set) var titleLabel: UILabel!
    
domenicw's avatar
domenicw committed
24
25
    public private(set) var infoLabel: UILabel!
    
domenicw's avatar
domenicw committed
26
27
    public private(set) var submitButton: UIButton!
    
domenicw's avatar
domenicw committed
28
29
    public private(set) var cancelButton: UIButton!
    
domenicw's avatar
domenicw committed
30
31
32
33
34
35
36
37
38
39
    public private(set) var usernameTextField: UITextField!
    
    public private(set) var passwordTextField: UITextField!
    
    // MARK: - Initializers
    
    public init() {
        super.init(nibName: nil, bundle: nil)
        
        // View Creation
domenicw's avatar
domenicw committed
40
41
42
43
44
45
        self.titleLabel = self.createTitleLabel()
        self.view.addSubview(self.titleLabel)
        
        self.infoLabel = self.createInfoTextLabel()
        self.view.addSubview(self.infoLabel)
        
domenicw's avatar
domenicw committed
46
47
48
        self.submitButton = self.createSubmitButton()
        self.view.addSubview(self.submitButton)
        
domenicw's avatar
domenicw committed
49
50
51
        self.cancelButton = self.createCancelButton()
        self.view.addSubview(self.cancelButton)
        
domenicw's avatar
domenicw committed
52
53
54
55
56
57
58
59
        self.usernameTextField = self.createUsernameTextField()
        self.view.addSubview(self.usernameTextField)
        
        self.passwordTextField = self.createPasswordTextField()
        self.view.addSubview(self.passwordTextField)
        
        // View Constraints
        self.applyTitleLabelConstraints()
domenicw's avatar
domenicw committed
60
        self.applyInfoLabelConstraints()
domenicw's avatar
domenicw committed
61
62
63
        self.applyUsernameTextFieldConstraints()
        self.applyPasswordTextFieldConstraints()
        self.applySubmitButtonConstraints()
domenicw's avatar
domenicw committed
64
        self.applyCancelButtonConstraints()
domenicw's avatar
domenicw committed
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
        
        // Tap view to hide keyboard
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboard))
        self.view.addGestureRecognizer(recognizer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Creation
    
    private func createTitleLabel() -> UILabel {
        let label = UILabel()
        label.text = "Login"
        label.textColor = .amivRed
        label.numberOfLines = 2
        label.textAlignment = .center
        label.font = UIFont.preferredFont(forTextStyle: .largeTitle).bold()
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }
    
    private func applyTitleLabelConstraints() {
        NSLayoutConstraint(item: self.titleLabel, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .topMargin, multiplier: 1, constant: 30).isActive = true
        NSLayoutConstraint(item: self.titleLabel, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.titleLabel, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
    }
    
domenicw's avatar
domenicw committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    private func createInfoTextLabel() -> UILabel {
        let label = UILabel()
        label.text = self.infoLabelText
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.preferredFont(forTextStyle: .body)
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }
    
    private func applyInfoLabelConstraints() {
        NSLayoutConstraint(item: self.infoLabel, attribute: .top, relatedBy: .equal, toItem: self.titleLabel, attribute: .bottom, multiplier: 1, constant: 20).isActive = true
        NSLayoutConstraint(item: self.infoLabel, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.infoLabel, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
    }
    
domenicw's avatar
domenicw committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    private func createSubmitButton() -> UIButton {
        let button = UIButton()
        button.setTitle("Login", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.setTitleColor(.lightGray, for: .highlighted)
        button.backgroundColor = .amivRed
        button.layer.cornerRadius = 12
        button.addTarget(self, action: #selector(self.login), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        return button
    }
    
    private func applySubmitButtonConstraints() {
        NSLayoutConstraint(item: self.submitButton, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.submitButton, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: self.submitButton, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottomMargin, multiplier: 1, constant: -10).isActive = true
domenicw's avatar
domenicw committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        NSLayoutConstraint(item: self.submitButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50).isActive = true
    }
    
    private func createCancelButton() -> UIButton {
        let button = UIButton()
        button.setTitle("Cancel", for: .normal)
        button.setTitleColor(.amivRed, for: .normal)
        button.setTitleColor(.lightGray, for: .highlighted)
        button.addTarget(self, action: #selector(self.cancel), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        return button
    }
    
    private func applyCancelButtonConstraints() {
        NSLayoutConstraint(item: self.cancelButton, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: self.passwordTextField, attribute: .bottom, multiplier: 1, constant: 30).isActive = true
        NSLayoutConstraint(item: self.cancelButton, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.cancelButton, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: self.cancelButton, attribute: .bottom, relatedBy: .equal, toItem: self.submitButton, attribute: .top, multiplier: 1, constant: -15).isActive = true
domenicw's avatar
domenicw committed
148
149
150
151
152
153
    }
    
    private func createUsernameTextField() -> UITextField {
        let field = UITextField()
        field.tintColor = .amivRed
        field.placeholder = "Username"
domenicw's avatar
domenicw committed
154
155
        field.returnKeyType = .continue
        field.borderStyle = .roundedRect
domenicw's avatar
domenicw committed
156
        field.addTarget(self, action: #selector(self.fillPassword), for: .primaryActionTriggered)
domenicw's avatar
domenicw committed
157
        field.translatesAutoresizingMaskIntoConstraints = false
domenicw's avatar
domenicw committed
158
159
160
161
162
        
        return field
    }
    
    private func applyUsernameTextFieldConstraints() {
domenicw's avatar
domenicw committed
163
        NSLayoutConstraint(item: self.usernameTextField, attribute: .top, relatedBy: .equal, toItem: self.infoLabel, attribute: .bottom, multiplier: 1, constant: 30).isActive = true
domenicw's avatar
domenicw committed
164
165
166
167
168
169
170
171
        NSLayoutConstraint(item: self.usernameTextField, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.usernameTextField, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
    }
    
    private func createPasswordTextField() -> UITextField {
        let field = UITextField()
        field.tintColor = .amivRed
        field.placeholder = "Password"
domenicw's avatar
domenicw committed
172
        field.returnKeyType = .go
domenicw's avatar
domenicw committed
173
        field.isSecureTextEntry = true
domenicw's avatar
domenicw committed
174
        field.borderStyle = .roundedRect
domenicw's avatar
domenicw committed
175
        field.addTarget(self, action: #selector(self.login), for: .primaryActionTriggered)
domenicw's avatar
domenicw committed
176
        field.translatesAutoresizingMaskIntoConstraints = false
domenicw's avatar
domenicw committed
177
178
179
180
181
        
        return field
    }
    
    private func applyPasswordTextFieldConstraints() {
domenicw's avatar
domenicw committed
182
        NSLayoutConstraint(item: self.passwordTextField, attribute: .top, relatedBy: .equal, toItem: self.usernameTextField, attribute: .bottom, multiplier: 1, constant: 20).isActive = true
domenicw's avatar
domenicw committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        NSLayoutConstraint(item: self.passwordTextField, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.passwordTextField, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
    }
    
    // MARK: - View Setup
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white
    }
    
    // MARK: - View Interaction
    
    @objc private func login() {
        self.hideKeyboard()
domenicw's avatar
domenicw committed
199
200
201
202
203
204
205
206
207
208
209
        
        if let username = self.usernameTextField.text, let password = self.passwordTextField.text {
            self.delegate?.login(username: username, password: password)
        } else {
            self.infoLabel.text = "Oops... This should not happen."
            self.infoLabel.textColor = .red
        }
    }
    
    @objc private func cancel() {
        self.delegate?.cancelLogin()
domenicw's avatar
domenicw committed
210
211
212
213
214
215
216
217
218
219
220
    }
    
    @objc private func fillPassword() {
        self.passwordTextField.becomeFirstResponder()
    }
    
    @objc private func hideKeyboard() {
        self.usernameTextField.resignFirstResponder()
        self.passwordTextField.resignFirstResponder()
    }
    
domenicw's avatar
domenicw committed
221
222
223
224
225
226
227
228
    public func loginFailed(with error: String) {
        self.infoLabel.text = error
        self.infoLabel.textColor = .red
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.infoLabel.textColor = .black
            self.infoLabel.text = self.infoLabelText
        }
domenicw's avatar
domenicw committed
229
230
231
    }
    
}