mirror of
https://github.com/confirmedcode/Lockdown-iOS.git
synced 2025-12-21 12:14:02 +01:00
258 lines
12 KiB
Swift
258 lines
12 KiB
Swift
//
|
||
// AdvancedPaywall.swift
|
||
// LockdownSandbox
|
||
//
|
||
// Created by Алишер Ахметжанов on 29.04.2023.
|
||
//
|
||
|
||
import UIKit
|
||
|
||
final class FirewallPaywallViewController: UIViewController {
|
||
|
||
//MARK: Properties
|
||
private var titleName = NSLocalizedString("Lockdown", comment: "")
|
||
|
||
private lazy var navigationView: ConfiguredNavigationView =
|
||
{
|
||
let view = ConfiguredNavigationView()
|
||
view.rightNavButton.setTitle(NSLocalizedString("RESTORE", comment: ""), for: .normal)
|
||
view.titleLabel.text = NSLocalizedString(titleName, comment: "")
|
||
view.leftNavButton.setTitle(NSLocalizedString("CLOSE", comment: ""), for: .normal)
|
||
view.leftNavButton.addTarget(self, action: #selector(closeButtonClicked), for: .touchUpInside)
|
||
view.rightNavButton.addTarget(self, action: #selector(restoreButtonClicked), for: .touchUpInside)
|
||
|
||
return view
|
||
}()
|
||
|
||
private lazy var annualPlan: AdvancedPlansViews = {
|
||
let view = AdvancedPlansViews()
|
||
view.title.text = "Annual"
|
||
view.detailTitle.text = "$29.99/year"
|
||
view.detailTitle2.text = "$2.49/month"
|
||
view.discountImageView.image = UIImage(named: "saveDiscount")
|
||
view.iconImageView.image = UIImage(named: "fill-1")
|
||
view.backgroundView.layer.borderColor = UIColor.white.cgColor
|
||
view.isUserInteractionEnabled = true
|
||
|
||
view.setOnClickListener { [unowned self] in
|
||
annualView.isHidden = false
|
||
monthlyView.isHidden = true
|
||
|
||
view.iconImageView.image = UIImage(named: "fill-1")
|
||
view.backgroundView.layer.borderColor = UIColor.white.cgColor
|
||
|
||
monthlyPlan.iconImageView.image = UIImage(named: "grey-ellipse-1")
|
||
monthlyPlan.backgroundView.layer.borderColor = UIColor.borderGray.cgColor
|
||
|
||
ftPriceLabel.text = "7-day free trial, then $29.99 yearly. Cancel anytime."
|
||
}
|
||
return view
|
||
}()
|
||
|
||
private lazy var monthlyPlan: AdvancedPlansViews = {
|
||
let view = AdvancedPlansViews()
|
||
view.title.text = "Monthly"
|
||
view.detailTitle.text = "$4.99/month"
|
||
view.detailTitle2.text = " "
|
||
view.isUserInteractionEnabled = true
|
||
|
||
view.setOnClickListener { [unowned self] in
|
||
monthlyView.isHidden = false
|
||
annualView.isHidden = true
|
||
|
||
view.iconImageView.image = UIImage(named: "fill-1")
|
||
view.backgroundView.layer.borderColor = UIColor.white.cgColor
|
||
|
||
annualPlan.iconImageView.image = UIImage(named: "grey-ellipse-1")
|
||
annualPlan.backgroundView.layer.borderColor = UIColor.borderGray.cgColor
|
||
|
||
ftPriceLabel.text = "7-day free trial, then $4.99 monthly. Cancel anytime."
|
||
}
|
||
return view
|
||
}()
|
||
|
||
private lazy var plansStack: UIStackView = {
|
||
let stack = UIStackView()
|
||
stack.axis = .horizontal
|
||
stack.addArrangedSubview(annualPlan)
|
||
stack.addArrangedSubview(monthlyPlan)
|
||
stack.alignment = .leading
|
||
stack.distribution = .fillEqually
|
||
stack.spacing = 16
|
||
return stack
|
||
}()
|
||
|
||
lazy var ftPriceLabel: UILabel = {
|
||
let label = UILabel()
|
||
label.text = NSLocalizedString("7-day free trial, then $29.99 yearly. Cancel anytime.", comment: "")
|
||
label.textColor = .smallGrey
|
||
label.font = fontMedium11
|
||
label.textAlignment = .center
|
||
label.numberOfLines = 0
|
||
return label
|
||
}()
|
||
|
||
private lazy var freeTrialButton: UIButton = {
|
||
let button = UIButton(type: .system)
|
||
button.tintColor = .white
|
||
button.backgroundColor = .tunnelsBlue
|
||
button.layer.cornerRadius = 28
|
||
button.addTarget(self, action: #selector(tryButtonClicked), for: .touchUpInside)
|
||
let titleLabel = UILabel()
|
||
let title = NSLocalizedString("Try 7-day free trial", comment: "")
|
||
titleLabel.font = fontSemiBold17
|
||
titleLabel.text = title
|
||
titleLabel.textColor = .white
|
||
titleLabel.textAlignment = .center
|
||
|
||
button.addSubview(titleLabel)
|
||
titleLabel.anchors.top.pin(inset: 16)
|
||
titleLabel.anchors.bottom.pin(inset: 16)
|
||
titleLabel.anchors.leading.pin(inset: 24)
|
||
titleLabel.anchors.trailing.pin(inset: 24)
|
||
button.anchors.height.equal(56)
|
||
return button
|
||
}()
|
||
|
||
private lazy var annualView: AnnualPlanView = {
|
||
let view = AnnualPlanView()
|
||
return view
|
||
}()
|
||
|
||
private lazy var monthlyView: MonthlyPlanView = {
|
||
let view = MonthlyPlanView()
|
||
view.isHidden = true
|
||
return view
|
||
}()
|
||
|
||
private lazy var privacyLabel: UILabel = {
|
||
let label = UILabel()
|
||
label.font = fontMedium11
|
||
label.textAlignment = .center
|
||
label.numberOfLines = 0
|
||
let attributedText = NSMutableAttributedString(string: NSLocalizedString("By continuing you agree with our ", comment: ""), attributes: [NSAttributedString.Key.font: fontMedium11, NSAttributedString.Key.foregroundColor: UIColor.smallGrey])
|
||
let termsRange = NSRange(location: attributedText.length, length: NSLocalizedString("Terms of Service", comment: "").count)
|
||
attributedText.append(NSAttributedString(string: NSLocalizedString("Terms of Service", comment: ""), attributes: [NSAttributedString.Key.font: fontMedium11, NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.link: URL(string: "https://lockdownprivacy.com/terms")!]))
|
||
attributedText.append(NSAttributedString(string: NSLocalizedString(" and ", comment: ""), attributes: [NSAttributedString.Key.font: fontMedium11, NSAttributedString.Key.foregroundColor: UIColor.smallGrey]))
|
||
let privacyRange = NSRange(location: attributedText.length, length: NSLocalizedString("Privacy Policy", comment: "").count)
|
||
attributedText.append(NSAttributedString(string: NSLocalizedString("Privacy Policy", comment: ""), attributes: [NSAttributedString.Key.font: fontMedium11, NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.link: URL(string: "https://lockdownprivacy.com/privacy")!]))
|
||
|
||
let paragraphStyle = NSMutableParagraphStyle()
|
||
paragraphStyle.alignment = .center
|
||
attributedText.addAttributes([NSAttributedString.Key.paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: attributedText.length))
|
||
label.attributedText = attributedText
|
||
|
||
label.isUserInteractionEnabled = true
|
||
|
||
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(labelTapped(sender:)))
|
||
label.addGestureRecognizer(tapGesture)
|
||
return label
|
||
}()
|
||
|
||
//MARK: Lificycle
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
let gradientLayer = CAGradientLayer()
|
||
gradientLayer.colors = [UIColor.purplePaywall.cgColor, UIColor.purplePaywall2.cgColor]
|
||
gradientLayer.frame = view.bounds
|
||
|
||
view.layer.insertSublayer(gradientLayer, at: 0)
|
||
configureUI()
|
||
}
|
||
|
||
//MARK: ConfigureUI
|
||
private func configureUI() {
|
||
view.addSubview(navigationView)
|
||
navigationView.anchors.top.safeAreaPin(inset: 18)
|
||
navigationView.anchors.leading.marginsPin()
|
||
navigationView.anchors.trailing.marginsPin()
|
||
|
||
view.addSubview(privacyLabel)
|
||
privacyLabel.anchors.bottom.safeAreaPin()
|
||
privacyLabel.anchors.leading.marginsPin()
|
||
privacyLabel.anchors.trailing.marginsPin()
|
||
privacyLabel.anchors.height.equal(34)
|
||
|
||
view.addSubview(freeTrialButton)
|
||
freeTrialButton.anchors.bottom.spacing(8, to: privacyLabel.anchors.top)
|
||
freeTrialButton.anchors.leading.marginsPin()
|
||
freeTrialButton.anchors.trailing.marginsPin()
|
||
|
||
view.addSubview(ftPriceLabel)
|
||
ftPriceLabel.anchors.bottom.spacing(8, to: freeTrialButton.anchors.top)
|
||
ftPriceLabel.anchors.leading.marginsPin()
|
||
ftPriceLabel.anchors.trailing.marginsPin()
|
||
|
||
view.addSubview(plansStack)
|
||
plansStack.anchors.bottom.spacing(20, to: ftPriceLabel.anchors.top)
|
||
plansStack.anchors.leading.pin(inset: 16)
|
||
plansStack.anchors.trailing.pin(inset: 16)
|
||
|
||
view.addSubview(annualView)
|
||
annualView.anchors.top.spacing(24, to: navigationView.anchors.bottom)
|
||
annualView.anchors.leading.pin()
|
||
annualView.anchors.trailing.pin()
|
||
annualView.anchors.bottom.spacing(8, to: plansStack.anchors.top)
|
||
|
||
view.addSubview(monthlyView)
|
||
monthlyView.anchors.top.spacing(24, to: navigationView.anchors.bottom)
|
||
monthlyView.anchors.leading.pin()
|
||
monthlyView.anchors.trailing.pin()
|
||
monthlyView.anchors.bottom.spacing(8, to: plansStack.anchors.top)
|
||
}
|
||
|
||
//MARK: Functions
|
||
|
||
@objc func closeButtonClicked() {
|
||
dismiss(animated: true)
|
||
}
|
||
|
||
@objc func restoreButtonClicked() {
|
||
|
||
}
|
||
|
||
@objc func tryButtonClicked() {
|
||
|
||
}
|
||
|
||
@objc private func labelTapped(sender: UITapGestureRecognizer) {
|
||
let termsRange = NSRange(location: privacyLabel.attributedText!.length - NSLocalizedString("Terms of Service", comment: "").count - 18, length: NSLocalizedString("Terms of Service", comment: "").count)
|
||
let privacyRange = NSRange(location: privacyLabel.attributedText!.length - NSLocalizedString("Privacy Policy", comment: "").count, length: NSLocalizedString("Privacy Policy", comment: "").count)
|
||
|
||
if sender.didTapAttributedTextInLabel(label: privacyLabel, inRange: privacyRange),
|
||
let url = URL(string: "https://lockdownprivacy.com/privacy") {
|
||
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
||
} else if sender.didTapAttributedTextInLabel(label: privacyLabel, inRange: termsRange),
|
||
let url = URL(string: "https://lockdownprivacy.com/terms") {
|
||
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
||
}
|
||
}
|
||
}
|
||
|
||
extension UITapGestureRecognizer {
|
||
func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
|
||
guard let attributedText = label.attributedText else { return false }
|
||
|
||
let layoutManager = NSLayoutManager()
|
||
let textContainer = NSTextContainer(size: CGSize.zero)
|
||
let textStorage = NSTextStorage(attributedString: attributedText)
|
||
layoutManager.addTextContainer(textContainer)
|
||
textStorage.addLayoutManager(layoutManager)
|
||
|
||
textContainer.lineFragmentPadding = 0.0
|
||
textContainer.lineBreakMode = label.lineBreakMode
|
||
textContainer.maximumNumberOfLines = label.numberOfLines
|
||
textContainer.size = label.bounds.size
|
||
let locationOfTouchInLabel = self.location(in: label)
|
||
let textBoundingBox = layoutManager.usedRect(for: textContainer)
|
||
let textContainerOffset = CGPoint(x: (label.bounds.width - textBoundingBox.width) * 0.5 - textBoundingBox.minX,
|
||
y: (label.bounds.height - textBoundingBox.height) * 0.5 - textBoundingBox.minY)
|
||
let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x,
|
||
y: locationOfTouchInLabel.y - textContainerOffset.y)
|
||
let index = layoutManager.characterIndex(for: locationOfTouchInTextContainer,
|
||
in: textContainer,
|
||
fractionOfDistanceBetweenInsertionPoints: nil)
|
||
return NSLocationInRange(index, targetRange)
|
||
}
|
||
}
|