Music_Player3/relax.offline.mp3.music/MP/Common/Base(公用基类)/Controllers/MP_IAPViewController.swift
2024-08-15 15:50:48 +08:00

205 lines
7.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MP_IAPViewController.swift
// relax.offline.mp3.music
//
// Created by Mr.Zhou on 2024/7/31.
//
import UIKit
///
let IAPChooseedImage:UIImage? = .init(named: "IAP Chooseed'logo")
///
let IAPChooseImage:UIImage? = .init(named: "IAP Choose'logo")
///A/B
class MP_IAPViewController: UIViewController {
//A/B
@IBOutlet weak var bgImageView: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!
var isType:Bool = false
@IBOutlet weak var aConstraint: NSLayoutConstraint!
@IBOutlet weak var bConstraint: NSLayoutConstraint!
@IBOutlet weak var adView: UIView!
@IBOutlet weak var adLabel: UILabel!
@IBOutlet weak var musicView: UIView!
@IBOutlet weak var musicLabel: UILabel!
@IBOutlet weak var downloadView: UIView!
@IBOutlet weak var downloadLabel: UILabel!
@IBOutlet weak var weekView: UIView!{
didSet{
weekView.isUserInteractionEnabled = true
weekView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseLevelClick(_ :))))
}
}
@IBOutlet weak var weekImageView: UIImageView!
@IBOutlet weak var weekPriceLabel: UILabel!
@IBOutlet weak var yearView: UIView!{
didSet{
yearView.isUserInteractionEnabled = true
yearView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseLevelClick(_ :))))
}
}
@IBOutlet weak var yearImageView: UIImageView!
@IBOutlet weak var yearPriceLabel: UILabel!
@IBOutlet weak var lifeView: UIView!{
didSet{
lifeView.isUserInteractionEnabled = true
lifeView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseLevelClick(_ :))))
}
}
@IBOutlet weak var lifeImageView: UIImageView!
@IBOutlet weak var lifePriceLabel: UILabel!
@IBOutlet weak var noticeLabel: UILabel!{
didSet{
noticeLabel.text = NSLocalizedString("No mandatory,cancel at anytime", comment: "提醒用户可以取消订阅")
}
}
@IBOutlet weak var continueBtn: UIButton!{
didSet{
continueBtn.setTitle(NSLocalizedString("Continue", comment: "继续"), for: .normal)
}
}
@IBOutlet weak var leftBtn: UIButton!{
didSet{
leftBtn.setTitle(NSLocalizedString("Terms of Use", comment: "用户协议"), for: .normal)
}
}
@IBOutlet weak var centerBtn: UIButton!{
didSet{
centerBtn.setTitle(NSLocalizedString("Restore", comment: "恢复"), for: .normal)
}
}
@IBOutlet weak var rightBtn: UIButton!{
didSet{
rightBtn.setTitle(NSLocalizedString("Privacy Policy", comment: "隐私政策"), for: .normal)
}
}
var privacyBlock:(() -> Void)?
var termsBlock:(() -> Void)?
//
private var level:Int?
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentInsetAdjustmentBehavior = .never
MP_IAPManager.shared.requestProducts()
MP_AnalyticsManager.shared.VIP_page_impAction()
//isTypeA/B
if isType {
//B
bgImageView.image = UIImage(named: "IAP B'bg")
aConstraint.priority = .init(500)
bConstraint.priority = .init(999)
//
musicView.isHidden = false
downloadView.isHidden = false
}else {
//A
bgImageView.image = UIImage(named: "IAP A'bg")
aConstraint.priority = .init(999)
bConstraint.priority = .init(500)
//
musicView.isHidden = true
downloadView.isHidden = true
}
//
adLabel.attributedText = setSubstring("Ad-free experience, NO ADs!!", coloredText: "NO ADs!!")
musicLabel.text = "All \"YouTube\" music & videos"
downloadLabel.text = "Unlimited Downloads"
//
weekPriceLabel.attributedText = setPriceText(1.99)
yearPriceLabel.attributedText = setPriceText(14.99)
lifePriceLabel.attributedText = setPriceText(24.99)
}
//
private func setSubstring(_ fullText:String, coloredText:String) -> NSAttributedString {
//
let attributedString = NSMutableAttributedString(string: fullText, attributes: [.foregroundColor:UIColor.white, .font:UIFont.systemFont(ofSize: 16*width)])
let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor(hex: "#80F988")]
if let range = fullText.range(of: coloredText) {
let nsRange = NSRange(range, in: fullText)
attributedString.addAttributes(attributes, range: nsRange)
}
return attributedString
}
//
private func setPriceText(_ price:Double) -> NSAttributedString {
let font = UIFont(name: "DINAlternate-Bold", size: 24*width) ?? .systemFont(ofSize: 24*width, weight: .bold)
let attributedString = NSMutableAttributedString(string: "$\(price)", attributes: [.foregroundColor:UIColor.white, .font:font])
attributedString.addAttributes([.font:UIFont(name: "DINAlternate-Bold", size: 16*width) ?? .systemFont(ofSize: 16*width, weight: .bold)], range: .init(location: 0, length: 1))
return attributedString
}
//退
@IBAction func dismissClick(_ sender: UIButton) {
dismiss(animated: true)
}
//
@objc private func chooseLevelClick(_ sender:UITapGestureRecognizer) {
let tag = sender.view?.tag ?? 0
switch tag {
case 0://
weekPriceLabel.setGradientTextColor(gradientTextColors)
weekImageView.image = IAPChooseedImage
yearPriceLabel.cleanGradientTextColor()
yearImageView.image = IAPChooseImage
lifePriceLabel.cleanGradientTextColor()
lifeImageView.image = IAPChooseImage
case 1://
yearPriceLabel.setGradientTextColor(gradientTextColors)
yearImageView.image = IAPChooseedImage
weekPriceLabel.cleanGradientTextColor()
weekImageView.image = IAPChooseImage
lifePriceLabel.cleanGradientTextColor()
lifeImageView.image = IAPChooseImage
default://
lifePriceLabel.setGradientTextColor(gradientTextColors)
lifeImageView.image = IAPChooseedImage
weekPriceLabel.cleanGradientTextColor()
weekImageView.image = IAPChooseImage
yearPriceLabel.cleanGradientTextColor()
yearImageView.image = IAPChooseImage
}
level = tag
}
//
@IBAction func continueClick(_ sender: UIButton) {
guard let level = level else {
MP_HUD.onlytext("Please select your product type".localizableString(), delay: 1.0, completion: nil)
return
}
guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
MP_HUD.onlytext("Bad connection~".localizableString(), delay: 1.0, completion: nil)
return
}
MP_IAPManager.shared.purchaseProduct(level)
}
//
@IBAction func reStoreClick(_ sender: UIButton) {
MP_IAPManager.shared.restorePurchases()
}
//
@IBAction func privacyClick(_ sender: UIButton) {
dismiss(animated: true) {
[weak self] in
if let block = self?.privacyBlock {
block()
}
}
}
//
@IBAction func termsClick(_ sender: UIButton) {
dismiss(animated: true) {
[weak self] in
if let block = self?.termsBlock {
block()
}
}
}
}