NewPaywallViewController.swift
5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// NewPaywallViewController.swift
// Pro-Seecurity-VPN
//
// Created by Volodymyr Kolomyltsev on 12/22/23.
//
import UIKit
import StoreKit
class NewPaywallViewController: UIViewController {
var mainView: OnboardingView
private var currentSelectedIndex: Int = 0
private let storeKitManager: SubscriptionManager
private let successCompletion: () -> Void
var subscriptionInfo: SKProduct? {
didSet {
updateSubscriptionLabels()
}
}
init(storeKit: SubscriptionManager, completion: @escaping () -> Void) {
self.storeKitManager = storeKit
self.successCompletion = completion
self.mainView = OnboardingView()
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
view = mainView
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
updateSubscriptionLabels()
}
private func setupUI() {
mainView.scrollView.delegate = self
mainView.privacyButton.addTarget(self, action: #selector(termsTapped), for: .touchUpInside)
mainView.termsButton.addTarget(self, action: #selector(policyTapped), for: .touchUpInside)
mainView.restoreButton.addTarget(self, action: #selector(restoreTapped), for: .touchUpInside)
mainView.closeButton.addTarget(self, action: #selector(closeViewController(_:)), for: .touchUpInside)
mainView.activateButton.addTarget(self, action: #selector(buyAdblocker), for: .touchUpInside)
}
private func updateSubscriptionLabels() {
if let price = storeKitManager.storeProducts.first?.displayPrice {
mainView.infoLabel
.text = "3-day free trial, then \(price) per week."
}
}
private func presentViewController(_ viewController: UIViewController) {
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true)
}
}
// MARK: - UIScrollViewDelegate
extension NewPaywallViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x / view.frame.width)
mainView.pageControl.currentPage = Int(pageIndex)
currentSelectedIndex = Int(pageIndex)
}
}
// MARK: - Targets
extension NewPaywallViewController {
@objc private func termsTapped() {
let termsViewController = TermsViewController()
presentViewController(termsViewController)
}
@objc private func policyTapped() {
let privacyViewController = PrivacyViewController()
presentViewController(privacyViewController)
}
@objc private func restoreTapped() {
Task {
try? await SubscriptionManager.shared.restorePurchase()
}
}
@objc private func closeViewController(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
private func isSubscriptionValid() -> Bool {
guard let expirationDate = CachingManager.shared.expirationDate else {
return false
}
let currentDate = Date().timeIntervalSince1970
return expirationDate > currentDate
}
@objc
private func buyAdblocker(_ sender: UIButton) {
ProgressHelper.show()
sender.isEnabled = false
Task {
defer {
ProgressHelper.hide()
sender.isEnabled = true
}
do {
try await buySubscription()
} catch {
let alert = UIAlertController(title: "Fail!", message: "Error with subscription", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alert.addAction(okAction)
present(alert, animated: true)
}
}
}
@MainActor
private func buySubscription() async throws {
if CachingManager.shared.isSubscriptionActive {
dismiss(animated: true)
successCompletion()
return
}
if let adblockerProduct = storeKitManager.storeProducts.first(where: { $0.id == SubscriptionsModel.subscriptionInfo["adBlocker"]}) {
do {
if let transaction = try await storeKitManager.purchase(adblockerProduct) {
CachingManager.shared.expirationDate = transaction.expirationDate?.timeIntervalSince1970 ?? 0
dismiss(animated: true)
successCompletion()
VPNAnalytics.logEvent(eventType: .startTrial, params: ["subscription_id": "\(transaction.originalID)"])
}
} catch {
print("Error purchasing product: \(error)")
}
}
}
}
// MARK: - Helpers
extension NewPaywallViewController {
private func moveScrollViewTo(page: Int) {
var currentContentOffset = mainView.scrollView.contentOffset
let width = mainView.scrollView.frame.width
currentContentOffset.x = width * CGFloat(page)
currentSelectedIndex = page
mainView.scrollView.setContentOffset(currentContentOffset, animated: true)
}
private func showErrorAlert(message: String) {
let alert = UIAlertController(title: "Fail!", message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alert.addAction(okAction)
present(alert, animated: true)
}
}