LoginView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import UIKit
  15. /// Login View presented when performing Email & Password Login Flow
  16. class LoginView: UIView {
  17. var emailTextField: UITextField! {
  18. didSet {
  19. emailTextField.textContentType = .emailAddress
  20. }
  21. }
  22. var passwordTextField: UITextField! {
  23. didSet {
  24. passwordTextField.textContentType = .password
  25. }
  26. }
  27. var emailTopConstraint: NSLayoutConstraint!
  28. var passwordTopConstraint: NSLayoutConstraint!
  29. lazy var loginButton: UIButton = {
  30. let button = UIButton()
  31. button.setTitle("Login", for: .normal)
  32. button.setTitleColor(.white, for: .normal)
  33. button.setTitleColor(.highlightedLabel, for: .highlighted)
  34. button.setBackgroundImage(UIColor.systemOrange.image, for: .normal)
  35. button.setBackgroundImage(UIColor.systemOrange.highlighted.image, for: .highlighted)
  36. button.clipsToBounds = true
  37. button.layer.cornerRadius = 14
  38. return button
  39. }()
  40. lazy var createAccountButton: UIButton = {
  41. let button = UIButton()
  42. button.setTitle("Create Account", for: .normal)
  43. button.setTitleColor(.secondaryLabel, for: .normal)
  44. button.setTitleColor(UIColor.secondaryLabel.highlighted, for: .highlighted)
  45. return button
  46. }()
  47. convenience init() {
  48. self.init(frame: .zero)
  49. setupSubviews()
  50. }
  51. // MARK: - Subviews Setup
  52. private func setupSubviews() {
  53. backgroundColor = .systemBackground
  54. clipsToBounds = true
  55. setupFirebaseLogoImage()
  56. setupEmailTextfield()
  57. setupPasswordTextField()
  58. setupLoginButton()
  59. setupCreateAccountButton()
  60. }
  61. private func setupFirebaseLogoImage() {
  62. let firebaseLogo = UIImage(named: "firebaseLogo")
  63. let imageView = UIImageView(image: firebaseLogo)
  64. imageView.contentMode = .scaleAspectFit
  65. addSubview(imageView)
  66. imageView.translatesAutoresizingMaskIntoConstraints = false
  67. NSLayoutConstraint.activate([
  68. imageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: -55),
  69. imageView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 55),
  70. imageView.widthAnchor.constraint(equalToConstant: 325),
  71. imageView.heightAnchor.constraint(equalToConstant: 325),
  72. ])
  73. }
  74. private func setupEmailTextfield() {
  75. emailTextField = textField(placeholder: "Email", symbolName: "person.crop.circle")
  76. emailTextField.translatesAutoresizingMaskIntoConstraints = false
  77. addSubview(emailTextField)
  78. NSLayoutConstraint.activate([
  79. emailTextField.leadingAnchor.constraint(
  80. equalTo: safeAreaLayoutGuide.leadingAnchor,
  81. constant: 15
  82. ),
  83. emailTextField.trailingAnchor.constraint(
  84. equalTo: safeAreaLayoutGuide.trailingAnchor,
  85. constant: -15
  86. ),
  87. emailTextField.heightAnchor.constraint(equalToConstant: 45),
  88. ])
  89. let constant: CGFloat = UIDevice.current.orientation.isLandscape ? 15 : 50
  90. emailTopConstraint = emailTextField.topAnchor.constraint(
  91. equalTo: safeAreaLayoutGuide.topAnchor,
  92. constant: constant
  93. )
  94. emailTopConstraint.isActive = true
  95. }
  96. private func setupPasswordTextField() {
  97. passwordTextField = textField(placeholder: "Password", symbolName: "lock.fill")
  98. passwordTextField.translatesAutoresizingMaskIntoConstraints = false
  99. addSubview(passwordTextField)
  100. NSLayoutConstraint.activate([
  101. passwordTextField.leadingAnchor.constraint(
  102. equalTo: safeAreaLayoutGuide.leadingAnchor,
  103. constant: 15
  104. ),
  105. passwordTextField.trailingAnchor.constraint(
  106. equalTo: safeAreaLayoutGuide.trailingAnchor,
  107. constant: -15
  108. ),
  109. passwordTextField.heightAnchor.constraint(equalToConstant: 45),
  110. ])
  111. let constant: CGFloat = UIDevice.current.orientation.isLandscape ? 5 : 20
  112. passwordTopConstraint =
  113. passwordTextField.topAnchor.constraint(
  114. equalTo: emailTextField.bottomAnchor,
  115. constant: constant
  116. )
  117. passwordTopConstraint.isActive = true
  118. }
  119. private func setupLoginButton() {
  120. addSubview(loginButton)
  121. loginButton.translatesAutoresizingMaskIntoConstraints = false
  122. NSLayoutConstraint.activate([
  123. loginButton.leadingAnchor.constraint(
  124. equalTo: safeAreaLayoutGuide.leadingAnchor,
  125. constant: 15
  126. ),
  127. loginButton.trailingAnchor.constraint(
  128. equalTo: safeAreaLayoutGuide.trailingAnchor,
  129. constant: -15
  130. ),
  131. loginButton.heightAnchor.constraint(equalToConstant: 45),
  132. loginButton.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 5),
  133. ])
  134. }
  135. private func setupCreateAccountButton() {
  136. addSubview(createAccountButton)
  137. createAccountButton.translatesAutoresizingMaskIntoConstraints = false
  138. NSLayoutConstraint.activate([
  139. createAccountButton.centerXAnchor.constraint(equalTo: centerXAnchor),
  140. createAccountButton.topAnchor.constraint(equalTo: loginButton.bottomAnchor, constant: 5),
  141. ])
  142. }
  143. // MARK: - Private Helpers
  144. private func textField(placeholder: String, symbolName: String) -> UITextField {
  145. let textfield = UITextField()
  146. textfield.backgroundColor = .secondarySystemBackground
  147. textfield.layer.cornerRadius = 14
  148. textfield.placeholder = placeholder
  149. textfield.tintColor = .systemOrange
  150. let symbol = UIImage(systemName: symbolName)
  151. textfield.setImage(symbol)
  152. return textfield
  153. }
  154. }