ReaderHive/ReaderHive/Class/Store/V/NRCoinsPackConfirmView.swift
澜声世纪 211bf5f3df 1
2025-12-12 15:01:30 +08:00

203 lines
6.4 KiB
Swift

//
// NRCoinsPackConfirmView.swift
// ReaderHive
//
// Created by on 2025/12/12.
//
import UIKit
import SnapKit
import HWPanModal
internal import StoreKit
class NRCoinsPackConfirmView: NRPanModalContentView {
var buyFinishHandle: (() -> Void)?
var shortPlayId: String?
var videoId: String?
var model: NRPayItem? {
didSet {
tipView.list = model?.ext_info?.sub_coins_txt_list
var price = model?.price ?? ""
if model?.discount_type == 1, let introductoryPrice = model?.introductionaryOffer {
price = introductoryPrice.price.stringValue
} else if model?.discount_type == 2, let discount = model?.promotionalOffers?.first {
price = discount.price.stringValue
}
priceLabel.text = "\(model?.currency ?? "")\(price)/\(model?.getTimeString() ?? "")"
}
}
private lazy var bgView: UIImageView = {
let imageView = UIImageView(image: UIImage(named: "bg_image_01"))
return imageView
}()
private lazy var titleLabel: NRLabel = {
let label = NRLabel()
label.font = .font(ofSize: 24, weight: .bold)
label.textColorImage = UIImage(named: "gradient_color_01")
label.text = "What You Get".localized
return label
}()
private lazy var closeButton: UIButton = {
let button = UIButton(type: .custom, primaryAction: UIAction(handler: { [weak self] _ in
guard let self = self else { return }
Task {
await self.dismiss(animated: true)
}
}))
button.setImage(UIImage(named: "close_icon_02"), for: .normal)
return button
}()
private lazy var scrollView: NRScrollView = {
let scrollView = NRScrollView()
scrollView.addObserver(self, forKeyPath: "contentSize", context: nil)
return scrollView
}()
private lazy var titleView: NRCoinsPackConfirmTitleView = {
let view = NRCoinsPackConfirmTitleView()
return view
}()
private lazy var tipView: NRCoinsPackConfirmTipView = {
let view = NRCoinsPackConfirmTipView()
return view
}()
private lazy var priceLabel: UILabel = {
let label = NRLabel()
label.font = .font(ofSize: 18, weight: .bold)
label.textColorImage = UIImage(named: "gradient_color_01")
return label
}()
private lazy var buyButton: UIButton = {
var configuration = UIButton.Configuration.plain()
configuration.background.image = UIImage(named: "gradient_color_01")
configuration.background.cornerRadius = 24
configuration.attributedTitle = AttributedString("Continue".localized, attributes: AttributeContainer([
.font : UIFont.font(ofSize: 14, weight: .bold),
.foregroundColor : UIColor.white
]))
let button = UIButton(configuration: configuration, primaryAction: UIAction(handler: { [weak self] _ in
guard let self = self else { return }
self.buyCoinsPack()
}))
return button
}()
@MainActor deinit {
scrollView.removeObserver(self, forKeyPath: "contentSize")
}
override init(frame: CGRect) {
super.init(frame: frame)
nr_setupUI()
}
@MainActor required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "contentSize" {
self.panModalSetNeedsLayoutUpdate()
}
}
override func allowsTapBackgroundToDismiss() -> Bool {
return false
}
override func longFormHeight() -> PanModalHeight {
let height = 88 + scrollView.contentSize.height + 92 + 10
return PanModalHeightMake(.content, height + UIScreen.safeBottom)
}
private func buyCoinsPack() {
guard let model = self.model else { return }
NRIapManager.manager.start(model: model, shortPlayId: self.shortPlayId, videoId: self.videoId) { [weak self] finish in
guard let self = self else { return }
if finish {
self.buyFinishHandle?()
Task {
await self.dismiss(animated: true)
}
}
}
}
}
extension NRCoinsPackConfirmView {
private func nr_setupUI() {
addSubview(bgView)
addSubview(titleLabel)
addSubview(closeButton)
addSubview(scrollView)
scrollView.addSubview(titleView)
scrollView.addSubview(tipView)
addSubview(buyButton)
addSubview(priceLabel)
bgView.snp.makeConstraints { make in
make.left.right.top.equalToSuperview()
}
titleLabel.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalToSuperview().offset(52)
}
closeButton.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-6)
make.top.equalToSuperview().offset(6)
make.width.height.equalTo(44)
}
scrollView.snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.top.equalToSuperview().offset(88)
make.bottom.equalToSuperview().offset(-(92 + UIScreen.safeBottom + 10))
}
titleView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(16)
make.centerX.equalToSuperview()
make.top.equalToSuperview()
}
tipView.snp.makeConstraints { make in
make.left.right.equalTo(titleView)
make.top.equalTo(titleView.snp.bottom).offset(12)
make.bottom.equalToSuperview()
}
buyButton.snp.makeConstraints { make in
make.left.equalToSuperview().offset(16)
make.centerX.equalToSuperview()
make.top.equalTo(scrollView.snp.bottom).offset(44)
make.height.equalTo(48)
}
priceLabel.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.centerY.equalTo(scrollView.snp.bottom).offset(22)
}
}
}