123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- //
- // TSPurchaseVideoTimesVC.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/8/6.
- //
- import Combine
- import SwiftUI
- import SwiftUIX
- class TSPurchaseVideoTimesVM : ObservableObject{
-
- @Published var selectedType: PremiumPeriod = .purchase(.videoNum2)
-
- /// 订阅publisher
- let buyPublisher = PassthroughSubject<Bool,Never>()
-
- /// 订阅publisher
- let closePagePublisher = PassthroughSubject<Bool,Never>()
- }
- class TSPurchaseVideoTimesVC: TSBaseVC {
- var isShowAlertModel:Bool//是否以弹窗的样式展现
- init(isShowAlertModel: Bool) {
- self.isShowAlertModel = isShowAlertModel
- super.init()
- }
-
- @MainActor required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- var cancellabel: [AnyCancellable] = []
- var viewModel: TSPurchaseVideoTimesVM = .init()
- var buyPeriod:PremiumPeriod = .year
- var isHandlePurchaseStateChanged = false //是否处理购买状态变化
-
-
- lazy var fullVC: UIHostingController<TSPurchaseVideoTimesView> = {
- let vc = UIHostingController(rootView: TSPurchaseVideoTimesView(viewModel: viewModel))
- vc.view.backgroundColor = .clear
- return vc
- }()
-
- lazy var alertVC: UIHostingController<TSPurchaseVideoTimesAlertView> = {
- let vc = UIHostingController(rootView: TSPurchaseVideoTimesAlertView(viewModel: viewModel))
- vc.view.backgroundColor = .clear
- return vc
- }()
-
-
-
- override func createView() {
- if isShowAlertModel {
- setUpAlerView()
- }else{
- setUpFullView()
- }
- }
-
- override func dealThings() {
- addNotifaction()
- onPurchaseStateChanged()
- NotificationCenter.default.addObserver(forName: .kPurchasePrepared, object: nil, queue: OperationQueue.main) { [weak self] _ in
- guard let self = self else { return }
- viewModel.selectedType = viewModel.selectedType
- }
- }
-
- func addNotifaction() {
- viewModel.buyPublisher.receive(on: DispatchQueue.main).sink { [weak self] _ in
- guard let self = self else { return }
- isHandlePurchaseStateChanged = true
- PurchaseManager.default.pay(for: self.viewModel.selectedType)
- }.store(in: &cancellabel)
-
- viewModel.closePagePublisher.receive(on: DispatchQueue.main).sink { [weak self] _ in
- guard let self = self else { return }
- closePage()
- }.store(in: &cancellabel)
- }
-
- }
- extension TSPurchaseVideoTimesVC {
-
- func setUpFullView(){
- addNormalNavBarView()
- setPageTitle("")
-
- contentView.snp.updateConstraints { make in
- make.top.equalTo(0)
- }
-
- let bgColorView = UIView(frame: UIScreen.main.bounds)
- bgColorView.addGradientBg(colors: ["#13052C".uiCGColor,"#160B2C".uiCGColor],startPoint: CGPoint(x: 0.5, y: 0),endPoint: CGPoint(x: 0.5, y: 1.0))
- contentView.addSubview(bgColorView)
-
-
- contentView.addSubview(fullVC.view)
- fullVC.view.snp.makeConstraints { make in
- make.leading.trailing.bottom.top.equalToSuperview()
- }
- }
-
- }
- extension TSPurchaseVideoTimesVC {
-
- func setUpAlerView(){
- setNavBarViewHidden(true)
- self.view.backgroundColor = .black.withAlphaComponent(0.5)
-
- contentView.addSubview(alertVC.view)
- alertVC.view.snp.makeConstraints { make in
- make.leading.trailing.bottom.top.equalToSuperview()
- }
- }
- }
- extension TSPurchaseVideoTimesVC {
- func onPurchaseStateChanged(){
- PurchaseManager.default.onPurchaseStateChanged = { [weak self] manager,state,object in
- guard let self = self else { return }
-
- if isHandlePurchaseStateChanged == false {
- debugPrint("purchaseManager.onPurchaseStateChanged 不处理")
- return
- }
-
- DispatchQueue.main.async {
- switch state {
- case .none:
- break
- case .loading:
- TSToastShared.showLoading(text: "Getting price".localized,containerView: self.view)
- case .loadSuccess:
- TSToastShared.hideLoading()
- case .loadFail:
- TSToastShared.hideLoading()
- let message = "Failed to get the price, will automatically retry in 5 seconds".localized
- TSToastShared.showToast(text: message)
- DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
- PurchaseManager.default.requestProducts()
- }
- case .paying:
- TSToastShared.showLoading(text: "Purchasing now".localized,containerView: self.view)
- case .paySuccess:
- TSToastShared.hideLoading()
- var loadingText = "Congratulations on your purchase! You have %d video effect uses. Try now!".localized
- if let purchaseNum = self.viewModel.selectedType.purchaseNum {
- loadingText = String(format: "Congratulations on your purchase! You have %d video effect uses. Try now!".localized, purchaseNum)
- }
- debugPrint(loadingText)
- TSToastShared.showToast(text:loadingText)
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- self.closePage()
- }
- case .payFail:
- TSToastShared.hideLoading()
- if let str = object as? String {
- TSToastShared.showToast(text: str)
- }
-
- case .restoreing:
- TSToastShared.showLoading(text: "Restoring now".localized,containerView: self.view)
- case .restoreSuccess:
- TSToastShared.hideLoading()
- var loadingText = "Congratulations on your purchase! You have %d video effect uses. Try now!".localized
- if let purchaseNum = self.viewModel.selectedType.purchaseNum {
- loadingText = String(format: "Congratulations on your purchase! You have %d video effect uses. Try now!".localized, purchaseNum)
- }
- debugPrint(loadingText)
- TSToastShared.showToast(text:loadingText)
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- self.closePage()
- }
-
- case .restoreFail:
- TSToastShared.hideLoading()
- let loadingText = (object as? String) ?? "Failed to restore subscribe, please try again".localized
- debugPrint(loadingText)
- TSToastShared.showToast(text: loadingText)
- case .verifying:
- #if DEBUG
- TSToastShared.showLoading(text: "Verifying receipt...".localized,containerView: self.view)
- #endif
- case .verifySuccess:
- break
- case .verifyFail:
- #if DEBUG
- TSToastShared.hideLoading()
- let message = (object as? String) ?? "Failed to validate receipt".localized
- TSToastShared.showToast(text:message)
- #endif
- }
- }
- debugPrint("PurchaseManager onPurchaseStateChanged=\(String(describing: state))")
- }
- }
-
- @objc func closePage(){
- TSToastShared.hideLoading()
- self.dismiss(animated: true)
- NotificationCenter.default.post(name: .kCloseTSPurchaseVC, object: nil)
- }
- }
|