LNMoneyNotEnoughAlertView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // LNMoneyNotEnoughAlertView.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/30.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNMoneyNotEnoughAlertView: LNCommonAlertView {
  11. private var exchangeType:LNCurrencyType?
  12. private let remainButton = UIButton()
  13. var exchangeHandler: (() -> Void)?
  14. func update(_ currency: LNCurrencyType, exchange: LNCurrencyType? = nil) {
  15. if let exchange, let exchangeType = LNExchangeType.map(from: exchange, to: currency) {
  16. titleLabel.text = .init(key: "B00132", currency.name(lowcase: true))
  17. messageLabel.font = .heading_h4
  18. messageLabel.textColor = .text_4
  19. messageLabel.attributedText = buildRemainString(exchangeType: exchange)
  20. if let rate = exchangeType.rate {
  21. subMessageLabel.attributedText = buildExchangeString(from: currency, fromValue: "1", exchange: exchange, exchangeValue: rate.toDisplay)
  22. }
  23. showConfirm(.init(key: "A00262")) { [weak self] in
  24. guard let self else { return }
  25. dismiss()
  26. if remainButton.isSelected {
  27. LNUserDefaults[.remainExchange] = curTime
  28. }
  29. exchangeHandler?()
  30. }
  31. showCancel()
  32. remainButton.setTitle(String(key: "B00134"), for: .normal)
  33. remainButton.setImage(.icCheck, for: .selected)
  34. remainButton.setImage(.icUncheck, for: .normal)
  35. remainButton.setTitleColor(.text_4, for: .normal)
  36. remainButton.titleLabel?.font = .body_s
  37. remainButton.imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 8)
  38. remainButton.addAction(UIAction(handler: { [weak remainButton] _ in
  39. guard let remainButton else { return }
  40. remainButton.isSelected.toggle()
  41. }), for: .touchUpInside)
  42. buttonViews.addArrangedSubview(remainButton)
  43. } else {
  44. titleLabel.text = .init(key: "B00131", currency.name(lowcase: true))
  45. showConfirm { [weak self] in
  46. guard let self else { return }
  47. dismiss()
  48. let panel = LNPurchasePanel()
  49. panel.update(currency)
  50. panel.popup(self)
  51. }
  52. showCancel()
  53. }
  54. }
  55. func buildRemainString(exchangeType: LNCurrencyType) -> NSMutableAttributedString {
  56. let icon = NSTextAttachment(image: exchangeType.icon)
  57. icon.bounds = .init(x: 0, y: -4, width: 19, height: 19)
  58. let text = String(key: "B00133", exchangeType.name(lowcase: true), exchangeType.currentValue.toDisplay)
  59. let attrString = NSMutableAttributedString(string: text)
  60. let iconRange = (text as NSString).range(of: "{icon}")
  61. attrString.replaceCharacters(in: iconRange, with: .init(attachment: icon))
  62. return attrString
  63. }
  64. func buildExchangeString(from: LNCurrencyType, fromValue: String,
  65. exchange: LNCurrencyType, exchangeValue: String) -> NSMutableAttributedString {
  66. let text = String(key: "B00138", fromValue, exchangeValue)
  67. let attrString = NSMutableAttributedString(string: text)
  68. let toIcon = NSTextAttachment(image: exchange.icon)
  69. toIcon.bounds = .init(x: 0, y: -3, width: 14, height: 14)
  70. let toIconRange = (text as NSString).range(of: "{icon2}")
  71. attrString.replaceCharacters(in: toIconRange, with: .init(attachment: toIcon))
  72. let fromIcon = NSTextAttachment(image: from.icon)
  73. fromIcon.bounds = .init(x: 0, y: -3, width: 14, height: 14)
  74. let fromIconRange = (text as NSString).range(of: "{icon1}")
  75. attrString.replaceCharacters(in: fromIconRange, with: .init(attachment: fromIcon))
  76. return attrString
  77. }
  78. }
  79. #if DEBUG
  80. import SwiftUI
  81. struct LNMoneyNotEnoughAlertViewPreview: UIViewRepresentable {
  82. func makeUIView(context: Context) -> some UIView {
  83. let container = UIView()
  84. container.backgroundColor = .lightGray
  85. let button = UIButton()
  86. button.backgroundColor = .red
  87. button.addAction(UIAction(handler: { [weak container] _ in
  88. guard let container else { return }
  89. let view = LNMoneyNotEnoughAlertView()
  90. view.update(.diamond, exchange: .coin)
  91. view.popup(container)
  92. }), for: .touchUpInside)
  93. container.addSubview(button)
  94. button.snp.makeConstraints { make in
  95. make.center.equalToSuperview()
  96. make.width.height.equalTo(50)
  97. }
  98. return container
  99. }
  100. func updateUIView(_ uiView: UIViewType, context: Context) { }
  101. }
  102. #Preview(body: {
  103. LNMoneyNotEnoughAlertViewPreview()
  104. })
  105. #endif