123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- //
- // TSPurchaseVC.swift
- // TSLiveWallpaper
- //
- // Created by 100Years on 2025/1/14.
- //
- import Combine
- import SwiftUI
- import SwiftUIX
- class PurchaseViewModel : ObservableObject{
-
- @Published var selectedType: PremiumPeriod = .year//.lifetime
-
- /// 订阅publisher
- let buyPublisher = PassthroughSubject<Bool,Never>()
- /// 隐私
- let privacyPublisher = PassthroughSubject<Bool, Never>()
- /// term
- let termPublisher = PassthroughSubject<Bool, Never>()
- /// restore
- let restorePublisher = PassthroughSubject<Bool, Never>()
- }
- class TSPurchaseVC: TSBaseVC {
-
- var closePageBlock:(()->Void)?
-
- var viewModel: PurchaseViewModel = .init()
- var cancellabel: [AnyCancellable] = []
- var buyPeriod:PremiumPeriod = .year
- lazy var purchaseManager: PurchaseManager = {
- let purchaseManager = PurchaseManager.default
- return purchaseManager
- }()
-
- lazy var hostVc: UIHostingController<PurchaseView> = {
- let vc = UIHostingController(rootView: PurchaseView(viewModel: viewModel))
- vc.view.backgroundColor = .clear
- return vc
- }()
-
-
- lazy var imageComparisonView: TYCycleImageComparisonView = {
- let imageComparisonView = TYCycleImageComparisonView()
- imageComparisonView.frame = CGRect(x: 0, y: 0, width: k_ScreenWidth, height: 532*kDesignScale)
- imageComparisonView.titleArray = ["Descratch".localized,"Colorize".localized,"Enhance".localized,"Recreate".localized]
- imageComparisonView.itemModelArray = [
- TSImageComparisonModel(oldImage: UIImage(named: "image_comparison_old_0")!, newImage: UIImage(named: "image_comparison_new_0")!),
- TSImageComparisonModel(oldImage: UIImage(named: "image_comparison_old_1")!, newImage: UIImage(named: "image_comparison_new_1")!),
- TSImageComparisonModel(oldImage: UIImage(named: "image_comparison_old_2")!, newImage: UIImage(named: "image_comparison_new_2")!),
- TSImageComparisonModel(oldImage: UIImage(named: "image_comparison_old_3")!, newImage: UIImage(named: "image_comparison_new_3")!)
- ]
- return imageComparisonView
- }()
-
- override func createView() {
- addNormalNavBarView()
- _ = setNavigationItem("", imageName: "close_gray", direction: .left, action: #selector(closePage))
- setViewBgImageNamed(named: "purchase_bg_shade")
- view.insertSubview(imageComparisonView, at: 0)
-
- contentView.addSubview(hostVc.view)
- hostVc.view.snp.makeConstraints { make in
- make.leading.trailing.bottom.top.equalToSuperview()
- }
- }
-
- override func dealThings() {
- PurchaseManager.default.requestProducts()
- addNotifaction()
- onPurchaseStateChanged()
- }
-
-
- func addNotifaction() {
- viewModel.buyPublisher.receive(on: DispatchQueue.main).sink { [weak self] _ in
- guard let self = self else {
- return
- }
- PurchaseManager.default.pay(for: self.viewModel.selectedType)
- }.store(in: &cancellabel)
- viewModel.privacyPublisher.receive(on: DispatchQueue.main).sink { [weak self] _ in
- guard let self = self else {
- return
- }
-
- let vc = TSBusinessWebVC(urlType: .privacy)
- vc.hidesBottomBarWhenPushed = true
- kPresentModalVC(target: self, modelVC: vc)
-
- }.store(in: &cancellabel)
- viewModel.termPublisher.receive(on: DispatchQueue.main).sink { [weak self] _ in
- guard let self = self else {
- return
- }
-
- let vc = TSBusinessWebVC(urlType: .terms)
- vc.hidesBottomBarWhenPushed = true
- kPresentModalVC(target: self, modelVC: vc)
- }.store(in: &cancellabel)
- viewModel.restorePublisher.receive(on: DispatchQueue.main).sink { _ in
- PurchaseManager.default.restorePremium()
- }.store(in: &cancellabel)
- }
-
-
- func onPurchaseStateChanged(){
- purchaseManager.onPurchaseStateChanged = { [weak self] manager,state,object in
- guard let self = self else { 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 = "Get price failure, 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()
- let loadingText = manager.isVip ? "Congratulation you have become VIP".localized : "Finish".localized
- TSToastShared.showToast(text:loadingText)
- if manager.isVip {
- 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()
- let loadingText = manager.isVip ? "Congratulation you have become VIP".localized : "Couldn't Restore Subscription".localized
- debugPrint(loadingText)
- TSToastShared.showToast(text:loadingText)
- if manager.isVip {
- 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) ?? "Verify receipt failed".localized()
- TSToastShared.showToast(text:message)
- #endif
- }
- }
- debugPrint("PurchaseManager onPurchaseStateChanged=\(String(describing: state))")
- }
- }
-
- @objc func closePage(){
- closePageBlock?()
- TSToastShared.hideLoading()
- self.dismiss(animated: true)
- }
-
-
- deinit {
- cancellabel.removeAll()
- }
- }
- extension TSPurchaseVC{
-
- static func show(target:UIViewController,closePageBlock:(()->Void)? = nil){
- let vc = TSPurchaseVC()
- vc.closePageBlock = closePageBlock
- let navi = TSBaseNavigationC(rootViewController: vc)
- navi.modalPresentationStyle = .overFullScreen
- target.present(navi, animated: true)
- }
- }
- struct PurchaseView :View {
-
- @ObservedObject var viewModel: PurchaseViewModel
-
- var body: some View {
- let vipType = PurchaseManager.default.vipType
- VStack {
- Spacer()
-
- VStack {
-
- // let text = vipType == .none ? "Get PRO Access".localized : "Super Offer for Yearly Pro".localized
- // 自定义颜色
- HighlightedText("Get PRO Access",
- highlighted: "PRO",
- baseColor: .gray,
- highlightColor: .yellow)
- .multilineTextAlignment(.center)
- .font(.font(name: .ZillaSlabBoldItalic,size: 36))
- // .foregroundColor(UIColor.white.color)
- .frame(width: k_ScreenWidth - 32, alignment: .center)
-
-
- }
-
- Spacer().frame(height: 30)
-
- VStack(spacing: 12) {
-
- // if vipType == .none {
- ZStack(alignment: .topTrailing) {
- // //增加月付费
- // PurchaseItemView(title: "One Month".localized, type: .month, selectedType: $viewModel.selectedType).onTapGesture {
- // viewModel.selectedType = .month
- PurchaseItemView(title: "YEARLY ACCESS".localized, type: .year, selectedType: $viewModel.selectedType).onTapGesture {
- // viewModel.selectedType = .year
- // viewModel.buyPublisher.send(true)
- }
- TSVipRecView(save: vipType.saveString)
- .offset(x:-30,y:-14)
- }
-
- PurchaseItemView(title: "MONTH ACCESS".localized, type: .month, selectedType: $viewModel.selectedType).onTapGesture {
- viewModel.selectedType = .month
- viewModel.buyPublisher.send(true)
- }
-
- // }else{
- // PurchaseItemTypeOneView(title: "One Year".localized, type: .year, selectedType: $viewModel.selectedType).onTapGesture {
- // viewModel.selectedType = .year
- // viewModel.buyPublisher.send(true)
- // }
- // }
- Button {
- viewModel.buyPublisher.send(true)
- } label: {
- ZStack {
- Image("submit_btn_bg").resizable().aspectRatio(contentMode: .fill)
- Text("Continue")
- .font(.system(size: 16))
- .foregroundColor(.hex("#111111"))
-
- }.frame(maxWidth: .infinity ,maxHeight: 48.0)
- .cornerRadius(0.0)
- }
-
- HStack {
- Text("Recurring billing, cancel anytime")
- .foregroundColor(UIColor.themeColor.color) +
- Text(", Payment will be charged to your iTunes account at confirmation of purchase. Subscriptions automatically renew for the same applicable term and price, unless auto-renew is turned off at least 24 hours before the end of the current period.")
- .foregroundColor(UIColor.lesserText.color)
- }
- .multilineTextAlignment(.center).font(.font(size: 8))
- .onTapGesture {
- viewModel.privacyPublisher.send(true)
- }
- HStack(spacing: 8) {
- Text("Term of us")
- .onTapGesture {
- viewModel.termPublisher.send(true)
- }
- Text("|")
- Text("Privacy Policy")
- .onTapGesture {
- viewModel.privacyPublisher.send(true)
- }
- Text("|")
- Text("Restore")
- .onTapGesture {
- viewModel.restorePublisher.send(true)
- }
- }.font(.system(size: 12)).foregroundColor(.hex("#999999"))
- }.padding(.horizontal)
-
- Spacer().frame(height:9+k_Height_safeAreaInsetsBottom())
- }
- }
- }
- struct PurchaseItemView: View {
- var title: String
- var type: PremiumPeriod
- @Binding var selectedType: PremiumPeriod
- var body: some View {
- let textColor = Color.white.opacity(0.6)
-
- ZStack {
- Color.white.opacity(0.1)
- HStack {
- //左边加个
- VStack(alignment: .leading, spacing: 14) {
- Text(title).font(.font(size: 16,weight: .medium)).foregroundColor(type == selectedType ? UIColor.themeColor.color : textColor)
- Text(priceString()).font(.font(size: 14))
- }
- Spacer()
- //右边每周的💰
- VStack(alignment: .trailing, spacing: 2) {
- Text("\(PurchaseManager.default.averageWeekly(for:type) ?? "--")")
- Text("Per week".localized)
- }.font(.font(size: 16,weight: .regular)).foregroundColor(Color.white.opacity(0.6))
- }.padding(.horizontal)
- }
- .foregroundColor(textColor)
- .frame(height: 74) // 设置高度
- .overlay(
- RoundedRectangle(cornerRadius: 0)
- .stroke(UIColor.themeColor.color, lineWidth: type == selectedType ? 1 : 0) // 边框
- )
- }
-
- func priceString()->String{
- switch type {
- case .year:
- return "Just \(PurchaseManager.default.price(for: type) ?? "--") Per Year"
- case .month:
- return "Just \(PurchaseManager.default.price(for: type) ?? "--") Per Week"
- default:
- return "--"
- }
-
- }
- }
- //推荐选择view
- struct TSVipRecView: View {
- var save:String
- var body: some View {
- HStack(spacing: 4) {
- // Image("upvote_black").resizable().frame(width: 16, height: 16)
- Text(save + " " + "OFF".localized).font(.font(size: 12,weight: .medium)).foregroundColor(.hex("#111111"))
- }
- .padding(EdgeInsets(top: 6, leading: 6, bottom: 6, trailing: 6))
- // .background(Color.hex("#FECB34"))
- // .background(
- .linearGradientBackground(colors: ["#F1D3AB".uiColor.color,"#E4A858".uiColor.color])
- .frame(height: 24) // 设置高度
- .cornerRadius(12.0)
- // .cornerRadius([.topLeading, .topTrailing, .bottomLeading, .bottomTrailing], 16.0)
- }
- }
- extension View {
- /// 添加线性渐变背景
- /// - Parameters:
- /// - colors: 渐变颜色数组
- /// - startPoint: 渐变起点 (默认 .top)
- /// - endPoint: 渐变终点 (默认 .bottom)
- /// - cornerRadius: 圆角半径 (默认 0)
- func linearGradientBackground(colors: [Color],
- startPoint: UnitPoint = .leading,
- endPoint: UnitPoint = .trailing,
- cornerRadius: CGFloat = 0) -> some View {
- self.background(
- LinearGradient(
- gradient: Gradient(colors: colors),
- startPoint: startPoint,
- endPoint: endPoint
- )
- .cornerRadius(cornerRadius)
- )
- }
-
- }
|