SearchResultViewController.swift
2.42 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
//
// 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
}
}
//MARK: URL handling
extension SearchResultViewController {
private func urlChecker(_ url: String) {
let request = URLRequest(url: URL(string: url) ?? URL(fileURLWithPath: ""))
mainView.searchResultView.load(request)
}
}
//MARK: AdBlocker
extension SearchResultViewController {
private func setupAdBlocker() {
let userDefaultsAdblockKey = CachingManager.shared.adBlockerState
if userDefaultsAdblockKey {
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)
}
}
}
}
}
}
}