| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- //
- // LNLoginPanel.swift
- // Lanu
- //
- // Created by OneeChan on 2025/12/2.
- //
- import Foundation
- import UIKit
- import SnapKit
- import GoogleSignIn
- private weak var curLoginPanel: LNLoginPanel? = nil
- class LNLoginPanel: LNPopupView {
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- containerHeight = .percent(1.0)
-
- setupViews()
-
- LNEventDeliver.addObserver(self)
- }
-
- static func show(container: UIView?) {
- guard curLoginPanel == nil else { return }
-
- let panel = LNLoginPanel()
- panel.showIn(container)
- curLoginPanel = panel
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNLoginPanel: LNAccountManagerNotify {
- func onUserLogin() {
- dismiss()
- }
- }
- extension LNLoginPanel: ASAuthorizationControllerDelegate {
- func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
- guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential,
- let token = credential.identityToken,
- let tokenStr = String(data: token, encoding: .utf8)
- else {
- return
- }
-
- LNAccountManager.shared.loginByApple(data: tokenStr)
- }
-
- func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: any Error) {
- }
- }
- extension LNLoginPanel {
- private func setupViews() {
- container.backgroundColor = .text_5.withAlphaComponent(0.7)
-
- let logo = UIImageView()
- logo.image = .init(named: "ic_login_logo")
- container.addSubview(logo)
- logo.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.centerY.equalToSuperview().multipliedBy(0.5)
- }
-
- let privacy = buildPrivacy()
- container.addSubview(privacy)
- privacy.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.width.equalToSuperview().offset(-50)
- make.bottom.equalToSuperview().offset(-safeBottomInset - 40)
- }
-
- let stackView = UIStackView()
- stackView.axis = .vertical
- stackView.spacing = 16
- container.addSubview(stackView)
- stackView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(38)
- make.bottom.equalTo(privacy.snp.top).offset(-50)
- }
-
- #if DEBUG
- let email = buildEmail()
- stackView.addArrangedSubview(email)
- #endif
-
- let apple = buildAppleLogin()
- stackView.addArrangedSubview(apple)
-
- let google = buildGoogleLogin()
- stackView.addArrangedSubview(google)
-
- let tipsLabel = UILabel()
- tipsLabel.text = .init(key: "A00114")
- tipsLabel.font = .body_m
- tipsLabel.textColor = .primary_3
- tipsLabel.numberOfLines = 0
- tipsLabel.textAlignment = .center
- container.addSubview(tipsLabel)
- tipsLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(38)
- make.bottom.equalTo(stackView.snp.top).offset(-16)
- }
-
- let close = buildClose()
- container.addSubview(close)
- close.snp.makeConstraints { make in
- make.top.equalToSuperview().offset(UIView.statusBarHeight)
- make.trailing.equalToSuperview().offset(-20)
- }
- }
-
- private func buildAppleLogin() -> UIView {
- let button = UIButton()
- button.backgroundColor = .init(hex: "#0B0B0A")
- button.layer.cornerRadius = 24
- button.snp.makeConstraints { make in
- make.height.equalTo(48)
- }
-
- let ic = UIImageView()
- ic.image = .init(named: "ic_apple")
- button.addSubview(ic)
- ic.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalToSuperview().offset(12)
- }
-
- let title = UILabel()
- title.font = .heading_h3
- title.textColor = .text_1
- title.text = .init(key: "A00285")
- button.addSubview(title)
- title.snp.makeConstraints { make in
- make.center.equalToSuperview()
- }
-
- button.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- let provider = ASAuthorizationAppleIDProvider()
- let request = provider.createRequest()
- request.requestedScopes = [.fullName, .email]
- let controller = ASAuthorizationController(authorizationRequests: [request])
- controller.delegate = self
- // controller.presentationContextProvider = self
- controller.performRequests()
- }), for: .touchUpInside)
-
- return button
- }
-
- 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 = .init(named: "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 }
- guard let topVC = navigationController?.topViewController else { return }
- GIDSignIn.sharedInstance.signIn(withPresenting: topVC) { [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_2
- privacyView.linkTextAttributes = [.foregroundColor: UIColor.text_1]
-
- return privacyView
- }
-
- #if DEBUG
- private func buildEmail() -> UIView {
- let container = UIView()
- container.backgroundColor = .fill
- container.layer.cornerRadius = 24
- container.snp.makeConstraints { make in
- make.height.equalTo(48)
- }
-
- let input = UITextField()
- let login = UIButton()
- login.setContentHuggingPriority(.defaultHigh, for: .horizontal)
- login.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
- 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.equalToSuperview()
- make.trailing.equalToSuperview().offset(-16)
- }
-
- container.addSubview(input)
- input.snp.makeConstraints { make in
- make.verticalEdges.equalToSuperview()
- make.leading.equalToSuperview().offset(16)
- make.trailing.equalTo(login.snp.leading)
- }
-
- return container
- }
- #endif
-
- private func buildClose() -> UIView {
- let config = UIImage.SymbolConfiguration(pointSize: 24)
-
- let button = UIButton()
- button.setImage(.init(systemName: "xmark", withConfiguration: config), for: .normal)
- button.tintColor = .white
- button.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- self.dismiss()
- }), for: .touchUpInside)
-
- return button
- }
- }
- #if DEBUG
- import SwiftUI
- import AuthenticationServices
- struct LNLoginPanelPreview: UIViewRepresentable {
- func makeUIView(context: Context) -> some UIView {
- let container = UIView()
- container.backgroundColor = .lightGray
-
- let view = LNLoginPanel()
- view.showIn(container)
-
- return container
- }
-
- func updateUIView(_ uiView: UIViewType, context: Context) { }
- }
- #Preview(body: {
- LNLoginPanelPreview()
- })
- #endif
|