LNLoginViewController.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // LNLoginViewController.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/11.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import GoogleSignIn
  11. class LNLoginViewController: LNViewController {
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. setupViews()
  15. }
  16. }
  17. extension LNLoginViewController {
  18. private func setupViews() {
  19. showNavigationBar = false
  20. let bg = UIImageView()
  21. bg.image = .icLoginBg
  22. view.addSubview(bg)
  23. bg.snp.makeConstraints { make in
  24. make.edges.equalToSuperview()
  25. }
  26. #if DEBUG
  27. let email = buildEmail()
  28. view.addSubview(email)
  29. email.snp.makeConstraints { make in
  30. make.centerX.equalToSuperview()
  31. make.centerY.equalToSuperview().multipliedBy(1.3)
  32. }
  33. #endif
  34. let google = buildGoogleLogin()
  35. view.addSubview(google)
  36. google.snp.makeConstraints { make in
  37. make.horizontalEdges.equalToSuperview().inset(38)
  38. make.centerY.equalToSuperview().multipliedBy(1.5)
  39. }
  40. let privacy = buildPrivacy()
  41. view.addSubview(privacy)
  42. privacy.snp.makeConstraints { make in
  43. make.centerX.equalToSuperview()
  44. make.width.equalToSuperview().offset(-50)
  45. make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-40)
  46. }
  47. }
  48. private func buildGoogleLogin() -> UIView {
  49. let button = UIButton()
  50. button.backgroundColor = .fill
  51. button.layer.cornerRadius = 24
  52. button.snp.makeConstraints { make in
  53. make.height.equalTo(48)
  54. }
  55. let ic = UIImageView()
  56. ic.image = .google
  57. button.addSubview(ic)
  58. ic.snp.makeConstraints { make in
  59. make.centerY.equalToSuperview()
  60. make.leading.equalToSuperview().offset(22)
  61. make.width.height.equalTo(20)
  62. }
  63. let title = UILabel()
  64. title.font = .heading_h3
  65. title.textColor = .text_5
  66. title.text = .init(key: "A00115")
  67. button.addSubview(title)
  68. title.snp.makeConstraints { make in
  69. make.center.equalToSuperview()
  70. }
  71. button.addAction(UIAction(handler: { [weak self] _ in
  72. guard let self else { return }
  73. GIDSignIn.sharedInstance.signIn(withPresenting: self) { [weak self] result, err in
  74. guard self != nil else { return }
  75. guard err == nil, let result else { return }
  76. guard let token = result.user.idToken?.tokenString else { return }
  77. LNAccountManager.shared.loginByGoogle(data: token)
  78. }
  79. }), for: .touchUpInside)
  80. return button
  81. }
  82. private func buildPrivacy() -> UIView {
  83. let text: String = .init(key: "A00116")
  84. let attrStr = NSMutableAttributedString(string: text)
  85. let agreementRange = (text as NSString).range(of: .init(key: "A00117"))
  86. attrStr.addAttribute(.link, value: String.serviceUrl, range: agreementRange)
  87. let privacyRange = (text as NSString).range(of: .init(key: "A00118"))
  88. attrStr.addAttribute(.link, value: String.privacyUrl, range: privacyRange)
  89. let privacyView = LNPrivacyTextView()
  90. privacyView.attributedText = attrStr
  91. privacyView.textAlignment = .center
  92. privacyView.backgroundColor = .clear
  93. privacyView.font = .systemFont(ofSize: 13.4)
  94. privacyView.textColor = .text_4
  95. privacyView.linkTextAttributes = [.foregroundColor: UIColor.text_5]
  96. return privacyView
  97. }
  98. #if DEBUG
  99. private func buildEmail() -> UIView {
  100. let container = UIView()
  101. let input = UITextField()
  102. container.addSubview(input)
  103. input.snp.makeConstraints { make in
  104. make.leading.verticalEdges.equalToSuperview()
  105. make.width.equalTo(150)
  106. }
  107. let login = UIButton()
  108. login.setImage(.init(systemName: "arrow.forward"), for: .normal)
  109. login.addAction(UIAction(handler: { [weak self] _ in
  110. guard self != nil else { return }
  111. guard let text = input.text, !text.isEmpty else { return }
  112. LNAccountManager.shared.loginByEmail(email: text) { _ in }
  113. }), for: .touchUpInside)
  114. container.addSubview(login)
  115. login.snp.makeConstraints { make in
  116. make.centerY.trailing.equalToSuperview()
  117. make.leading.equalTo(input.snp.trailing).offset(5)
  118. }
  119. view.onTap { [weak self] in
  120. guard let self else { return }
  121. self.view.endEditing(true)
  122. }
  123. return container
  124. }
  125. #endif
  126. }
  127. #if DEBUG
  128. import SwiftUI
  129. struct LNLoginViewControllerPreview: UIViewControllerRepresentable {
  130. func makeUIViewController(context: Context) -> some UIViewController {
  131. LNNavigationController(rootViewController: LNLoginViewController())
  132. }
  133. func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
  134. }
  135. }
  136. #Preview(body: {
  137. LNLoginViewControllerPreview()
  138. })
  139. #endif