| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- //
- // LNLoginViewController.swift
- // Lanu
- //
- // Created by OneeChan on 2025/11/11.
- //
- import Foundation
- import UIKit
- import SnapKit
- import GoogleSignIn
- class LNLoginViewController: LNViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
-
- setupViews()
- }
- }
- extension LNLoginViewController {
- private func setupViews() {
- showNavigationBar = false
-
- let bg = UIImageView()
- bg.image = .icLoginBg
- view.addSubview(bg)
- bg.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
-
- #if DEBUG
- let email = buildEmail()
- view.addSubview(email)
- email.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.centerY.equalToSuperview().multipliedBy(1.3)
- }
- #endif
-
- let google = buildGoogleLogin()
- view.addSubview(google)
- google.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(38)
- make.centerY.equalToSuperview().multipliedBy(1.5)
- }
-
- let privacy = buildPrivacy()
- view.addSubview(privacy)
- privacy.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.width.equalToSuperview().offset(-50)
- make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-40)
- }
- }
-
- private func buildGoogleLogin() -> UIView {
- let button = UIButton()
- button.backgroundColor = .fill
- button.layer.cornerRadius = 24
- button.snp.makeConstraints { make in
- make.height.equalTo(48)
- }
-
- let ic = UIImageView()
- ic.image = .google
- button.addSubview(ic)
- ic.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalToSuperview().offset(22)
- make.width.height.equalTo(20)
- }
-
- let title = UILabel()
- title.font = .heading_h3
- title.textColor = .text_5
- title.text = .init(key: "A00115")
- button.addSubview(title)
- title.snp.makeConstraints { make in
- make.center.equalToSuperview()
- }
-
- button.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- GIDSignIn.sharedInstance.signIn(withPresenting: self) { [weak self] result, err in
- guard self != nil else { return }
- guard err == nil, let result else { return }
- guard let token = result.user.idToken?.tokenString else { return }
- LNAccountManager.shared.loginByGoogle(data: token)
- }
- }), for: .touchUpInside)
-
- return button
- }
-
- private func buildPrivacy() -> UIView {
- let text: String = .init(key: "A00116")
- let attrStr = NSMutableAttributedString(string: text)
- let agreementRange = (text as NSString).range(of: .init(key: "A00117"))
- attrStr.addAttribute(.link, value: String.serviceUrl, range: agreementRange)
- let privacyRange = (text as NSString).range(of: .init(key: "A00118"))
- attrStr.addAttribute(.link, value: String.privacyUrl, range: privacyRange)
-
- let privacyView = LNPrivacyTextView()
- privacyView.attributedText = attrStr
- privacyView.textAlignment = .center
- privacyView.backgroundColor = .clear
- privacyView.font = .systemFont(ofSize: 13.4)
- privacyView.textColor = .text_4
- privacyView.linkTextAttributes = [.foregroundColor: UIColor.text_5]
-
- return privacyView
- }
-
- #if DEBUG
- private func buildEmail() -> UIView {
- let container = UIView()
-
- let input = UITextField()
- container.addSubview(input)
- input.snp.makeConstraints { make in
- make.leading.verticalEdges.equalToSuperview()
- make.width.equalTo(150)
- }
-
- let login = UIButton()
- login.setImage(.init(systemName: "arrow.forward"), for: .normal)
- login.addAction(UIAction(handler: { [weak self] _ in
- guard self != nil else { return }
- guard let text = input.text, !text.isEmpty else { return }
- LNAccountManager.shared.loginByEmail(email: text) { _ in }
- }), for: .touchUpInside)
- container.addSubview(login)
- login.snp.makeConstraints { make in
- make.centerY.trailing.equalToSuperview()
- make.leading.equalTo(input.snp.trailing).offset(5)
- }
-
- view.onTap { [weak self] in
- guard let self else { return }
- self.view.endEditing(true)
- }
-
- return container
- }
- #endif
- }
- #if DEBUG
- import SwiftUI
- struct LNLoginViewControllerPreview: UIViewControllerRepresentable {
- func makeUIViewController(context: Context) -> some UIViewController {
- LNNavigationController(rootViewController: LNLoginViewController())
- }
-
- func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
-
- }
- }
- #Preview(body: {
- LNLoginViewControllerPreview()
- })
- #endif
|