LNJoinUsInputCaptchaView.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // LNJoinUsInputCaptchaView.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/1/19.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. protocol LNJoinUsInputCaptchaViewDelegate: AnyObject {
  11. func joinUsInputCaptchaViewDidFinish(view: LNJoinUsInputCaptchaView)
  12. }
  13. class LNJoinUsInputCaptchaView: UIView {
  14. private var code: String?
  15. private var phone: String?
  16. private let descLabel = UILabel()
  17. private let captchaInput = LNCaptchaInputView()
  18. private let tipsView = LNAutoSizeTextView()
  19. weak var delegate: LNJoinUsInputCaptchaViewDelegate?
  20. override var isHidden: Bool {
  21. didSet {
  22. if !isHidden {
  23. captchaInput.startInput()
  24. }
  25. }
  26. }
  27. override init(frame: CGRect) {
  28. super.init(frame: frame)
  29. setupViews()
  30. LNEventDeliver.addObserver(self)
  31. }
  32. func update(_ code: String, phone: String) {
  33. self.code = code
  34. self.phone = phone
  35. descLabel.text = .init(key: "B00026") + ": \(code) \(phone)"
  36. }
  37. required init?(coder: NSCoder) {
  38. fatalError("init(coder:) has not been implemented")
  39. }
  40. }
  41. extension LNJoinUsInputCaptchaView: LNProfileManagerNotify {
  42. func onBindPhoneCaptchaCoolDownChanged(time: Int) {
  43. if time == 0 {
  44. let text = NSMutableAttributedString(string: .init(key: "B00028"), attributes: [
  45. .foregroundColor: UIColor.text_5
  46. ])
  47. let resend = NSMutableAttributedString(string: .init(key: "B00029"), attributes: [
  48. .font: UIFont.heading_h4,
  49. .link: "Gami:resend",
  50. ])
  51. text.append(.init(string: " "))
  52. text.append(resend)
  53. tipsView.attributedText = text
  54. } else {
  55. let text: String = .init(key: "B00027") + " (\(time)s)"
  56. tipsView.attributedText = .init(string: text, attributes: [
  57. .foregroundColor: UIColor.text_3
  58. ])
  59. }
  60. }
  61. }
  62. extension LNJoinUsInputCaptchaView: UITextViewDelegate {
  63. func textView(_ textView: UITextView, shouldInteractWith URL: URL,
  64. in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
  65. guard let code, let phone else { return false}
  66. showLoading()
  67. LNProfileManager.shared.getBindPhoneCaptcha(code: code, phone: phone)
  68. { [weak self] success in
  69. dismissLoading()
  70. guard self != nil else { return }
  71. guard success else { return }
  72. showToast(.init(key: "B00030"))
  73. }
  74. return false
  75. }
  76. func textViewDidChangeSelection(_ textView: UITextView) {
  77. textView.selectedTextRange = nil
  78. }
  79. }
  80. extension LNJoinUsInputCaptchaView: LNCaptchaInputViewDelegate {
  81. func onCaptchaInputChange(view: LNCaptchaInputView) {
  82. guard let code, let phone else { return }
  83. guard view.hasDone else { return }
  84. LNProfileManager.shared.bindPhone(
  85. code: code, phone: phone,
  86. captcha: view.curInput)
  87. { [weak self] success in
  88. guard let self else { return }
  89. guard success else { return }
  90. delegate?.joinUsInputCaptchaViewDidFinish(view: self)
  91. }
  92. }
  93. }
  94. extension LNJoinUsInputCaptchaView {
  95. private func setupViews() {
  96. onTap { [weak self] in
  97. guard let self else { return }
  98. endEditing(true)
  99. }
  100. let titleLabel = UILabel()
  101. titleLabel.font = .heading_h2
  102. titleLabel.textColor = .text_5
  103. titleLabel.text = .init(key: "B00035")
  104. titleLabel.textAlignment = .center
  105. addSubview(titleLabel)
  106. titleLabel.snp.makeConstraints { make in
  107. make.horizontalEdges.equalToSuperview().inset(16)
  108. make.top.equalToSuperview().offset(26)
  109. }
  110. descLabel.font = .body_m
  111. descLabel.textColor = .text_4
  112. descLabel.numberOfLines = 0
  113. addSubview(descLabel)
  114. descLabel.snp.makeConstraints { make in
  115. make.horizontalEdges.equalToSuperview().inset(22)
  116. make.top.equalTo(titleLabel.snp.bottom).offset(20)
  117. }
  118. captchaInput.delegate = self
  119. captchaInput.inputViews.forEach {
  120. $0.superview?.backgroundColor = .fill_2
  121. }
  122. addSubview(captchaInput)
  123. captchaInput.snp.makeConstraints { make in
  124. make.horizontalEdges.equalToSuperview().inset(22)
  125. make.top.equalTo(descLabel.snp.bottom).offset(20)
  126. make.height.equalTo(64)
  127. }
  128. let tipsView = buildCoolDown()
  129. addSubview(tipsView)
  130. tipsView.snp.makeConstraints { make in
  131. make.horizontalEdges.equalToSuperview().inset(22 - 4)
  132. make.top.equalTo(captchaInput.snp.bottom).offset(20 - 7) // 7 是 UITextView 自身的内容缩进
  133. }
  134. }
  135. private func buildCoolDown() -> UIView {
  136. tipsView.font = .body_m
  137. tipsView.isEditable = false
  138. tipsView.delegate = self
  139. tipsView.showsVerticalScrollIndicator = false
  140. tipsView.showsHorizontalScrollIndicator = false
  141. tipsView.backgroundColor = .clear
  142. tipsView.linkTextAttributes = [.foregroundColor: UIColor.text_6]
  143. return tipsView
  144. }
  145. }