SearchResultViewController.swift
2.39 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
//
// SearchResultViewColroller.swift
// browser
//
// Created by Artem Talko on 05.10.2023.
//
import UIKit
import WebKit
final class SearchResultViewController: UIViewController {
let mainView = SearchResultView()
var searchLink: String
init(searchLink: String) {
self.searchLink = searchLink
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()
initViewController()
}
private func initViewController() {
view.backgroundColor = ColorConstants.lightGray
urlChecker(searchLink)
setupAdBlocker()
}
override func viewIsAppearing(_ animated: Bool) {
navigationController?.isNavigationBarHidden = true
}
}
extension SearchResultViewController {
func urlChecker(_ url: String) {
let request = URLRequest(url: URL(string: url) ?? URL(fileURLWithPath: ""))
mainView.searchResultView.load(request)
}
}
//MARK: AdBlocker
extension SearchResultViewController {
func setupAdBlocker() {
let userDefaultsValue = CachingManager.shared.isAdBlocking
if userDefaultsValue {
WKContentRuleListStore.default().getAvailableContentRuleListIdentifiers { res in
if let url = Bundle.main.url(forResource: "blockerList", withExtension: "json"),
let str = try? String(contentsOf: url) {
WKContentRuleListStore.default().compileContentRuleList(forIdentifier: "blockerList", encodedContentRuleList: str) { list, error in
if let list {
self.mainView.searchResultView.configuration.userContentController.add(list)
}
}
}
}
} else {
WKContentRuleListStore.default().getAvailableContentRuleListIdentifiers { res in
if res != nil {
WKContentRuleListStore.default().removeContentRuleList(forIdentifier: "blockerList") { error in
if let error {
print(error)
}
}
}
}
}
}
}