SettingViewController.swift
4.62 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
//
// SettingController.swift
// browser
//
// Created by Artem Talko on 26.09.2023.
//
import UIKit
import MessageUI
import StoreKit
final class SettingViewController: UIViewController {
private let mainView = SettingView()
private let settingTableViewData = StringConstants.settingViewControllerTableViewData
init() {
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()
view.backgroundColor = .white
initViewController()
}
private func initViewController() {
navigationController?.isNavigationBarHidden = false
setupSettingTableView()
addTargets()
}
}
//MARK: SettingTableView
extension SettingViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return settingTableViewData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: SettingTableViewCell.cellId, for: indexPath) as? SettingTableViewCell
else {
return UITableViewCell()
}
let info = settingTableViewData[indexPath.item]
cell.settingTableViewLabel.text = info
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let index = indexPath.row
cellPresshandler(index)
}
}
//MARK: - Helpers
extension SettingViewController {
private func setupSettingTableView() {
mainView.settingTableView.delegate = self
mainView.settingTableView.dataSource = self
mainView.settingTableView.register(SettingTableViewCell.self, forCellReuseIdentifier: SettingTableViewCell.cellId)
}
private func setupMessage() {
guard MFMailComposeViewController.canSendMail() else {
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["creattechllc@gmail.com"])
composer.setSubject("Browser")
present(composer, animated: true, completion: nil)
}
private func cellPresshandler(_ index: Int) {
switch index {
case 0:
let privacyViewController = PrivacyViewController()
presentViewController(privacyViewController)
case 1:
let termsViewController = TermsViewController()
presentViewController(termsViewController)
case 2:
shareButtonPressed()
case 3:
break
case 4:
setupMessage()
case 5:
Task {
try? await SubscriptionManager.shared.restorePurchase()
}
default:
break
}
}
private func addTargets() {
mainView.backButton.addTarget(self, action: #selector(closeViewController(_ :)), for: .touchUpInside)
}
private func shareButtonPressed() {
let items = [URL(string: URLConstants.shareAppLink)]
let shareView = UIActivityViewController(activityItems: items as [Any], applicationActivities: nil)
present(shareView, animated: true)
}
private func presentViewController(_ viewController: UIViewController) {
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true)
}
}
//MARK: - Action
extension SettingViewController {
@objc
private func closeViewController(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
//MARK: Mail helper
extension SettingViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
controller.dismiss(animated: true)
}
switch result {
case .cancelled:
print("cancelled")
case .saved:
print("saved")
case .sent:
print("send")
case .failed:
print("fail")
@unknown default:
print("error!")
}
controller.dismiss(animated: true)
}
}