LNLoginPanel.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // LNLoginPanel.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/2.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. private weak var curLoginPanel: LNLoginPanel? = nil
  11. class LNLoginPanel: LNPopupView {
  12. override init(frame: CGRect) {
  13. super.init(frame: frame)
  14. containerHeight = .percent(1.0)
  15. setupViews()
  16. LNEventDeliver.addObserver(self)
  17. }
  18. static func show(container: UIView?) {
  19. guard curLoginPanel == nil else { return }
  20. let panel = LNLoginPanel()
  21. panel.popup(container)
  22. curLoginPanel = panel
  23. }
  24. required init?(coder: NSCoder) {
  25. fatalError("init(coder:) has not been implemented")
  26. }
  27. }
  28. extension LNLoginPanel: LNAccountManagerNotify {
  29. func onUserLogin() {
  30. dismiss()
  31. }
  32. }
  33. extension LNLoginPanel {
  34. private func setupViews() {
  35. container.backgroundColor = .text_5.withAlphaComponent(0.7)
  36. let logo = UIImageView()
  37. logo.image = .icLoginLogo
  38. container.addSubview(logo)
  39. logo.snp.makeConstraints { make in
  40. make.centerX.equalToSuperview()
  41. make.centerY.equalToSuperview().multipliedBy(0.5)
  42. }
  43. let privacy = buildPrivacy()
  44. container.addSubview(privacy)
  45. privacy.snp.makeConstraints { make in
  46. make.centerX.equalToSuperview()
  47. make.width.equalToSuperview().offset(-50)
  48. make.bottom.equalToSuperview().offset(-safeBottomInset - 40)
  49. }
  50. let stackView = UIStackView()
  51. stackView.axis = .vertical
  52. stackView.spacing = 16
  53. container.addSubview(stackView)
  54. stackView.snp.makeConstraints { make in
  55. make.horizontalEdges.equalToSuperview().inset(38)
  56. make.bottom.equalTo(privacy.snp.top).offset(-50)
  57. }
  58. #if DEBUG
  59. let email = buildEmail()
  60. stackView.addArrangedSubview(email)
  61. #endif
  62. let phone = buildLoginItem(icon: .icLoginPhone, title: .init(key: "B00019"), textColor: .text_1, color: .primary_3)
  63. phone.addAction(UIAction(handler: { [weak self] _ in
  64. guard let self else { return }
  65. pushToLoginPhone()
  66. }), for: .touchUpInside)
  67. stackView.addArrangedSubview(phone)
  68. let apple = buildLoginItem(icon: .icApple, title: .init(key: "A00285"), textColor: .text_1, color: .init(hex: "#0B0B0A"))
  69. apple.addAction(UIAction(handler: { [weak self] _ in
  70. guard self != nil else { return }
  71. LNAccountManager.shared.doAppleLogin()
  72. }), for: .touchUpInside)
  73. stackView.addArrangedSubview(apple)
  74. let google = buildLoginItem(icon: .icGoogle, title: .init(key: "A00115"), textColor: .text_5, color: .fill)
  75. google.addAction(UIAction(handler: { [weak self] _ in
  76. guard let self else { return }
  77. guard let topVC = navigationController?.topViewController else { return }
  78. LNAccountManager.shared.doGoogleLogin(topVC)
  79. }), for: .touchUpInside)
  80. stackView.addArrangedSubview(google)
  81. let tipsLabel = UILabel()
  82. tipsLabel.text = .init(key: "A00114")
  83. tipsLabel.font = .body_m
  84. tipsLabel.textColor = .primary_3
  85. tipsLabel.numberOfLines = 0
  86. tipsLabel.textAlignment = .center
  87. container.addSubview(tipsLabel)
  88. tipsLabel.snp.makeConstraints { make in
  89. make.horizontalEdges.equalToSuperview().inset(38)
  90. make.bottom.equalTo(stackView.snp.top).offset(-16)
  91. }
  92. let close = buildClose()
  93. container.addSubview(close)
  94. close.snp.makeConstraints { make in
  95. make.top.equalToSuperview().offset(UIView.statusBarHeight)
  96. make.trailing.equalToSuperview().offset(-20)
  97. }
  98. }
  99. private func buildLoginItem(icon: UIImage, title: String, textColor: UIColor, color: UIColor) -> UIButton {
  100. let button = UIButton()
  101. button.setBackgroundImage(UIImage.image(for: color), for: .normal)
  102. button.layer.cornerRadius = 24
  103. button.clipsToBounds = true
  104. button.snp.makeConstraints { make in
  105. make.height.equalTo(48)
  106. }
  107. let ic = UIImageView()
  108. ic.image = icon
  109. button.addSubview(ic)
  110. ic.snp.makeConstraints { make in
  111. make.centerY.equalToSuperview()
  112. make.leading.equalToSuperview().offset(12)
  113. }
  114. let titleLabel = UILabel()
  115. titleLabel.font = .heading_h3
  116. titleLabel.textColor = textColor
  117. titleLabel.text = title
  118. button.addSubview(titleLabel)
  119. titleLabel.snp.makeConstraints { make in
  120. make.center.equalToSuperview()
  121. }
  122. return button
  123. }
  124. private func buildPrivacy() -> UIView {
  125. let text: String = .init(key: "A00116")
  126. let attrStr = NSMutableAttributedString(string: text)
  127. let agreementRange = (text.lowercased() as NSString).range(of: .init(key: "A00117").lowercased())
  128. attrStr.addAttributes([
  129. .link: String.serviceUrl,
  130. .underlineStyle: NSUnderlineStyle.single.rawValue,
  131. .underlineColor: UIColor.text_2
  132. ], range: agreementRange)
  133. let privacyRange = (text.lowercased() as NSString).range(of: .init(key: "A00118").lowercased())
  134. attrStr.addAttributes([
  135. .link: String.privacyUrl,
  136. .underlineStyle: NSUnderlineStyle.single.rawValue,
  137. .underlineColor: UIColor.text_2,
  138. ], range: privacyRange)
  139. let privacyView = LNPrivacyTextView()
  140. privacyView.attributedText = attrStr
  141. privacyView.textAlignment = .center
  142. privacyView.backgroundColor = .clear
  143. privacyView.font = .systemFont(ofSize: 13.4)
  144. privacyView.textColor = .text_2
  145. privacyView.linkTextAttributes = [.foregroundColor: UIColor.text_1]
  146. return privacyView
  147. }
  148. #if DEBUG
  149. private func buildEmail() -> UIView {
  150. let container = UIView()
  151. container.backgroundColor = .fill
  152. container.layer.cornerRadius = 24
  153. container.snp.makeConstraints { make in
  154. make.height.equalTo(48)
  155. }
  156. let input = UITextField()
  157. let login = UIButton()
  158. login.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  159. login.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
  160. login.setImage(.init(systemName: "arrow.forward"), for: .normal)
  161. login.addAction(UIAction(handler: { [weak self] _ in
  162. guard self != nil else { return }
  163. guard let text = input.text, !text.isEmpty else { return }
  164. LNAccountManager.shared.loginByEmail(email: text) { _ in }
  165. }), for: .touchUpInside)
  166. container.addSubview(login)
  167. login.snp.makeConstraints { make in
  168. make.centerY.equalToSuperview()
  169. make.trailing.equalToSuperview().offset(-16)
  170. }
  171. container.addSubview(input)
  172. input.snp.makeConstraints { make in
  173. make.verticalEdges.equalToSuperview()
  174. make.leading.equalToSuperview().offset(16)
  175. make.trailing.equalTo(login.snp.leading)
  176. }
  177. return container
  178. }
  179. #endif
  180. private func buildClose() -> UIView {
  181. let config = UIImage.SymbolConfiguration(pointSize: 24)
  182. let button = UIButton()
  183. button.setImage(.init(systemName: "xmark", withConfiguration: config), for: .normal)
  184. button.tintColor = .white
  185. button.addAction(UIAction(handler: { [weak self] _ in
  186. guard let self else { return }
  187. self.dismiss()
  188. }), for: .touchUpInside)
  189. return button
  190. }
  191. }
  192. #if DEBUG
  193. import SwiftUI
  194. import AuthenticationServices
  195. struct LNLoginPanelPreview: UIViewRepresentable {
  196. func makeUIView(context: Context) -> some UIView {
  197. let container = UIView()
  198. container.backgroundColor = .lightGray
  199. let view = LNLoginPanel()
  200. view.popup(container)
  201. return container
  202. }
  203. func updateUIView(_ uiView: UIViewType, context: Context) { }
  204. }
  205. #Preview(body: {
  206. LNLoginPanelPreview()
  207. })
  208. #endif