RemoveAdvertViewController.swift
5.88 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
//
// RemoveAdvertViewController.swift
// browser
//
// Created by Artem Talko on 27.09.2023.
//
import UIKit
final class RemoveAdvertViewController: UIViewController {
private let mainView = RemoveAdvertView()
private let notification: NotificationManager
private var storeKit: SubscriptionManager
private let tableViewData = StringConstants.tableViewData
init() {
storeKit = SubscriptionManager()
notification = NotificationManager()
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
initViewController()
}
override func loadView() {
view = mainView
}
private func initViewController() {
addTargets()
setupTableView()
}
}
//MARK: - Action
extension RemoveAdvertViewController {
@objc
private func adBlockerButtonPressed(_ sender: UIButton) {
if CachingManager.shared.adBlockerState {
turnOffAdBlocker()
} else {
subscriptionHandler()
}
}
@objc
private func advertSegmantControlStateHandler() {
let browserHomeViewController = BrowserHomeViewController(url: nil, currentTabId: nil)
let tabsViewController = TabsViewController()
navigationController?.setViewControllers([tabsViewController, browserHomeViewController], animated: true)
}
}
//MARK: Helpers
extension RemoveAdvertViewController {
private func subscriptionHandler() {
if storeKit.purchasedSubscriptions.isEmpty {
Task {
try? await storeKit.restorePurchase()
if storeKit.purchasedSubscriptions.isEmpty {
presentSubscriptionHandler()
} else {
if let restoreDate = CachingManager.shared.expirationDate {
if Date().timeIntervalSince1970 < restoreDate {
turnOnAdBlocker()
} else {
presentSubscriptionHandler()
}
} else {
DateManager.shared.getSubscriptionExpirationDate()
let restoreDate = CachingManager.shared.expirationDate
if Date().timeIntervalSince1970 < restoreDate ?? 0 {
turnOnAdBlocker()
} else {
presentSubscriptionHandler()
}
}
}
}
} else {
if let restoreDate = CachingManager.shared.expirationDate {
if Date().timeIntervalSince1970 < restoreDate {
turnOnAdBlocker()
} else {
presentSubscriptionHandler()
}
} else {
DateManager.shared.getSubscriptionExpirationDate()
let restoreDate = CachingManager.shared.expirationDate
if Date().timeIntervalSince1970 < restoreDate ?? 0 {
turnOnAdBlocker()
} else {
presentSubscriptionHandler()
}
}
}
}
private func addTargets() {
mainView.adBlockButton.addTarget(self, action: #selector(adBlockerButtonPressed(_ :)), for: .touchUpInside)
mainView.removeAdvertSegmentControl.addTarget(self, action: #selector(advertSegmantControlStateHandler), for: .valueChanged)
}
private func presentSubscriptionHandler() {
let subcriptionViewController = SubscriptionViewController(storeKit: storeKit, complition: testHandler)
subcriptionViewController.modalPresentationStyle = .fullScreen
subcriptionViewController.modalTransitionStyle = .crossDissolve
present(subcriptionViewController, animated: true)
}
private func testHandler() {
let alert = UIAlertController(title: "Congradulations!", message: "Success by subscription", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alert.addAction(okAction)
present(alert, animated: true)
turnOnAdBlocker()
}
private func setupTableView() {
mainView.adBlockTableView.dataSource = self
mainView.adBlockTableView.delegate = self
mainView.adBlockTableView.register(RemoveAdvertTableViewCell.self, forCellReuseIdentifier: RemoveAdvertTableViewCell.cellID)
}
private func turnOnAdBlocker() {
mainView.adBlockButton.buttonImageView.image = .adBlockOnState
mainView.tapActionLabel.textColor = ColorConstants.SearchbarTintBlue
mainView.tapActionLabel.text = StringConstants.turnOff
CachingManager.shared.adBlockerState = true
}
private func turnOffAdBlocker() {
mainView.adBlockButton.buttonImageView.image = .adBlockOffState
mainView.tapActionLabel.textColor = ColorConstants.customPink
mainView.tapActionLabel.text = StringConstants.turnOn
CachingManager.shared.adBlockerState = false
}
}
//MARK: - Table View
extension RemoveAdvertViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: RemoveAdvertTableViewCell.cellID) as?
RemoveAdvertTableViewCell else {
return UITableViewCell()
}
cell.advantagesCellLabel.text = tableViewData[indexPath.row]
return cell
}
}