LNLoginPhoneInputViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // LNLoginPhoneInputViewController.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/1/15.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. extension UIView {
  11. func pushToLoginPhone() {
  12. let vc = LNLoginPhoneInputViewController()
  13. navigationController?.pushViewController(vc, animated: true)
  14. }
  15. }
  16. class LNLoginPhoneInputViewController: LNViewController {
  17. private let container = UIView()
  18. private let countryIcon = UIImageView()
  19. private let countryCodeLabel = UILabel()
  20. private let phoneInputView = LNTextField()
  21. private let confirmButton = UIButton()
  22. private var sections: [(code: String, list: [LNCountryCodeVO])] = []
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. loadCountryCodeList()
  26. setupViews()
  27. LNEventDeliver.addObserver(self)
  28. }
  29. }
  30. extension LNLoginPhoneInputViewController {
  31. private func loadCountryCodeList() {
  32. LNConfigManager.shared.getCountryCodeList { [weak self] list in
  33. guard let self else { return }
  34. guard let list else { return }
  35. sortList(list)
  36. }
  37. }
  38. private func sortList(_ list: [LNCountryCodeVO]) {
  39. sections.removeAll()
  40. var map: [String: [LNCountryCodeVO]] = [:]
  41. for item in list {
  42. let first = item.name.classificationFirstLetter
  43. var section = map[first] ?? []
  44. section.append(item)
  45. map[first] = section
  46. }
  47. let keys = map.keys.sorted()
  48. for key in keys {
  49. sections.append((key, map[key]) as! (code: String, list: [LNCountryCodeVO]))
  50. }
  51. var item: LNCountryCodeVO? = list.first {
  52. $0.code == LNAppConfig.shared.curLang.countryCode
  53. }
  54. if item == nil {
  55. item = list.first {
  56. $0.code == LNAppLanguage.indonesian.countryCode
  57. }
  58. }
  59. if let item {
  60. countryIcon.sd_setImage(with: URL(string: item.icon))
  61. countryCodeLabel.text = item.num
  62. }
  63. }
  64. }
  65. extension LNLoginPhoneInputViewController: LNAccountManagerNotify {
  66. func onLoginCaptchaCoolDownChanged(time: Int) {
  67. let text: String = if time == 0 {
  68. .init(key: "B00023")
  69. } else {
  70. .init(key: "B00023") + " (\(time)s)"
  71. }
  72. confirmButton.setTitle(text, for: .normal)
  73. checkConfirmButton()
  74. }
  75. }
  76. extension LNLoginPhoneInputViewController {
  77. private func checkConfirmButton() {
  78. let text = phoneInputView.text ?? ""
  79. confirmButton.isEnabled = !text.isEmpty && LNAccountManager.shared.canSendCaptcha
  80. }
  81. private func setupViews() {
  82. navigationBarColor = .clear
  83. let cover = UIImageView(image: .icLoginPhoneBg)
  84. view.addSubview(cover)
  85. cover.snp.makeConstraints { make in
  86. make.horizontalEdges.equalToSuperview()
  87. make.top.equalTo(fakeNaviBgView)
  88. }
  89. let paragraphStyle = NSMutableParagraphStyle()
  90. paragraphStyle.lineHeightMultiple = 0.8
  91. let titleLabel = UILabel()
  92. titleLabel.attributedText = .init(string: .init(key: "B00020"), attributes: [
  93. .paragraphStyle: paragraphStyle,
  94. ])
  95. titleLabel.font = .heading_h1
  96. titleLabel.textColor = .text_5
  97. titleLabel.numberOfLines = 0
  98. titleLabel.clipsToBounds = false
  99. titleLabel.layer.masksToBounds = false
  100. view.addSubview(titleLabel)
  101. titleLabel.snp.makeConstraints { make in
  102. make.horizontalEdges.equalToSuperview().inset(24)
  103. make.bottom.equalTo(cover.snp.bottom).offset(-42)
  104. }
  105. container.backgroundColor = .fill
  106. container.layer.cornerRadius = 20
  107. container.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
  108. view.addSubview(container)
  109. container.snp.makeConstraints { make in
  110. make.horizontalEdges.equalToSuperview()
  111. make.top.equalTo(titleLabel.snp.bottom).offset(12)
  112. make.bottom.equalToSuperview()
  113. }
  114. let phoneInput = buildPhoneInput()
  115. container.addSubview(phoneInput)
  116. phoneInput.snp.makeConstraints { make in
  117. make.horizontalEdges.equalToSuperview().inset(22)
  118. make.top.equalToSuperview().offset(26)
  119. }
  120. let confirm = buildConfirmButton()
  121. container.addSubview(confirm)
  122. confirm.snp.makeConstraints { make in
  123. make.horizontalEdges.equalToSuperview().inset(22)
  124. make.top.equalTo(phoneInput.snp.bottom).offset(24)
  125. }
  126. let loginMethods = buildOtherLoginMethod()
  127. container.addSubview(loginMethods)
  128. loginMethods.snp.makeConstraints { make in
  129. make.horizontalEdges.equalToSuperview()
  130. make.bottom.equalToSuperview().offset(view.commonBottomInset)
  131. }
  132. view.onTap { [weak self] in
  133. guard let self else { return }
  134. view.endEditing(true)
  135. }
  136. }
  137. private func buildPhoneInput() -> UIView {
  138. let container = UIView()
  139. container.backgroundColor = .fill_2
  140. container.layer.cornerRadius = 26
  141. container.snp.makeConstraints { make in
  142. make.height.equalTo(52)
  143. }
  144. let countryView = UIView()
  145. countryView.onTap { [weak self] in
  146. guard let self else { return }
  147. guard !sections.isEmpty else { return }
  148. let panel = LNCountrySelectPanel()
  149. panel.containerHeight = .height(self.container.bounds.height)
  150. panel.update(sections)
  151. panel.handler = { [weak self] item in
  152. guard let self else { return }
  153. countryIcon.sd_setImage(with: URL(string: item.icon))
  154. countryCodeLabel.text = item.num
  155. }
  156. panel.popup()
  157. }
  158. container.addSubview(countryView)
  159. countryView.snp.makeConstraints { make in
  160. make.verticalEdges.equalToSuperview()
  161. make.leading.equalToSuperview()
  162. }
  163. countryIcon.layer.cornerRadius = 12
  164. countryIcon.clipsToBounds = true
  165. countryView.addSubview(countryIcon)
  166. countryIcon.snp.makeConstraints { make in
  167. make.centerY.equalToSuperview()
  168. make.leading.equalToSuperview().offset(16)
  169. make.width.height.equalTo(24)
  170. }
  171. countryCodeLabel.font = .heading_h2
  172. countryCodeLabel.textColor = .text_5
  173. countryCodeLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  174. countryCodeLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
  175. countryView.addSubview(countryCodeLabel)
  176. countryCodeLabel.snp.makeConstraints { make in
  177. make.centerY.equalToSuperview()
  178. make.leading.equalTo(countryIcon.snp.trailing).offset(4)
  179. }
  180. let config = UIImage.SymbolConfiguration(pointSize: 10)
  181. let arrow = UIImageView()
  182. arrow.image = .init(systemName: "chevron.down", withConfiguration: config)?.withRenderingMode(.alwaysTemplate)
  183. arrow.tintColor = .text_5
  184. countryView.addSubview(arrow)
  185. arrow.snp.makeConstraints { make in
  186. make.centerY.equalToSuperview()
  187. make.leading.equalTo(countryCodeLabel.snp.trailing).offset(4)
  188. make.trailing.equalToSuperview()
  189. make.width.equalTo(12)
  190. }
  191. phoneInputView.font = .heading_h2
  192. phoneInputView.textColor = .text_5
  193. phoneInputView.attributedPlaceholder = .init(string: .init(key: "B00021"), attributes: [
  194. .font: UIFont.body_l,
  195. .foregroundColor: UIColor.text_2
  196. ])
  197. phoneInputView.clearButtonMode = .always
  198. phoneInputView.keyboardType = .numberPad
  199. phoneInputView.cursorWidth = 2
  200. phoneInputView.cursorHeight = 18
  201. phoneInputView.addAction(UIAction(handler: { [weak self] _ in
  202. guard let self else { return }
  203. checkConfirmButton()
  204. }), for: .editingChanged)
  205. container.addSubview(phoneInputView)
  206. phoneInputView.snp.makeConstraints { make in
  207. make.centerY.equalToSuperview()
  208. make.leading.equalTo(countryView.snp.trailing).offset(12)
  209. make.trailing.equalToSuperview().offset(-12)
  210. }
  211. return container
  212. }
  213. private func buildConfirmButton() -> UIView {
  214. confirmButton.setBackgroundImage(.primary_8, for: .normal)
  215. confirmButton.layer.cornerRadius = 23.5
  216. confirmButton.clipsToBounds = true
  217. confirmButton.setTitle(.init(key: "B00023"), for: .normal)
  218. confirmButton.setTitleColor(.text_1, for: .normal)
  219. confirmButton.titleLabel?.font = .heading_h3
  220. confirmButton.isEnabled = false
  221. confirmButton.addAction(UIAction(handler: { [weak self] _ in
  222. guard let self else { return }
  223. let code = countryCodeLabel.text
  224. let phone = phoneInputView.text
  225. guard let code, let phone else { return }
  226. showLoading()
  227. LNAccountManager.shared.getLoginCaptcha(code: code, phone: phone)
  228. { [weak self] success in
  229. dismissLoading()
  230. guard let self else { return }
  231. guard success else { return }
  232. view.pushToLoginCaptcha(code: code, phone: phone)
  233. }
  234. }), for: .touchUpInside)
  235. confirmButton.snp.makeConstraints { make in
  236. make.height.equalTo(47)
  237. }
  238. return confirmButton
  239. }
  240. private func buildOtherLoginMethod() -> UIView {
  241. let container = UIView()
  242. let titleLabel = UILabel()
  243. titleLabel.text = .init(key: "B00099")
  244. titleLabel.font = .body_m
  245. titleLabel.textColor = .text_3
  246. titleLabel.textAlignment = .center
  247. container.addSubview(titleLabel)
  248. titleLabel.snp.makeConstraints { make in
  249. make.horizontalEdges.equalToSuperview().inset(24)
  250. make.top.equalToSuperview()
  251. }
  252. let stackView = UIStackView()
  253. stackView.axis = .horizontal
  254. stackView.spacing = 62
  255. container.addSubview(stackView)
  256. stackView.snp.makeConstraints { make in
  257. make.centerX.equalToSuperview()
  258. make.leading.greaterThanOrEqualToSuperview()
  259. make.top.equalTo(titleLabel.snp.bottom).offset(20)
  260. make.bottom.equalToSuperview()
  261. }
  262. let google = buildLoginMethod(icon: .icGoogle)
  263. google.onTap { [weak self] in
  264. guard let self else { return }
  265. LNAccountManager.shared.doGoogleLogin(self)
  266. }
  267. stackView.addArrangedSubview(google)
  268. let apple = buildLoginMethod(icon: .icApple.withTintColor(.init(hex: "#0B0B0A"), renderingMode: .alwaysOriginal))
  269. apple.onTap { [weak self] in
  270. guard self != nil else { return }
  271. LNAccountManager.shared.doAppleLogin()
  272. }
  273. stackView.addArrangedSubview(apple)
  274. return container
  275. }
  276. private func buildLoginMethod(icon: UIImage) -> UIView {
  277. let container = UIView()
  278. container.backgroundColor = .fill_2
  279. container.layer.cornerRadius = 20
  280. container.snp.makeConstraints { make in
  281. make.width.height.equalTo(40)
  282. }
  283. let imageView = UIImageView()
  284. imageView.image = icon
  285. container.addSubview(imageView)
  286. imageView.snp.makeConstraints { make in
  287. make.center.equalToSuperview()
  288. }
  289. return container
  290. }
  291. }
  292. #if DEBUG
  293. import SwiftUI
  294. struct LNLoginPhoneInputViewControllerPreview: UIViewControllerRepresentable {
  295. func makeUIViewController(context: Context) -> some UIViewController {
  296. LNLoginPhoneInputViewController()
  297. }
  298. func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
  299. }
  300. }
  301. #Preview(body: {
  302. LNLoginPhoneInputViewControllerPreview()
  303. })
  304. #endif