| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- //
- // LNJoinUsInputCaptchaView.swift
- // Gami
- //
- // Created by OneeChan on 2026/1/19.
- //
- import Foundation
- import UIKit
- import SnapKit
- protocol LNJoinUsInputCaptchaViewDelegate: AnyObject {
- func joinUsInputCaptchaViewDidFinish(view: LNJoinUsInputCaptchaView)
- }
- class LNJoinUsInputCaptchaView: UIView {
- private var code: String?
- private var phone: String?
-
- private let descLabel = UILabel()
-
- private let captchaInput = LNCaptchaInputView()
- private let tipsView = LNAutoSizeTextView()
-
- weak var delegate: LNJoinUsInputCaptchaViewDelegate?
-
- override var isHidden: Bool {
- didSet {
- if !isHidden {
- captchaInput.startInput()
- }
- }
- }
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
-
- LNEventDeliver.addObserver(self)
- }
-
- func update(_ code: String, phone: String) {
- self.code = code
- self.phone = phone
-
- descLabel.text = .init(key: "B00026") + ": \(code) \(phone)"
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNJoinUsInputCaptchaView: LNProfileManagerNotify {
- func onBindPhoneCaptchaCoolDownChanged(time: Int) {
- if time == 0 {
- let text = NSMutableAttributedString(string: .init(key: "B00028"), attributes: [
- .foregroundColor: UIColor.text_5
- ])
- let resend = NSMutableAttributedString(string: .init(key: "B00029"), attributes: [
- .font: UIFont.heading_h4,
- .link: "Gami:resend",
- ])
- text.append(.init(string: " "))
- text.append(resend)
- tipsView.attributedText = text
- } else {
- let text: String = .init(key: "B00027") + " (\(time)s)"
- tipsView.attributedText = .init(string: text, attributes: [
- .foregroundColor: UIColor.text_3
- ])
- }
- }
- }
- extension LNJoinUsInputCaptchaView: UITextViewDelegate {
- func textView(_ textView: UITextView, shouldInteractWith URL: URL,
- in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
- guard let code, let phone else { return false}
- showLoading()
- LNProfileManager.shared.getBindPhoneCaptcha(code: code, phone: phone)
- { [weak self] success in
- dismissLoading()
- guard self != nil else { return }
- guard success else { return }
- showToast(.init(key: "B00030"))
- }
-
- return false
- }
-
- func textViewDidChangeSelection(_ textView: UITextView) {
- textView.selectedTextRange = nil
- }
- }
- extension LNJoinUsInputCaptchaView: LNCaptchaInputViewDelegate {
- func onCaptchaInputChange(view: LNCaptchaInputView) {
- guard let code, let phone else { return }
- guard view.hasDone else { return }
-
- LNProfileManager.shared.bindPhone(
- code: code, phone: phone,
- captcha: view.curInput)
- { [weak self] success in
- guard let self else { return }
- guard success else { return }
- delegate?.joinUsInputCaptchaViewDidFinish(view: self)
- }
- }
- }
- extension LNJoinUsInputCaptchaView {
- private func setupViews() {
- onTap { [weak self] in
- guard let self else { return }
- endEditing(true)
- }
-
- let titleLabel = UILabel()
- titleLabel.font = .heading_h2
- titleLabel.textColor = .text_5
- titleLabel.text = .init(key: "B00035")
- titleLabel.textAlignment = .center
- addSubview(titleLabel)
- titleLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.top.equalToSuperview().offset(26)
- }
-
- descLabel.font = .body_m
- descLabel.textColor = .text_4
- descLabel.numberOfLines = 0
- addSubview(descLabel)
- descLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(22)
- make.top.equalTo(titleLabel.snp.bottom).offset(20)
- }
-
- captchaInput.delegate = self
- captchaInput.inputViews.forEach {
- $0.superview?.backgroundColor = .fill_2
- }
- addSubview(captchaInput)
- captchaInput.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(22)
- make.top.equalTo(descLabel.snp.bottom).offset(20)
- make.height.equalTo(64)
- }
-
- let tipsView = buildCoolDown()
- addSubview(tipsView)
- tipsView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(22 - 4)
- make.top.equalTo(captchaInput.snp.bottom).offset(20 - 7) // 7 是 UITextView 自身的内容缩进
- }
- }
-
- private func buildCoolDown() -> UIView {
- tipsView.font = .body_m
- tipsView.isEditable = false
- tipsView.delegate = self
- tipsView.showsVerticalScrollIndicator = false
- tipsView.showsHorizontalScrollIndicator = false
- tipsView.backgroundColor = .clear
- tipsView.linkTextAttributes = [.foregroundColor: UIColor.text_6]
-
- return tipsView
- }
- }
|